Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find element's attribute value

Tags:

jquery

I have got the following code :

  <ul>
    <li class="jstree-leaf" kids="0" range="5-7" name="mars" public_id="mars_05" ra_depth="5">
        <a href="#"><ins class="jstree-icon">&nbsp;</ins>mars</a>
    </li>
    <li class="jstree-leaf" kids="0" range="8-10" name="pluto" public_id="pluto_8" ra_depth="5">
        <a href="#"><ins class="jstree-icon">&nbsp;</ins>pluto</a>
    </li>
  </ul>

I need to get the ra_depth attribute value of a particular <li> by using it's name attribute value.

I tried the code below but it's not working:

alert(li[name='"+myarray[0]+"'].attr("ra_depth"));

myarray[0] contains the value "pluto".

Is there anything I am missing?

like image 829
tanya Avatar asked Jan 27 '12 11:01

tanya


3 Answers

Description

You forget $( in your selector. Check out my sample and this jsFiddle Demonstration

Sample

 alert($("li[name='"+myarray[0]+"']").attr("ra_depth"));
like image 60
dknaack Avatar answered Oct 19 '22 04:10

dknaack


alert($('li[name="'+myarray[0]+'"]').attr("ra_depth"));
like image 44
Supr Avatar answered Oct 19 '22 02:10

Supr


You are missing the actually jQuery function around your selector:

alert($("li[name='"+myarray[0]+"']").attr("ra_depth"));
like image 26
Richard Dalton Avatar answered Oct 19 '22 02:10

Richard Dalton