0

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} ?

4
  • 2
    html method as a setter returns an object which is a truthy value, so ... . Is that supposed to be jQuery(".lc-" + post_id).html() === response.like? Commented Dec 31, 2013 at 15:08
  • Describe the case in which you want jQuery(".lc-" + post_id).html(response.like) to cause an alert negative. Commented Dec 31, 2013 at 15:09
  • jQuery.fn.html returns the jQuery collection which evaluates to true, so your condition is always true. What exactly did you want your if to check for? Commented Dec 31, 2013 at 15:09
  • Theres two buttons... One for like a post the other to dislike... i want to check if user click on response.like or response.unlike and do my code for each one Commented Dec 31, 2013 at 15:14

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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) { ... 

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.