1

in following code rank variable not updating after click event. the click event also contains data attr change anybody may point out problem data attr change is in ajax update success function.

//php

<div class="logo_rank" data-id="<?php the_ID(); ?>" data-rank="<?php echo $rank_num; ?>">
                    <?php
$disable_star = 5 - $rank_num;
                    for ($i = 0; $i < $rank_num; $i++) {
                        echo '<i class="fa fa-star" id="star' . $i + 1 . '" style="color:#0170B9;" aria-hidden="true"></i>';
                    }
                    for ($i = 0; $i < $disable_star; $i++) {
                        echo '<i class="fa fa-star" id="star' . $i + $rank_num + 1 . '" style="color:#d3d3d3;" aria-hidden="true"></i>';
                    }

//java
$(".fa-star").click(function(e) {
            var id = $(e.target).parents(".logo_rank").data('id');
            var rank = $(e.target).parents(".logo_rank").data('rank');
            
            var starclicked = $(this).index() + 1;
  • java != javascript – freedomn-m Aug 11 '21 at 09:00
  • 2
    The only relevant part is how you *set* the value *"in ajax update success"* isn't "how". Don't mix `.data("rank")` with `.attr("data-rank")` - if at any point you use `.data("rank")` then always use `.data("rank")` - but you've not included this so just speculation that you're either a) doing `.data("rank", newrank)` then inspecting and not seeing it or b) doing `.attr("data-rank", newrank)` then `.data("rank")` and not seeing the new value - because `.data("rank")` is already cached – freedomn-m Aug 11 '21 at 09:03
  • thanks for your reply in ajax success i am using this what is the right code? $(e.target).parents(".logo_rank").attr('data-rank', newRank); – Niraj Singh Aug 11 '21 at 10:28
  • 2
    Yep - looks like you're missing `.attr("data-rank", newRank)` with retrieving it via .data - use: `.data("rank", newRank)` – freedomn-m Aug 11 '21 at 11:08
  • hi thank you, now working perfectly – Niraj Singh Aug 11 '21 at 11:19

0 Answers0