mmmttzz
mmmttzz

Reputation: 81

Get data value from .click by class

This code will output the right class name but not the right data-validamount. it will only output : undefined.

$(".prod_amount_add").click(function(){     
    var rowAmount = 0;
    var datavalid = $(this).attr('data-validamount');   
    console.log(datavalid);
    console.log($(this).attr('class'));
});

<div class="prod_amount_add">
    <img src="icons/add.png" width="16" height="16" 
         alt="Plus 1" data-validamount="1">   
</div>

Upvotes: 0

Views: 79

Answers (3)

Amit Kumar
Amit Kumar

Reputation: 5962

you need to find your img attr after click on div . as it is part of img not of div

$(".prod_amount_add").click(function(){

    var rowAmount = 0;  

    var datavalid = $(this).find('img').attr('data-validamount');   
    console.log(datavalid);
    console.log($(this).attr('class'));

    });

DEMO

Upvotes: 0

Silviu Burcea
Silviu Burcea

Reputation: 5348

Try $(this).find('img').data('validamount').

Upvotes: 0

Derek
Derek

Reputation: 4751

The attribute is on your img, not your .prod_amount_add div, so you'd do $(this).find('img').attr('data-validamount');

Upvotes: 2

Related Questions