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

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..    
});  

Best Answer

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

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

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