tags:

views:

37

answers:

5

How can i apply jquery on all elements with same id attribute ?

i want to apply a focus() and blur() function on a textarea elements that have same id?

+2  A: 

You cannot have more than one element with the same ID, you should use classes, then access them like so.

$(".classname").focus()

And if you want to focus on a single ID use

$("#idname").focus()
Tom Walters
+2  A: 

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

However, you could 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>
Dustin Laine
A: 

You cannot/shouldn't have multiple elements with the same id. You could use a class selector but I would definitely fix the ids problem first.

Darin Dimitrov
A: 

you can't have more than one element with the same id not only is it invalid html but if you trageted it using jquery it would only effect the first one. you should use a class

$('.class_name').blur();
mcgrailm
A: 

It is best not to use the same id for 2 elements on the same page. Other forms that sets the textarea nodes can 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 use that too.

動靜能量
problem resolve. thanks every one
aniaz