jquery .find to get the text in a <li>

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-06-01T17:38:38Z Indexed on 2010/06/01 17:43 UTC
Read the original article Hit count: 174

Filed under:
|
|

So I have a series of 2 nested ul's. When you click on the text (which is in an a tag) in the li, my page makes that editable and adds a save button. clicking the save button needs to give me back the new text inside that li and the id of that li. The id is not a problem. I'm trying to use jQuery's .find to select that anchor tag (which is successful) but i can't seem to get the text from it.

Here is an example of the first list and it's sublists.

<ul class='lists'>
  <li class='list1'>
    <a class='a list' id='list1'> List 1 Name</a>
    <img id='savelist1id' onClick="SaveName(this.parentNode.id)" src='save.jpg'>
    <ul class='list1subs'>
      <li class='sub1'>
        <a class='a sub' id='sub1id'> Sub 1 Name</a>
        <img id='savesub1id' onClick="SaveName(this.parentNode.id)" src='save.jpg'>
      </li>
      <li class='sub3'>
        <a class='a sub' id='sub2id'> Sub 2 Name</a>
        <img id='savesub2id' onClick="SaveName(this.parentNode.id)" src='save.jpg'>
      </li>
      <li class='sub2'>
        <a class='a sub' id='sub3id'> Sub 3 Name</a>
        <img id='savesub3id' onClick="SaveName(this.parentNode.id)" src='save.jpg'>
      </li>
    </ul>
  </li>
</ul>

Here's the code for identifying which save button you clicked.

function SaveName(parentid){
  $('li').find('a').each(function(){
    if (this.id == parentid){
      alert(this.id+' '+this.text)
      }
  }
});

I am wanting this.text to show me the text inside the anchor tags. Help, please?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery