简体   繁体   中英

Get Values of sub elements of clicked class using jquery

I have a class amt and when that class is clicked I want to get the values of the clicked <h6> , <span> and <label> tags. How do I do this in jquery? I have already seen a question here Get value of List Item with jQuery but it uses same

  • under tag but i have to get different elemet value under same tag

     <li class="amt" id="diecut_am1"> <h6>50</h6> <span>$59.00</span> <label>$51.30</label> </li> <li class="amt" id="diecut_am2"> <h6>100</h6> <span>$68.00</span> <label>$61.20</label> </li> 
  • Try this

    $(".amt").click(function() {
        var elem1 = $(this).find("h6").html();
        var elem2 = $(this).find("span").html();
        var elem3 = $(this).find("label").html();
    
        alert(elem1);
        alert(elem2);
        alert(elem3);
    });
    

    https://jsfiddle.net/kLe5kLc3/1/

    You could do something like this:

    $( document ).ready(function() {
        $('.amt').on("click", function() {
           var h6 = $(this).find('h6').text();
           var span = $(this).find('span').text();
           var label = $(this).find('label').text();
        });
    });
    

    Demo: https://jsfiddle.net/12q12k52/

    here's the JS way :

     var amt = document.querySelectorAll('.amt') //add event listener to all .amt elements var amtArr = [].slice.call(amt) amtArr.forEach(function (x) { x.addEventListener('click', listChilds, true) }); //we retrive the target properties function listChilds(e) { console.log(e.path[1]) //all the children //if you want one in particular it would be console.log(e.target.childNodes[0]) } 
     <li class="amt" id="diecut_am1"> <h6>50</h6> <span>$59.00</span> <label>$51.30</label> </li> <li class="amt" id="diecut_am2"> <h6>100</h6> <span>$68.00</span> <label>$61.20</label> </li> 

    您可以迭代单击元素的元素

    $(this).children()
    

    The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM