Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tilde in jQuery selector

My understanding of the tilde's function in Javascript is that it performs a bitwise not operation (i.e. 1 becomes 0 and vice versa; 1000 becomes 0111). However, I've recently begun work on an existing project where my predecessor has included a lot of code like this:

var iValuation = $('div[class~="iValuation"]');

Can anyone tell me what the purpose of the tilde in this instance is? I've not come across it before and haven't been able to find any reference to it online.

like image 218
Dorian Fabre Avatar asked Feb 18 '13 16:02

Dorian Fabre


People also ask

What is tilde in jQuery?

It appears in a string. Since that string is passed to the jQuery function, and it doesn't look like a piece of HTML, it is a selector.

What is use of tilde in CSS?

In CSS, the symbol tilde(~) is know as Subsequent-sibling Combinator (also known as tilde or squiggle or twiddle or general-sibling selector). As the name suggests it is made of the “tilde” (U+007E, ~) character that separates two sequences of simple selectors.

What does tilde mean in sass?

Description of the tilde selector In CSS, the tilde symbol is known as subsequent-sibling combinator, which separates two compound selectors. The elements that are represented by the two compound selectors have the same parent element.


2 Answers

Tiled used as selector means

Selects elements that have the specified attribute with a value containing a given word, delimited by spaces.

which is not a JavaScript operator at all.

More from doc:

This selector matches the test string against each word in the attribute value, where a "word" is defined as a string delimited by whitespace. The selector matches if the test string is exactly equal to any of the words.

For example:

<input name="man-news" />
<input name="milk man" />
<input name="letterman2" />
<input name="newmilk" />

$('input[name~="man"]') will select only second input, because its attribute name is separated by space.

For detail see here

like image 102
thecodeparadox Avatar answered Oct 09 '22 06:10

thecodeparadox


That isn't a JavaScript operator. It appears in a string.

Since that string is passed to the jQuery function, and it doesn't look like a piece of HTML, it is a selector.

Specifically one of the attribute selectors:

Represents an element with the att attribute whose value is a whitespace-separated list of words, one of which is exactly "val". If "val" contains whitespace, it will never represent anything (since the words are separated by spaces). Also if "val" is the empty string, it will never represent anything.

like image 39
Quentin Avatar answered Oct 09 '22 06:10

Quentin