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

$('#div1').click(function() {    
   $('span:first', this).prop('class', 'newClassName');
})

http://api.jquery.com/first-selector/

http://jsfiddle.net/BUBb9/1/


$('#div1').click(function(){
    var v = $(this).find('span').text();
});

Like this you can get the value of span.

You can also use .html() instead of .text() method.


use

 $("div span:first-child")

as follow:

$('#div1').click(function() {    
   $('span:first', this).prop('class', 'ClassName');
})

refer http://api.jquery.com/first-child-selector/

Tags:

Jquery