javascript - jquery click not chaning text/numbers correctly with if conditions -



javascript - jquery click not chaning text/numbers correctly with if conditions -

i'm writing button jquery have problems conditionals.

other problem have total_likes counter: if click , click unlike counter set 0 if 1 time again te counter goes 2 instead of 1...

the php/html:

if (total_likes > 0) { $hide = 'block'; } $hide = 'none'; $photos_box .= '<input type="hidden" class="tl' . $imgid . '" value="' . $total_likes . '" /> <a id="' . $imgid . '" class="like_button" title="' . $like_title . '">' . $like . '</a> //the counter <a id="" style="display: '. $hide . '"> <span id="color" class="mod' . $imgid . '">' . $total_likes . '</span> </a> // text <li id="counter" style="display: ' . $hide . ';"> <div id=""> <span class="text_color">' . $text . '</span> </div> </li>

the jquery script:

$('.like_button').click(function() { var total_likes = $('.tl'+this.id).val(); total_likes = parseint(total_likes); var status = ''; if ($(this).html() == 'like') { status = 'like'; $(this).html('unlike'); $(this).attr('title', 'unlike this'); $('.mod'+this.id).html(total_likes+1); if (total_likes == 0) { $('#counter').slidetoggle('fast'); $('.text_color').html('like this.'); } else if (total_likes == 1) { $('.text_color').html('you , <a id="a_112">other</a> this.'); } else if (total_likes > 2) { var tl = total_likes+1; $('.text_color').html('you , <a id="a_112">'+tl+' others</a> this.'); } } else if ($(this).html() == 'unlike') { status = 'unlike'; $(this).html('like'); $(this).attr('title', 'like this'); $('.mod'+this.id).html(total_likes-1); if (total_likes == 0 || total_likes == 1) { $('#counter').slidetoggle('fast'); } else if (total_likes == 2) { $('.text_color').html('<a id="a_112">1 person</a> this.'); } else if (total_likes > 2) { var tl = total_likes-1; $('.text_color').html('<a id="a_112">'+tl+' people</a> this.'); } } var info = { img_id : this.id, sta : status }; $.ajax({ type : 'post', url : '/includes/like.php', info : info }).done(function(result) { console.log(result); }); });

working jquery , real time changes mess... i'm not sure if conditionals right needs.

i don't know why like_counter-1 doing -2 , like_counter+1 doing +2...

what i'm doing wrong?

you don't need print 2 times $total_likes... utilize actual counter:

var total_likes = $('.mod'+this.id).html();

javascript php jquery

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -