Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery : Changing class of button with AJAX call
    primarykey
    data
    text
    <p>I am building up a Like/Unlike system, I have a button what has a class like, if i click it data is inserted in the database, and the class is changed to unlike.</p> <p>And unlike suppose to pull another ajax call what removes the actual like but its not working, when the class changed it preforms what like class suppose to do, and it only works if i refresh the page.</p> <p>php code</p> <pre><code>&lt;div class="btn-group pull-right"&gt; &lt;?php $like = '&lt;button class="btn btn-primary btn-small like" data-like="'.$user-&gt;id.'"&gt; &lt;i class="icon-thumbs-up icon-white"&gt;&lt;/i&gt; Like &lt;/button&gt;'; foreach ($user-&gt;likes as $likes) { if($likes['liked_by'] == Session::get('sentry_user')) { $like = '&lt;button class="btn btn-primary btn-small unlike" data-like="'.$user-&gt;id.'"&gt; &lt;i class="icon-thumbs-down icon-white"&gt;&lt;/i&gt; Unlike &lt;/button&gt;'; break 1; } } echo $like; ?&gt; &lt;/div&gt; </code></pre> <p>jquery</p> <pre><code>$('button.like').bind('click', function(){ var likeId = $(this).data('like'); $.ajax({ url: siteUrl + 'profile/like', type: "post", data: {user_id: likeId}, dataType: "json", context: this, beforeSend: function() { $(this).addClass('disabled'); }, success: function(data) { if(data.status == "like") { $(this).removeClass('like') .addClass('unlike') .append() .html('&lt;i class="icon-thumbs-down icon-white"&gt;&lt;/i&gt; Unlike'); } }, complete: function() { $(this).removeClass('disabled'); } }); }); $('button.unlike').bind('click', function(){ var likeId = $(this).data('like'); alert('you are about to unlike'); }) </code></pre> <p>just made the unlike alert for an example to test it befoe I make the ajax call with it.</p> <p>So cloud someone give me a hint?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload