Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
    
  • like image 871
    Bhawna Malhotra Avatar asked Sep 27 '22 23:09

    Bhawna Malhotra


    2 Answers

    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/

    like image 150
    Cláudio Barreira Avatar answered Oct 03 '22 20:10

    Cláudio Barreira


    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/

    like image 30
    Ash Avatar answered Oct 03 '22 22:10

    Ash