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>?
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).
Use the :not() CSS selector.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With