Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

jQuery – How to select Input Nodes?

0.00/5 (No votes)
17 Dec 2013 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here