Get title attribute using jQuery

I have a jQuery Mobile menu for accordion configured as:

<div class="subCat" data-role="collapsible"> <h3 title="someTitle">Subcategory Name</h3> <div class="itemImg"> <img alt="Item 1" src="[image location]" /><span>Item 1</span> </div> <div class="itemImg"> <img alt="Item 1" src="[image location]" /><span>Item 1</span> </div> </div><!--End of subCat--> 

What happens for several subcategories. I have some code to get the subcategory name and title attribute from the image when clicked.

 var currCat=$(this).closest('.subCat').children('h3').text(); var titleData=$(this).closest('.subcat').children('h3').attr("title"); 

"this" is the image that clicked. currCat gets the desired string that it needs, but I always get "undefined" for titleData. Do not pretend that it will go wrong how to get the title.

+7
source share
1 answer

The second line has subcat instead of subcat . The following should work:

 $(this).closest('.subCat').children('h3').attr("title"); 

Thanks for reminding me that class names are case sensitive in selectors. Easy to miss.

+20
source

All Articles