Tilde in jQuery selector

My understanding of the tilde function in Javascript is that it performs a bitwise action (i.e. 1 becomes 0 and vice versa 1000 becomes 0111). However, I recently started working on an existing project in which my predecessor included a lot of code like this:

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

      

Can anyone tell me what is the purpose of the tilde in this case? I have not come across this before and have not been able to find a link to it on the internet.

+3


source to share


4 answers


This is not a JavaScript statement. It is displayed in line.

Since this string is being passed to the jQuery function and doesn't look like a chunk of HTML, it's a selector .



Specifically, one of the attribute selectors :

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

+2


source


Tile used as a selector

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

which is not a JavaScript operator at all.

More from doc:

This selector matches a test string for each word in the attribute value, where "word" is defined as a whitespace-delimited string. 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 only select the second input

one because its attribute name

is split space

.

See here for details

+8


source


$

is a selector function jQuery

that contains the string CSS3

Selector: According to the Selector CSS3

Definition, the selector
you selected selects:

E [foo ~ = "bar"] element E, attribute value "foo" is a space-separated list of values, one of which is exactly "bar"

in DOM

. Since the tilde is wrapped in a string, it doesn't work as an operator.

+2


source


If you are curious about the difference between

[class~="foo"]

      

and

[class*="foo"]

      

~

will only match with spaces (eg "foo bar" but not "foo-1") will match ours without spaces (eg "foo bar" and "foo-1")
*

~

- Selector with attribute exploding
*

- Attribute contains a selector

0


source







All Articles