How can I apply jquery to all elements with the same id attribute?

How can I apply jquery to all elements with the same id attribute?

I want to apply the focus() and blur() functions to textarea elements that have the same identifier?

+6
jquery
source share
8 answers

First, you should not have more than one element with the same ID .

However, you can do this with CSS classes:

 <div> <textarea class="Text"></textarea> <textarea class="Text"></textarea> <textarea class="Text"></textarea> </div> <script> $(".Text").focus(); $(".Text").blur(); </script> 
+14
source share

Correct answer:

 $("[id=yourID]").doSomething() 

for any code like

 <textarea id="yourID" /> <img id="yourID" /> <div id="yourID" /> 

yes, I know this is not true html, but note that middle-class developers often have to solve problems that were made "better" by developers. See, for example, Microsoft SharePoint, where several identifiers are very common. And we cannot tell our customers what Microsoft did wrong, so we cannot help them. We tell them that Microsoft did it poorly, so we should help them :-)

+17
source share

You cannot have more than one element with the same identifier, you must use classes and then access them like this.

 $(".classname").focus() 

And if you want to focus on one ID, use

 $("#idname").focus() 
+2
source share

It’s better not to use the same identifier for two elements on the same page. Other forms that textarea nodes set up may be

 $('textarea').blur() // note it sets all textarea elements on the page 

or

 $('#content textarea, #comment-area textarea').blur() 

or if you create a class for these textarea nodes, you can also use this.

+1
source share

You cannot / should not have multiple elements with the same identifier. You can use the class selector, but I would definitely fix the issue with identifiers.

0
source share

you cannot have more than one element with the same identifier, this is not only invalid html, but if you launched it using jquery, this would only affect the first one. you must use class

 $('.class_name').blur(); 
0
source share

It is very simple if you want to use `$ (" # yourID "). click (function () {});

0
source share

use jQuery delegate

syntax

 jQuery('body').delegate("#your-id","click",function(e){ // do code here }); 
0
source share

All Articles