How to traverse dom tree from a list of elements

sajid

I have HTML snippet which looks like this. I generate this snippet multiple times form the backend. When I click the Save button, I catch which Save button was clicked using $(this) selector. Now I want to grab the attribute item-id of the corresponding Save button. I have the following jquery code snippet. But it does not work. I have tried to look but I don't know where the error is.

<td><input type="text" size="10" value="val1" item-id="id1"></td>
<td><input type="text" value="val2" size="4"></td> 
<td>
  <button class="btn btn-primary save-btn">Save</i></button>
</td>

Here is the jquery snippet

 $(".save-btn").click(function(){
        var ems = $(this).parent().siblings();
       var item_id = ems[0].child().attr("item-id");
   }
rahulparekh

click doesn't work on dynamically added elements.You need to use on('click'). Also there is no method .child() so you need to use .children().first().

This is the corrected code:

$(document).on('click', '.save-btn', function(){
   var ems = $(this).parent().siblings();
   var item_id = ems.children().first().attr("item-id");
});

// The text
var text="";
text += "<td><input type=\"text\" size=\"10\" value=\"val1\" item-id=\"id1\"><\/td>";
text += "<td><input type=\"text\" value=\"val2\" size=\"4\"><\/td> ";
text += "<td>";
text += "  <button class=\"btn btn-primary save-btn\">Save<\/i><\/button>";
text += "<\/td>";


// Adding the text to html
$('body').html(text);


$(document).on('click', '.save-btn', function(){
    var ems = $(this).parent().siblings();
    console.log(ems);
    var item_id = ems.children().first().attr("item-id");
    alert(item_id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to traverse dom tree from a list of elements

From Dev

How to traverse a nested list from deepest elements

From Dev

How to traverse DOM elements with jquery?

From Dev

How to remove div elements from the DOM tree one by one?

From Dev

How to traverse a tree from sklearn AgglomerativeClustering?

From Dev

How to traverse parse tree from instaparse

From Dev

Haskell - How to traverse through a list and reverse elements

From Dev

Iterate over and traverse DOM tree

From Dev

How to construct a tree of particular shape with elements from a list

From Dev

How to construct a tree of particular shape with elements from a list

From Dev

Jquery: How to traverse dom to slideToggle groups of tab elements

From Dev

how to traverse this tree?

From Dev

How to traverse a Huffman tree?

From Dev

Traverse the DOM with this Tree Traversal? help JQuery

From Dev

Remove elements from tree based on list of terms

From Dev

How to get the next tree elements of a list

From Dev

How to target a list of DOM elements within React

From Dev

How to Traverse an NLTK Tree object?

From Dev

Traverse tree recursively with id while adding to a list

From Dev

how to extract elements from tree.productions()

From Dev

how to extract elements from tree.productions()

From Dev

How to elegantly add and remove elements from DOM

From Dev

How do we traverse from nth to last element in a linked list?

From Dev

How to remove elements from a list?

From Dev

How to traverse a tree in Clojure while collecting the value from each node node?

From Dev

How to traverse through a dictionary of dictionaries and extract values that match list elements in python?

From Dev

Python lxml: How to traverse back up a tree

From Dev

How to Traverse a N-Ary Tree

From Dev

How to traverse recursively an HTML tree with Jsoup?

Related Related

  1. 1

    How to traverse dom tree from a list of elements

  2. 2

    How to traverse a nested list from deepest elements

  3. 3

    How to traverse DOM elements with jquery?

  4. 4

    How to remove div elements from the DOM tree one by one?

  5. 5

    How to traverse a tree from sklearn AgglomerativeClustering?

  6. 6

    How to traverse parse tree from instaparse

  7. 7

    Haskell - How to traverse through a list and reverse elements

  8. 8

    Iterate over and traverse DOM tree

  9. 9

    How to construct a tree of particular shape with elements from a list

  10. 10

    How to construct a tree of particular shape with elements from a list

  11. 11

    Jquery: How to traverse dom to slideToggle groups of tab elements

  12. 12

    how to traverse this tree?

  13. 13

    How to traverse a Huffman tree?

  14. 14

    Traverse the DOM with this Tree Traversal? help JQuery

  15. 15

    Remove elements from tree based on list of terms

  16. 16

    How to get the next tree elements of a list

  17. 17

    How to target a list of DOM elements within React

  18. 18

    How to Traverse an NLTK Tree object?

  19. 19

    Traverse tree recursively with id while adding to a list

  20. 20

    how to extract elements from tree.productions()

  21. 21

    how to extract elements from tree.productions()

  22. 22

    How to elegantly add and remove elements from DOM

  23. 23

    How do we traverse from nth to last element in a linked list?

  24. 24

    How to remove elements from a list?

  25. 25

    How to traverse a tree in Clojure while collecting the value from each node node?

  26. 26

    How to traverse through a dictionary of dictionaries and extract values that match list elements in python?

  27. 27

    Python lxml: How to traverse back up a tree

  28. 28

    How to Traverse a N-Ary Tree

  29. 29

    How to traverse recursively an HTML tree with Jsoup?

HotTag

Archive