James Wilson
James Wilson

Reputation: 5150

Jquery If structure not working as expected

$('#myClick2').click(function () {
                var value = $(this).val();
                If (value != 'on') {
                    $.getJSON('message_center_getMessageLists.asp', {}, function (json) {
                        alert(json.one);
                        alert(json.two);
                        alert(json.three);
                    })
                    .error(function () { alert("There was an error while trying to make this request;  If it persists please contact support"); });
                }
            });

The $.getJSON part was working just fine until I wrapped the IF statement around it. I am trying to get the check box to execute the .getJSON only if it is not currently checked. Anyone have any idea what I am doing incorrectly?

Upvotes: 0

Views: 52

Answers (3)

brenjt
brenjt

Reputation: 16297

You if statement is capitalized. Change it to lowercase and that should fix your problem.

Also are you sure that the value is in fact not equal to on?

UPDATE

Then what you want to do is use this.checked in the if statement.

Upvotes: 2

user1106925
user1106925

Reputation:

"...execute the .getJSON only if it is not currently checked"

Then do it like this...

if (!this.checked) {
    $.getJSON(...

Upvotes: 4

SLaks
SLaks

Reputation: 887315

.val() returns the value of the value attribute.

You want if (this.checked) (no jQuery) or if ($(this).is(':checked'))

Upvotes: 1

Related Questions