jQuery Get Textarea ID Examples

In jQuery, Tips and Tricks by June 7th, 2012

1247 Views

Did you ever feel the need to use jquery to Get Textarea ID? In this article, I am going to share with you a very simple way to get textarea ID using jQuery. Actual code is just 1 line, so it’s super easy to follow and implement.

How to use jQuery to Get Textarea ID

Let’s assume that you have 3 textareas with ID “comments”, “skills” & “description”. The HTML code for the textareaes would be:

Now you want to get the ID of the textarea whose value has been changed. You can do it in the following manner:

Example 1: Get Textarea ID on change

When you run the above example, you will see the 3 input textareas. Simply enter anything in any of these textareas and click outside it so that the textarea loses focus. As soon as you do that, you will see the JavaScript alert that shows you the ID of the textarea in which you just made the change.

Example 2 – Get Textarea ID using name attribute

Let’s take the above example as the basis for this example. Let’s assume that you have only the name of the textarea and now you want to get the textarea ID using just the name. For illustration purposes, let’s assume that you want to get the ID of the “description” textarea using just it’s name. You can do it as below:

In the above example, when you run the page, you will receive a JavaScript alert box that gives you the ID of the “description” textarea. Note that we are using the “name” attribute of the “description” textarea in order to get it’s ID.

Example 3 – Get Textarea ID using class

Let’s assume that you have applied a class of “my_textarea” to all the textareas. You can now find out the ID of the textarea whose value has been changed using the class like so:

When you run the above example, you will see the 3 input textareas. Simply enter anything in any of them and click outside the textarea so that it loses focus. As soon as you do that, you will see the JavaScript alert that shows you the ID of the textarea in which you just made the change.

That’s it!

Do you know of any other ways to get textarea ID using jQuery? Feel free to suggest by commenting below.