Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get first span element of a div and change class using jQuery

Tags:

jquery

I have a div with a span

<div id="div1">
    <span class="">hello</span>
</div>

When i click on div i want to change the class of only first span element of the div

$('#div1').click(function() {
    // ... check if first span element inside the div .. if there and change the class..    
});  
like image 721
user1184100 Avatar asked Apr 04 '12 09:04

user1184100


People also ask

Which selector select the first element of the tag in jQuery?

The :first selector selects the first element.

How do you move the span of an element?

How to move span element with image right to the div tag? position: absolute removes the floating possibility. Use right:0 if you need to have it absolutely positioned, but also set the parent div to position: relative for it to work.


1 Answers

Multiple answers valid. My approach:

$('#div1').click(function() {    
     $(this).find('span').first().attr('class','newClassName');
});
like image 96
Khôi Avatar answered Sep 18 '22 20:09

Khôi