Oscar Batlle
Oscar Batlle

Reputation: 157

How to add a conditional statement for JQUERY AJAX response function

This is my code :

jQuery(document).ready(function () {
    jQuery(".jlk").live("click", function (e) {
        e.preventDefault();
        var task = jQuery(this).attr("data-task");
        var post_id = jQuery(this).attr("data-post_id");
        var nonce = jQuery(this).attr("data-nonce");
        var currentClass = $(this).attr('class');

        jQuery(".status-" + post_id).html("  ").addClass("loading-img").show();

        jQuery.ajax({
            type: "post",
            dataType: "json",
            url: wtilp.ajax_url,
            data: { action: "wti_like_post_process_vote", task: task, post_id: post_id, nonce: nonce },
            success: function (response) {            
                jQuery(".lc-" + post_id).html(response.like);
                jQuery(".unlc-" + post_id).html(response.unlike);
                jQuery(".status-" + post_id).removeClass("loading-img").empty().html(response.msg);
            }
        });
    });

    // Other users tooltip
    jQuery("span.wti-others-like").live("mouseover", function () {
        jQuery(this).children("span").show();
    });

    jQuery("span.wti-others-like").live("mouseout", function () {
        jQuery(this).children("span").hide();
    });
});

And I'm trying to add the conditional statement like this :

if (jQuery(".lc-" + post_id).html(response.like)) {
                    $(".action-unlike-" + post_id + " img").removeClass('opacity-none'); $(".action-like-" + post_id + " img").addClass('opacity-none'); alert('positive');        
                }
                else if (jQuery(".unlc-" + post_id).html(response.unlike)) { $(".action-like-" + post_id + " img").removeClass('opacity-none'); $(".action-unlike-" + post_id + " img").addClass('opacity-none'); alert('negative'); }

But it always alert 'positive' like its not reading my conditions.

How can i accomplish this?

If (response.like == 'true') {do this} else if (response.unlike == 'true') {do this} ?

Upvotes: 0

Views: 413

Answers (3)

Ashish Kumar
Ashish Kumar

Reputation: 3039

You should actually check the HTML value like $(".lc-" + post_id).html() with the response.like instead of setting the value in it. You can replace the code with the below one:

if ($(".lc-" + post_id).html() == response.like) {
    $(".action-unlike-" + post_id + " img").removeClass('opacity-none');
    $(".action-like-" + post_id + " img").addClass('opacity-none');
    alert('positive');
} else if ($(".unlc-" + post_id).html() == response.unlike) {
    $(".action-like-" + post_id + " img").removeClass('opacity-none');
    $(".action-unlike-" + post_id + " img").addClass('opacity-none');
    alert('negative');
}

Good luck!

Upvotes: 0

Matt617
Matt617

Reputation: 458

the jQuery HTML Method always returns true since it is a method to set HTML when you pass in a value.

See: http://api.jquery.com/html/

If your HTML is an input string your looking for the "Val" Method.

See: http://api.jquery.com/val/

if you literally want to check the HTML you need to check the .html() with NO Values passed in. Then do a string or other comparison of that HTML.

Upvotes: 1

Rob Sedgwick
Rob Sedgwick

Reputation: 5226

H i,

The if statement is a declaration ( it is setting html, not getting html and checking )

  if (jQuery(".lc-" + post_id).html(response.like)) { ... 

do you mean to do :

 if (jQuery(".lc-" + post_id).html()===response.like) { ... 

Upvotes: 0

Related Questions