Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jQuery to do "does contain"

Tags:

jquery

I have an array of strings, I want to use jQuery to see if a particular string is contained within the array?

Can i do this with jQuery>?

like image 340
CLiown Avatar asked Jun 08 '10 13:06

CLiown


People also ask

What does has () do in jQuery?

jQuery has() Method The has() method returns all elements that have one or more elements inside of them, that matches the specified selector. Tip: To select elements that have multiple elements inside of them, use comma (see example below).

How do you check does not contain jQuery?

Use the :not() CSS selector.

What is $() in jQuery?

In the first formulation listed above, jQuery() — which can also be written as $() — searches through the DOM for any elements that match the provided selector and creates a new jQuery object that references these elements: 1. $( "div.

How can you tell if UL has Li in a text?

The text inside the returned element is checked for the innerText property to see if it matches the text required. A successful match means that the text inside the selected unordered list has the required text. Check if ul has li with a specific text in jQuery.


1 Answers

You can use $.inArray() and check that the result is not == -1, like this:

var arr = [ "string1", "string2" ];
jQuery.inArray("string1", arr) // returns 0
jQuery.inArray("string2", arr) // returns 1
jQuery.inArray("string3", arr) // returns -1

And for the flame wars about "why use jquery?" here...it's because older IE (and maybe current IIRC) doesn't have the Array.indexOf function, $.indexOf() will use the built-in Array.indexOf is it's present, it's just a wrapper to take care of IE not having this.

Alternatively, you can add the Array.indexOf method if it's not present, bobince shows how to do that here.

like image 141
Nick Craver Avatar answered Sep 22 '22 03:09

Nick Craver