Warm tip: This article is reproduced from serverfault.com, please click

jQuery to get different value from on click class

发布于 2020-11-28 06:57:53

I tried to get value from class have defined values

<input type="checkbox" class="cat" value="3">
<input type="checkbox" class="cat" value="5">
<input type="checkbox" class="cat" value="8">

and my jquery code

$('.cat').click(function(){
          var cat = Number($('.cat').val());
        $(".cat").val(cat);
 // after that using my code

}
Questioner
v2mpire 20202
Viewed
0
Sina Riani 2020-11-28 15:49:07

Remove $(".cat").val(cat); and use this instead of '.cat' in selector

$('.cat').click(function(){
            var cat = Number($(this).val());
            // after that using my code
    });

$(function(){
            $('.cat').click(function(){
                        var cat = Number($(this).val());
                console.log("cat value: " + cat);
                        // after that using my code
            });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<input type="checkbox" class="cat" value="3">
<input type="checkbox" class="cat" value="5">
<input type="checkbox" class="cat" value="8">