Alex Baldwin Alex Baldwin - 3 months ago 9
HTML Question

Get two attributes using Jquery

i've a problem using Jquery.

This is my html markup:

<li><a class="" value="short" tool="car" type="monov"> text </a></li>


I have to capture, after selecting an LI, "ttol" and "type" attr.

This is my code:

var selectedTool = $(this).attr('tool');
var selectedType = $(this).attr('type');


But it doesn't work, it arrives undefined...But if i use this:

$(this).html();


i get:

<li><a class="" value="short" tool="car" type="monov"> text </a></li>


How can i get these attributes?

Thanks

Answer

try below code :

var selectedTool = $(this).find("a").attr('tool');
var selectedType = $(this).find("a").attr('type');

you were trying to access the attributes of li and the attributes 'tool' and 'type' are on anchor so it was coming undefined.