Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Find element with specific data attribute [duplicate]

What function can I use to select child div of class="rate_stars".

I have a variable range from 1 - 5 and I want to select child div with that specific data-rating.

I have tried

 $('rate_widget').find().attr('data-rating', info).prevAll().andSelf().addClass('ratings_vote');

But it did't work.

<div id="r1" class="rate_stars"
                             data-post-id="<?php echo $post_id ?>"
                             data-user-id="<?php echo $user_id;  ?>"
                             data-nonce="<?php echo $nonce ?>"
                            >
    <div data-rating="1" class="ratings_stars"></div>
    <div data-rating="2" class="ratings_stars"></div>
    <div data-rating="3" class="ratings_stars"></div>
    <div data-rating="4" class="ratings_stars"></div>
    <div data-rating="5" class="ratings_stars"></div>
    <div class="total_votes">vote data</div>
</div>
like image 458
Epistemologist Avatar asked Dec 19 '22 13:12

Epistemologist


1 Answers

You can use the attribute selector found in jQuery:

$('#r1').find('[data-rating="' + info + '"]')

Reference: List of All jQuery Attribute Selectors

like image 147
War10ck Avatar answered Mar 05 '23 04:03

War10ck