7

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

X10nD
  • 19,972
  • 44
  • 105
  • 150
  • jQuery code is Javascript. There is no difference. – nickf Apr 28 '10 at 08:57
  • See the comparison table in this question http://stackoverflow.com/questions/80646/ It is for PHP, but mostly applies to Javascript too. – nickf Apr 28 '10 at 09:01

5 Answers5

19

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.

Deniz Dogan
  • 23,833
  • 33
  • 104
  • 154
  • I was just wondering what possible value there is in checking the type when doing not equal, anyone know? – Darren Oct 30 '13 at 11:05
2

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

Francisco Soto
  • 9,917
  • 2
  • 34
  • 46
1

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');
}
YOU
  • 106,832
  • 29
  • 175
  • 207
0

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

Lloyd
  • 27,966
  • 4
  • 78
  • 91
0

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.

Krunal
  • 3,283
  • 3
  • 19
  • 27