X10nD
X10nD

Reputation: 22030

Not Equal To notation in Javascript to use in jQuery

Is this the notation to use for Not Equal To in JS, in jquery code

!== OR != 

None of them work

Here is the code I am using

var val = $('#xxx').val();
if (val!='') {
 alert("jello");
}

Thanks Jean

Upvotes: 7

Views: 30406

Answers (5)

YOU
YOU

Reputation: 123831

May be you have whitespace in your #xxx node, that why both !== and != failing, you could try following to test non whitespace characters

var val = $('#xxx').val();

if (/\S/.test(val)){
    alert('jello');
}

Note: I assume jQuery's .val() won't return null because of this line in jQuery source

return (elem.value || "").replace(/\r/g, "");

If not, you need to do like this

if (val && /\S/.test(val)){
    alert('jello');
}

Upvotes: 1

Krunal
Krunal

Reputation: 3541

It is working with jquery and normal java script.

You should check (alert/debug) your val variable for its value and null.

You should also check $('#xxx').length whether you are getting elements or not otherwise you will get, hence your if condition will be false.

Upvotes: 0

Francisco Soto
Francisco Soto

Reputation: 10392

.val() is used to retrieve or set values from input in forms mostly, is that what you want to do? If not maybe you mean using .text() or .html().

If that is indeed what you want to do maybe you have your selector wrong and its returning null to you, and null does not equal '', or maybe you actually have data there, like whitespaces. :)

Upvotes: 2

Deniz Dogan
Deniz Dogan

Reputation: 26227

Equality testing in JQuery works no different from "standard" JavaScript.

!= means "not equal to", but !== makes sure that both values have the same type as well. As an example, 1 == '1' is true but not 1 === '1', because the LHS value is a number and the RHS value is a string.

Given your example, we cannot really tell you a lot about what is going on. We need a real example.

Upvotes: 19

Lloyd
Lloyd

Reputation: 29668

It's both, but the latter is strict on type, see here:

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Comparison_Operators

Upvotes: 0

Related Questions