jquery - How do I get the click function to work on first click -


i have script. works great if topic liked, if has never been marked liked, need double click show user has liked. how work first time. continuation old conversation had gotten great on here old conversation

$(document).ready(function(){     $("#like<? echo $msgid;?>").click(function(){                                               var islike = $(this).text() === "like",         url = islike ? "status-updates/like.php?status_id=<? echo $msgid;?>&user=<? echo $session->username;?>" : "status-updates/unlike.php?status_id=<? echo $msgid;?>&user=<? echo $session->username;?>";     $.post(url + "?status_id=<? echo $msgid;?>&user=<? echo $session->username;?>", $(this).serialize());     settimeout(function () {         $("#likediv<? echo $msgid;?>").load('status-updates/like-count.php?status_id=<? echo $msgid;?>');         $(".wholikes<? echo $msgid;?>").load('status-updates/who-likes.php?status_id=<? echo $msgid;?>');         $("#like<? echo $msgid;?>").text(islike ? "unlike" : "like");     }, 500);                                    });      }); 

i suspect problem may come down these lines:

var islike = $(this).text() === "like"; url = islike ? "status-updates/like.php?status_id=<? echo $msgid;?>&user=<? echo $session->username;?>" : "status-updates/unlike.php?status_id=<? echo $msgid;?>&user=<? echo $session->username;?>"; 

or, in non-very-long-scrolling-way,

var islike = $(this).text() === "like"; url = islike ? <like url> : <unlike url>; 

in other words, when text says "like", url being posted 1 "like".

incidentally, there neater way perform delay you're invoking settimeout, seems designed wait half second - sufficient, you're hoping, post complete - before running. instead, can run code once post has completed using third parameter of $.post:

$.post( <url>, <data>,          function () {           $("#likediv<? echo $msgid;?>").load('status-updates/like-count.php?status_id=<? echo $msgid;?>');           $(".wholikes<? echo $msgid;?>").load('status-updates/who-likes.php?status_id=<? echo $msgid;?>');           $("#like<? echo $msgid;?>").text(islike ? "unlike" : "like");         } ); 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

IIS->Tomcat Redirect: multiple worker with default -