If and else does not work in request

I tried the if else statement in jquery but it doesn't work. Here is my code.

jQuery(document).ready(function () {
        jQuery(".checkboxes").hide();
        //toggle the componenet with class msg_body
        jQuery(".Select").click(function () {
            jQuery(this).next(".checkboxes").slideToggle(500);
            if ($(".select").text() == "+") {
                alert('hi');
                $(".select").html() == "-"
            }
            else {
                $(".select").text() == "+";
            }
            return false;
        });
    });

      

any help would be appreciated.

+3


source to share


4 answers


I'm guessing it's a typo .. Put capital "S" instead of "s" like in a click event.

try under code: -

if ($(".Select").text() == "+") {
                alert('hi');
                $(".Select").html("-");
            }
            else {
                $(".Select").text() == "+";
            }

      



or just use $ (this) as shown below: -

 if ($(this).text().trim() == "+") {
            $(this).html("-");
        } else {
            $(this).text("+");
        }

      

0


source


You cannot assign a value to a function call. Your code should throw an error likeUncaught ReferenceError: Invalid left-hand side in assignment



jQuery(function ($) {
    $(".checkboxes").hide();
    //toggle the componenet with class msg_body
    $(".Select").click(function () {
        $(this).next(".checkboxes").slideToggle(500);
        $(this).text(function (i, txt) {
            return txt.trim() == '+' ? '-' : '+'
        })
        return false;
    });
});

      

0


source


Have a look at this fiddle : JsFiddle You should change your code to this by assigning it$(".Select").html("-");

jQuery(document).ready(function () {
        jQuery(".checkboxes").hide();
        //toggle the componenet with class msg_body
        jQuery(".Select").click(function () {
            jQuery(this).next(".checkboxes").slideToggle(500);
            if ($(".Select").text() == "+") {
                alert('hi');
                $(".Select").html("-");
            }
            else {
                $(".Select").text("+");
            }
            return false;
        });
    });

      

0


source


First of all, you are using two different classes .Select

and .Select

. I think the problem is here. And second, it must be html('-')

not html() == '-'

because it is a boolean / conditional statement and it returns a boolean value. It is not an assignment operator. Therefore, your code should be:

jQuery(document).ready(function () {
    jQuery(".checkboxes").hide();
    //toggle the componenet with class msg_body
    jQuery(".select").click(function () {
        jQuery(this).next(".checkboxes").slideToggle(500);
        if ($(".select").text() == "+") {
            alert('hi');
            $(".select").html("-");
        }
        else {
            $(".select").text("+");
        }
        return false;
    });
});

      

I suppose this is the correct class select

as you have used more than select

.

0


source







All Articles