Click here to Skip to main content
15,908,015 members
Articles / jQuery

jQuery – How to select Input Nodes?

Rate me:
Please Sign up or sign in to vote.
4.75/5 (3 votes)
17 Dec 2013CPOL3 min read 6.7K   2  
jQuery – How to select Input Nodes?

In yesterday’s blog posts, we have discussed about Selecting Nodes by Attribute Value in jQuery. If you didn’t read that, I strongly recommend you read it before proceeding with this article. Here is the link jQuery – Selecting Nodes by Attribute Value. In this article, we will go over Selecting Input Nodes in jQuery.

Selecting Input Nodes using jQuery is very useful if you are working with Forms, TextBoxes, TextAreas, Select, etc., through which end users can input data into your application.

How Does Selecting Input Nodes Syntax Look Like?

  • A new character syntax $(‘:input’) used for this.
  • The above syntax will select all the input elements including Input, Select, TextArea, Button, Image, Radio and more. It will return a collection of input elements.
  • $(‘:input[type="radio"]‘) will specifically target RadioButtons on the web page.

Let’s look at an example. In my web page, I have a Form. In that Form, I have 4 input elements. The first one is the input element whose type is text and value is hard-coded to John. The second one is the input element LastNameTextBox whose type is text. The third one is TextArea for Comments. The fourth one is a button whose value is Submit.

jQuery.1

Let’s try to find the value of first Input TextBox which is John. $(‘:input’) will give the collection of all the 4 input elements. Assign that value to a variable called inputs. Then input[0].val() will give the value of the first input element which is FirstNameTextBox, whose value is John. Put that in an alert box. So in output, we should get John in the alert box.

jQuery.2

But while running the application, we will get nothing as output. So what went wrong here? Let’s analyze it. Go to Developer tools of Google Chrome.

jQuery.3

Notice that on the Console tab of Developer Tools, an error is showing – Uncaught TypeError: Object #<an HTMLInputElement> has no method ‘val’.

jQuery.4

The problem here is, val() is a jQuery function. In order to get a jQuery function, you have to use jQuery wrapper around the object. So in the 2 lines of code, we should use jQuery wrapper. Let’s modify the code like this and rerun the application.

jQuery.5

Now we will get the desired output which is John in the alert box.

jQuery.6

As I mentioned earlier, we have 4 input elements in our web page. Let’s try to iterate through each of them and find their values. $(‘:input’).each() is used for this purpose. Here each() function is used to iterate through each of the input elements. Write the code as given below and run the application.

jQuery.7

While running the application, we will get value of all the 4 input elements. While clicking on OK button, the value of next input element is displayed. First of all, we will get the value of FirstNameTextBox which is John.

jQuery.8

While clicking on OK button, we will get the empty LastNameTextBox value.

jQuery.9

Then, we will get the TextArea value.

jQuery.10

Finally, the button value which is Submit.

jQuery.11

Tomorrow

In tomorrow’s blog post, we will discuss some Additional Selector Features in jQuery.

Reference: Arun Ramachandran (http://BestTEchnologyBlog.Com)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
Arun Ramachandran is a Software Engineer having hands on experience in different Microsoft Technologies who is presently working in Experion Technologies, India. He has written over 95 articles on the subject on his blog at http://BestTEchnologyBlog.com. Along with 3 years of hands on experience he holds a Master of Computer Applications degree from Cochin University of Science & Technology (CUSAT).

Comments and Discussions

 
-- There are no messages in this forum --