javascript - Use jQuery to make div value go up or down where $(this) is used -


i have pretty script allows user "thumb up" comment. script uses jquery make "thumb up" image green when user clicks it. user can also"un-thumb up" comment clicking image again, , image go green white. lastly, there little div displays number of people have "thumbed up" particular comment.

this question comes in. of right now, little div total number of thumb-ups comment not refreshed when user clicks or un-clicks "thumb-up." number changes if user refreshes page. div automatically go 1 integer if user thumbing comment (i.e. making image turn green) , go down 1 integer if user un-thumbing (i.e. turning image go green white).

the "little div" total number of thumb-ups called expressed (you can see full use in second block of code):

<div class='upvote_count'>$total_upvotes</div> 

i having trouble because jquery uses "$(this)" , don't know how refer element... ideas on how make happen helpful.

my jquery:

<script> $(document).ready(function() {  $('.thumb a').on('click', function(e) { e.preventdefault(); var $img = $(this).find('img'); var $idshow = $(this).attr("id");  if($img.hasclass('thumbsup')) {      $img.removeclass('thumbsup'); } else {      $img.addclass('thumbsup'); }      $img.attr('src', function(_,s) {          return s.indexof('thumbs_up_green') > -1 ? '../images/thumbs_up.png'                                                   : '../images/thumbs_up_green.png'                                                });      $.get("thumb_up_tip.php?tip_id=" + $idshow + "&x=" + "<?php echo "1"; ?>")        if($img.hasclass('thumbsup')) {          alert("up");         //this want <div class='upvote_count'>$total_upvotes</div> go 1     } else {         alert("down");         //this want <div class='upvote_count'>$total_upvotes</div> go down 1     }  });  }); </script> 

and here html:

echo " <table width='100%' class='box'> <tr> <td><span class='item'>$p[first_name] </span><span class='price'> ($tip_city_store$p[date_time])</span></td> <td align='center' valign='top'><div class='upvote_count'>$total_upvotes</div></td> </tr> <tr> <td width='87%' valign='top'> <span class='desc'>$p[tip]</span> </td> <td width='13%' align='center' valign='top'> <span class='thumb'> <a href='#' id = '$p[tip_id]' onclick='thumbtip(this)'>";  if ($upvote_count=="0") {     echo "<img src='../images/thumbs_up.png' width='21' scalefit='1'>"; }  if ($upvote_count=="1") {     echo "<img src='../images/thumbs_up_green.png' width='21' scalefit='1'      class='thumbsup'>"; }  echo "</a> </span> </td> </tr </table>"; 

firstly, can use combination of closest , find .upvote_count element relative clicked button.

secondly, logic little flawed , needed drying up. try this:

$('.thumb a').on('click', function(e) {     e.preventdefault();     var $img = $(this).find('img');     var idshow = this.id;     var $counter = $(this).closest('table').find('.upvote_count');      if ($img.hasclass('thumbsup')) {         $img             .removeclass('thumbsup')             .attr('src', '../images/thumbs_up.png');         $counter.text(parseint($counter.text(), 10) - 1);     }      else {          $img             .addclass('thumbsup')             .attr('src', '../images/thumbs_up_green.png');         $counter.text(parseint($counter.text(), 10) + 1);     }      $.get("thumb_up_tip.php?tip_id=" + idshow + "&x=<?php echo "1"; ?>"); }); 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -