Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 375
X10nD Avatar asked Apr 28 '10 08:04

X10nD


People also ask

How do you put not equal to in jQuery?

The opposite of the == compare operator is != .

What does != Mean in jQuery?

The inequality operator ( != ) checks whether its two operands are not equal, returning a Boolean result. Unlike the strict inequality operator, it attempts to convert and compare operands that are of different types.

What does $() mean in JavaScript?

Usually when you encounter $() , that means the developer is using a javascript library, such as jQuery. The $ symbol is the namespace for those libraries. All the functions they define begin with $. , such as $. get() .

Why does JavaScript use === instead of ==?

Use === if you want to compare couple of things in JavaScript, it's called strict equality, it means this will return true if only both type and value are the same, so there wouldn't be any unwanted type correction for you, if you using == , you basically don't care about the type and in many cases you could face ...


2 Answers

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.

like image 143
Deniz Dogan Avatar answered Oct 05 '22 22:10

Deniz Dogan


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

like image 27
Francisco Soto Avatar answered Oct 05 '22 23:10

Francisco Soto