Jquery check contains element.
Jquery check contains element Use the querySelector() method to check if an element has a child with a specific id. Feb 13, 2024 · In jQuery, you can use the indexOf() method to check if a string contains a specific substring. It takes a substring as an argument and checks if it exists within the text of the selected element. Share Improve this answer. If you want to check if the element clicked is contained inside the given parent at any level, this is the correct code to use. length and a number comparison operator. This is mostly used together with another selector to select the elements containing the text in a group (like in the example above). Aug 28, 2023 · In jQuery, you can select an element using the $() function, which returns a jQuery object. target is a reference to the clicked element, you can check to see if #parent-node is event. 0. But what about the third click function. selectorText); Nov 3, 2015 · Using JavaScript or jQuery, I want to check through an array to make sure all items have a certain value. myCheckbox'). The text must have matching case to be selected. Here's a simple Apr 23, 2024 · The most basic concept of jQuery is to "select some elements and do something with them. contains function but that function is used to see if a DOM element is a descendant of another DOM element. I recommend you put in the "dataType" attribute the "json" value, that way if you get a JSON or not, JQuery validate it for you and make it known through their functions "success" and "error". contents(), then jQuery returns both elements and text nodes. hasData(element) is that it does not create and associate a data object with the element if none currently exists. Compare this selector with the Attribute Contains Word selector (e. So for example, if I am checking for "active" in the following arrays, I want: ["active"] Oct 18, 2016 · I'm trying to get a function to fire only if more than one element in a div contains a certain text. Check if each element contains a another particular element. About contains selector. Aug 17, 2018 · How can I create a function in which it validates whether a "span" element exists under a div class? Check if div exists outside span jQuery. var hasClass = document. 78, 67, 99, 56, 89. And I need to do that in a Nov 30, 2023 · Using contains() method. Check if li contains ul. length ); // 1or use this shortcut notation with descendant selector, which literally means "find this child node and see if it has this other element as parent": your code becomes shorter and more readable! Jul 31, 2013 · container is a jquery object and . contains() Method. prop('checked', true); $('. For Example if my code looks like this: <ul> <li><span&g Below function can be used to check for a value in any level in a JSON. Nov 19, 2008 · The question is "how to check whether or not a selector exists in jQuery. 32. While both our solutions work, mine is just a tad more semantically correct. For example – you are dynamically listing records from the MySQL database table here, you have a requirement to add a search filter based on keypress. inArray() and it acts kinda strange. isEmptyObject([]) returns false, but for jQuery 1. Execute script when the Array contains a particular value. contains. Syntax: $('#elementSelector'). . jQuery allows you to create custom selectors, but see here what happens when you try to use on e before initializing it; The . Oct 23, 2017 · Your text variable contains a CSS/jQuery style selector, not the actual string you want to search for. length > 0; } This will return a boolean value. some(key Feb 6, 2013 · The jQuery object does not have a contains method. Mar 11, 2025 · Yes, you can use a custom implementation of the contains() method to check the text content of HTML elements. Remember, OP doesn't want to check if it exists, he just wants to see if the bold tag contains text. contains() jQuery. this method could find out the container-contained relationship weather it’s the direct child of nested more deeply . parent') and potentially running all the way up the DOM. To find li's that have text containing BOTH Mary AND John: $('li:contains("Mary"):contains("John")') To find li's that have text containing EITHER Mary OR Aug 3, 2012 · Simple task here, but I can't get it right: If a table-cell is clicked, I need to check whether it contains an input field or not. Array B contains the following values. myClass"); or you could recurse over the children. If the substring is not found, it returns -1. etc. querySelector(". find( selector ) Nov 28, 2010 · AJAX / JQuery (recommended) If you use JQuery to bring information via AJAX. Ask Question Asked 15 years, 11 months ago. Both work for single level children, and SLaks's answer may be more efficient if you know there is only one level of children to check against. contains() method returns true if the DOM element provided by the second argument is a descendant of the DOM element provided by the first argument, whether it is a direct child or nested more deeply. jquery check a div to see if it has any text in it. Feb 2, 2012 · I would like to use Jquery to add a class to "li" element that contains a "span" element with a html/val equal to zero. currentStyle || getComputedStyle(element, null); and //to iterate over css styles from style tags or linked CSS for i document. 6, always use . How do you check if an element (e. If element exists, it returns true. I want to check to see if the <li> has a class t Jun 8, 2017 · I'm trying to check if an element contains an input element in the following function $(document). jQuery: Check if two classes exist per div. As with attribute value selectors, text inside the parentheses of :contains() can be written as a bare word or surrounded by quotation marks. 34, 78, 89. Mar 10, 2010 · Answer. But i m new in jquery. body, myElement ) If you want to select elements which name contains a given word, delimited by spaces $("input[name~='DiscountType']"). Therefore it is not enough that you use :contains() selector, you also need to check if the text you search for is the direct content of the element you are targeting for, something like that: Sep 16, 2010 · @EscapeNetscape: Nice to see a benchmark link. is('div'): Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments. The indexOf() method is used to determine the position of the substring. You might be able to use :contains(): $('#tblspecializations tr > td:contains(Some Value) + td:contains(2)'). Feb 24, 2012 · Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand Aug 28, 2017 · Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments. contains returns true for the element itself (e. Example: This example uses css() method to check an element co Oct 1, 2021 · This can be done using the jQuery contains selector to select elements that contain the string. For example, If you use || and the attr is false, the first check in the condition (attr !== 'undefined') would have a logical result of true, thus the if statement would short-circuit and bypass to run the lines of code inside the first condition, resulting in a unintended output. 1. jQuery. For example, if my last td has id "1234abc", I want to know if this id contains "34a". If you want to select element by class name, just replace # with . Syntax: $(selector). contains( container, contained ) Parameters: This method accepts two parameters as mentioned above and described below: container: This parameter holds the DOM element that may contain the other element. InArray() method. True, if Jul 17, 2019 · Going through jQuery you can use $(this). contains() method dedicated to this particular situation. 78, 89 W3Schools offers free online tutorials, references and exercises in all the major languages of the web. How to check if string is present in variable in jquery. fn. You can check the . Check span Mar 9, 2012 · Gosh, so many answers! To get just the text of an element, the simple way is to use textContent or, were not supported, innerText. Example Here Nov 23, 2009 · jQuery check if element has a class beginning with some string. Dec 27, 2012 · To check if an element contains another, use the ID selector and the length returned: alert( $("#lnk"). jquery Here is a little snippet If you’re trying to check wether element contains a class, without using jQuery. 3 jQuery. hasClass() method will return true if the class is assigned to an element, even if other classes also are. Will be slower than the approach used in the earlier answers (markedly slower, on some browsers), and have a much larger memory impact as well (jQuery has to build up an array of all of the div elements on the page, then go back and loop through them to see whether they have that class, all to throw away the array at the end). So, if we have a value and need to check whether the value exists in an associative array or not. className); } This accounts for the fact that element might contain multiple class names separated by space. Sep 21, 2011 · check if any element contains certain css style. [attr~="word"]), which is more appropriate in many cases. 8. //This is button click code This is the most generous of the jQuery attribute selectors that match against a value. I believe I need to use the . g. Viewed 5k times Apr 5, 2013 · in a function I need to check if a (div, span, p) contains any . I am trying to check to see if an HTML element has a class and if so, then run a function with jQuery. Apr 6, 2017 · Check if element only contains another element without any text. This method takes two parameters, which are the container element and the contained element, and returns true if the container element contains the contained element, and false You can create your own custom selector:hasValue and then use that to find, filter, or test any other jQuery elements. Dec 24, 2016 · The :contains() jQuery selector allows you to match find elements that contain a specified string of text. If no matching elements are found, it returns an empty array. length of the collection returned by the selector or check whether the first array element is 'undefined'. Mar 23, 2012 · Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. It filters the selector it's called on -- and it's faster than using $('. Sep 20, 2010 · If you have an element that does not have a specific selector and you still want to check if it is a descendant of another element, you can use jQuery. In jQuery: Feb 10, 2017 · Check if an element has the class `active` using jQuery. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more. inArray(. They will help you select all the elements you want directly without the need to call any other functions than . is May 31, 2014 · There are more complex scenarios where this doesn't work. contains() This static method works with DOM elements, not with jQuery elements and returns true or false. jQuery: how do I check if an element attribute contains part of a string? 2. i search lot but not getting. is( selector ) I don't think there is a . jquery if variable in a list of values. 8. Ask Question Asked 7 years, 10 months ago. In such a case nowadays we can use Element. simple example : Jul 6, 2023 · The jQuery:contains() selector in jQuery is used to select elements containing the specified string. rules //great for searching with var elements = document. Jquery Check if element's title contains specific text. " Most people have taken this for "how to check whether an element exists in the DOM using jQuery. serialize(); Or test the current element with . It will select an element if the selector's string appears anywhere within the element's attribute value. JQuery: if Is there any way to check if the certain element: $("#someElement") has a particular class (in my case, "test"). inArray() returns 0. com. Checking if a button exists. I need an jQuery script that will see if any element has an specific class and do an action like change position. Check if Element has a Child with a given ID; Check if Element has a Child with a given Class # Check if Element has a Child with a given ID. children(). Jquery check if element exist and if conditon. jQuery: check if a div contains a div with class on. The text jquery: check if string exist in element, return boolean. is() Jul 15, 2012 · You are comparing a jQuery object (jQuery('input:first')) to strings (the elements of the array). Syntax: $(":contains(text)") Parameters: This selector contains single parameter text which is mandatory and used to specify the text to find. jQuery Check if String Contains Word. :) I upvoted you though. Ask Question Asked 13 years, 6 months ago. In contrast, jQuery. The following code will show the element if it exists, and do nothing (with no errors) if it does not: Description: Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element. Approach 1: Use css() method to check an element contains certain CSS styles or not. Bingo! There we are using jQuery. if you want to check for a value, Since JQuery 1. child'). prop(): $('. Oct 16, 2024 · 🧠 Understanding jQuery. Jun 11, 2021 · We are using jQuery [attribute*=“value”] Selector. To clarify, as Tony mentioned you can use the ":contains()" selector to search within the text nodes, but jQuery will not return the individual text nodes in the results (just a list of elements). find() is a function of that object that finds elements within it. html elements in it before attempting to remove the html and add in new content. Jan 2, 2015 · I want to check whether each child element has ENCRYPT as a part of string with it's id. Using Regular Expression (RegExp) with test() MethodThe most efficient way to check if a string c I want to see if an <li> has a certain class, the catch is though that they all are unique but all contain a constant string of 'unqID'. 3 it returns true. Check presence of an element with jquery. contains() will return True this is an alternative, but an expensive one. inArray function to check our element is present in the array or not. e. element. Description: Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments. $. The supplied selector is tested against the descendants of the matching elements; the element will be included in the result if any of its descendant elements matches the selector. The text Mar 11, 2025 · In this example, we define a custom jQuery function called contains. " Hardly interchangeable. contains(element) === true) so this snippet should always work. expr[':']. 💡 Syntax. filter(function () { return [this. First JQuery selector checks for the ID via var name and then length property is used to verify whether something is selected or not. Jun 10, 2011 · me neither; I believe the downvoter just didn't see it as ideal as there's a jQuery . Aug 23, 2009 · @Pyjcoder The use of && in this instance is correct. Asking for help, clarification, or responding to other answers. Are you going to write multiple lines of vanilla js to support all browsers just to get a millisecond of performance over a single line of jquery? And as mentioned you would have to also check for nulls, so you get more out of a single line of jquery over vanilla js. This method returns the position of the first occurrence of a specified value in a string. If the selected element’s length is greater than 0, the element exists in the DOM; otherwise, it doesn’t. Here is the jQuery function I use: function isExists(var elemId){ return jQuery('#'+elemId). Dec 31, 2012 · Or put your jQuery code in a function you can use to shadow the $ symbol, like this: // If you have another library loaded, out here $ may not === jQuery (function($) { // Here, your code can happily assume $ === jQuery })(jQuery); Once you sort that out: I'd strongly recommend not using :contains if the page is of any Jun 16, 2011 · Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Here's a snippet which adds a simple contains(str) method to your arsenal: Jan 6, 2017 · jQuery: how to check if a specific element is already in an array. See documentation for . ), it's the kind of ugly, jQuery-ish solution that most sane people wouldn't tolerate. has(function that you described above. So the following will output: "is NOT in array" var Jul 29, 2024 · Given an HTML document containing some CSS property and the task is to check whether an element has a specific CSS style or not with the help of jQuery. Aug 31, 2010 · Like, I want to check if a link has a certain domain, or already has parameters attached to the end. How does the indexOf() method work in jQuery? The indexOf() method returns the position of the first occurrence of a specified substring within a string. Here is an example for a use case. Browser support seems to cover pretty much everything according to that MDN page as well. This can be used to check if an element is empty or not. – sierrasdetandil Commented Jan 5, 2018 at 20:38 Oct 4, 2009 · Is there a way to check if an event exists in jQuery? I’m working on a plugin that uses custom namespaced events, and would like to be able to check if the event is bound to an element or not. If you are expecting the returned result to be a string, you can check if your substring is contained within it: If you are expecting the returned result to be a string, you can check if your substring is contained within it: Jul 13, 2023 · In the below example, we learn about how to check if a value exists in an array or not using jQuey. 7405. append() method? $ if contains certain text, then run jquery. prop('checked', false); DOM API. If you're wondering about the edge case of checking if the click is on the element itself, Node. 3 days ago · If all li elements have different identifiers, then use one of the solutions below and replicate per element. " jQuery supports most CSS3 selectors, as well as some non-standard selectors. 11. length ; Example 1: In this example, the var name contains ID name to check. Aug 3, 2021 · It finds the number of object in a jQuery element. Jan 5, 2012 · I've used the contains()function successfully, but never have used it with an if-statement. See below for more details, In jQuery documentation it says: The matching text can appear directly within the selected element, in any of that element's descendants, or a combination. (Credits to @beezir) I think you are looking for :contains selector. Use . attr('id'); // Preserve existing id Jun 14, 2012 · for that i have to find span tag in table and check wheather contains text or not if contains text then make it yellow. Hence, to check if an element exists, we simply need to check if this array is empty or not. Apr 23, 2019 · Given an HTML document containing some CSS property and the task is to check whether an element has a specific CSS style or not with the help of jQuery. My below code doesn't seem to work when it should. Here are the different methods to check if the string contains only digits. styleSheets[i]. jquery. querySelectorAll(rules[i]. For example, given the HTML above, the following will return true: Given a jQuery object that represents a set of DOM elements, the . Nov 9, 2010 · Select elements where class contains the string 'award' (jQuery) 23 Select div (or other element) that class contains "grap" (o other specified word) for example May 13, 2010 · In jQuery, we can check if an element exists by using the jQuery selector to select it and . Any value other than 0 means that some element is present. contains() will return Jan 4, 2011 · How do I check if an element exists if the element is created by . The above check is not getting successful. Another way of checking whether an element exists in the DOM or not with jQuery is by calling the static method contains() on the jQuery class. keys(json). param2 is the element contained by the containner . The children() function returns a JQuery object that contains the children. By adding sterik * in front of it search all elements in document with matching part of id or class like [attribute=“value”] Selector. version added: 1. Use jquery to check Mar 27, 2012 · Use document. – nnnnnn. The selector returns an array of jQuery objects. each of theese checks must return true and then . So you just need to check the size and see if it has at least one child. inArray May 21, 2009 · check if div contains an element in jQuery. If the length is greater than 0, it means the element exists. Example: Aug 28, 2012 · The primary advantage of jQuery. function _isContains(json, value) { let contains = false; Object. The :contains() selector selects elements containing the specified string. extend({ hasClasses: function (selectors) { // Setup const _id = $(this). But when you use . OR Feb 1, 2015 · //to get info about the end results of CSS var computedStyle = element. The jQuery element selector selects elements based on the element name. How to Check if A Value exists in Array Jquery. className && new RegExp("(^|\\s)" + className + "(\\s|$)"). 3. This is useful for conditionally manipulating elements. Example: Jan 14, 2024 · In this tutorial, I show how you can check whether an Array already contains a specific value or not using Javascript and jQuery. This would return falseexample 3: Array A contains the following values. Feb 10, 2010 · A use case I had to work with isn't checking if an element is child of another specific element, but if an element is a child of a kind of element expressed by a selector. parent('. contains( container, contained ) Description: Check to see if a DOM element is a descendant of another DOM element. target or if it contains event. contains( document. If you're working with just one element, you can Jul 16, 2012 · Having html collection of selects you can check if every select has option with specific value and filter those which don't match condition: //get select collection: let selects = $('select') //reduce if select hasn't at least one option with value 1 . javascript/jquery- how to check, if an element has text attribute or not. Alternately, is there a way to check that en element has a certain style? In this example, I'd like to know if the element has "position: absolute". Now, we will learn a little about the jQuery. function hasClass(element, className) { return element. The :contains() selector in jQuery is used to select elements containing the specified string. If no matching elements are found, the array will be empty. Actually, jQuery documentation says The argument should always be a plain JavaScript Object as other types of object (DOM elements, primitive strings/numbers, host objects) may not give consistent results across browsers. contains(param1,param2); param1 is the container which also called parent , this param must be a DOM element . value != ""; }; Then you can find elements like this: var data = $("form input:hasValue"). Otherwise, it returns false. Try Teams for free Explore Teams I don't like $. What I want to do seems related: I'm providing the user a "filter" text box that they can type in, and I have a set of list items. The querySelector method returns the first element that matches the provided selector or null if no element matches. edit_text", function() { if($(this). Sep 25, 2023 · To verify the presence of an element in jQuery, you can utilize various methods, but the most straightforward and commonly used approach is to check the length property of the jQuery object obtained by selecting the element using a selector. May 26, 2021 · jQuery length Property: The length property is used to count number of the elements of the jQuery object. The $. – Jun 30, 2009 · With jQuery >=1. Provide details and share your research! But avoid …. 10. 78, 67, 34, 99, 56, 89. contains function in jQuery. Modified 7 years, 10 months ago. Check if element only contains another element without any text. If the object is in the array, it will return 0, but 0 is false in Javascript. Try Teams for free Explore Teams Oct 17, 2012 · Additionally there is the underlying cross browser support you get with jquery. If it's not there, a new one should be created. on("click", ". how can i do that. Example 1: This example uses :contains() selector to select an element. jQuery selector will always return jQuery collection object, if you want to check it contains anything you should check its length property. Jan 15, 2012 · Just use QS. Only element nodes are supported; if the second argument is a text or comment node, $. The syntax for the jQuery. May 14, 2020 · This contains() method in jQuery is used to check whether a DOM element is a descendant of another DOM element or not. Apr 23, 2024 · Note that this method allows you to test for other things as well. The . prop() Jul 31, 2024 · A string with only digits means it consists solely of numeric characters (0-9) and contains no other characters or symbols. Related. length() to check its length. matches. target as a descendant element. Is it possible to check for case Oct 1, 2021 · This can be done using the jQuery contains selector to select elements that contain the string. Oct 12, 2010 · jQuery check if element have specific attribute. Example: This example uses css() method to check an element co May 11, 2017 · @Nick Thanks for that. some(el => el. parents() – Feb 7, 2010 · just use the method in jquery , $. See this fiddle vs. var element = document Apr 23, 2015 · How do I check if an element is hidden in jQuery? 5226. For example, you can test whether an element is hidden (by using the custom :hidden selector): I'm ok with the function that Denys Séguret posted, it looks elegant and I like it. I've tried the following approach, but cant seem to make it work: HTML <div> <h2& I need to have a if/else statement inside a function. body, myElement ) Native DOM With jQuery >=1. children("#aboutLnk"). length - Example. getElementById("items"). 0. childNodes. hasValue = function(el,index,match) { return el. $("a. Thank you very much! check if div contains an element in jQuery. data(element) always returns a data object to the caller, creating one if no data object previously existed. 4 (2010) you can use the very fast function jQuery. The hasClass() method checks if any of the selected elements have a specified class name. The comparison between values is strict. Mar 12, 2009 · I have a table and I want to know if its last td its id contains a certain string. 15. getElementById("myElement"). Oct 16, 2019 · Given an HTML document containing some CSS property and the task is to check whether an element has a specific CSS style or not with the help of jQuery. A length of 0 means the element has no other elements in it. Check if the element has text, if not add one. You end up needing something more like: If parent element does not contain certain child element; jQuery Apr 23, 2024 · Note that it isn't always necessary to test whether an element exists. contains( container, descendant ) Example: To check if a element is in the document you could do this: jQuery. Because JavaScript treats 0 as loosely equal to false (i. 2. each(function (i, el) { //It'll be an array of elements }); If you want to select elements which id is equal to a given string or starting with that string followed by a hyphen If the first element within the array matches value, $. I am basically saying if H1 contains the word "true", do something, if not do something else. If your ul does contain other nodes than li, you will have to filter them. contains() method in jQuery is used to check if a DOM element contains another DOM element. link Selecting Elements by ID Jun 30, 2015 · As you can see, we are adding elements to the array, checking the array element in the first two click functions. Be aware, though, that :contains() will return the element if the given string is matched anywhere in the text of the element. This is the way, but I don`t think this will work. How to check whether a string contains a substring in JavaScript? 4990. This requires within the program in some cases like – Stop new value from insert if it already exists in an Array. My code only works if an element doesn't have multiple classes. This kind of overlapping is what IMO contributes to people thinking that jQuery is a language, when really it's just a framework. Dec 22, 2015 · Attribute Contains Selector [name*="value"]: Selects elements that have the specified attribute with a value containing the a given substring Check out the sample and jSFiddle Demonstration Sample This is quite old, but hear me out on this one. Jan 6, 2016 · Since event. Jquery: How to check if the element has certain css class/style. inArray function. How do I find if an element contains a specific class with jQuery? 2. Depending on the element, the matching text can appear directly within or within a descendant of the selected element. length. Jan 18, 2013 · in the last if statement we check for the node to exist and than check for a node type and node name. Definition and Usage. Syntax: jQuery. So far I got this The element Selector. – Aug 13, 2024 · Checking if an element exists in jQuery involves selecting the element and verifying its length. This would return trueexample 2: Array A contains the following values. this one. Change the code in order to compare the input's value (wich is a string) to the array elements: Be careful with isEmptyObject!For jQuery 1. contact"). The $. removeChild(node) removes the designated element node, which is marked in 'IMG' string. See if div contains another div with a particular class? 147. As much as I love jQuery, sometimes I feel that it overlaps onto features that should be left alone. 9. toggle(function Mar 3, 2024 · The :contains() is a jQuery selector that allows us to search text within the element and select it and perform an action on it. The code you’ve provided is a valid and effective way to check if an element exists in the DOM using Oct 1, 2013 · using [readonly] as a selector simply checks if the attribute is defined on your element. There is a . innerText anyway, so yeah jQuery helps there for sure. Example: This example uses css() method to check an element contains certain CSS style or not. children]. is there a way of doing a regex thing like ~= maybe? currently, I have alert(ref); Feb 12, 2013 · Look at the Attribute starts with, Attribute ends with and Attribute contains Selectors. #cadrage) has a display style property? This is what I have found around the net and yet, it is not workin May 29, 2018 · When you search for the td:contains it will retrieve all DOM elements that match that selector, jQuery: check if text exists in another element. I never really use . Jan 9, 2009 · Modern jQuery. If I change the observation string to a very long UTF-8 multibyte text, the results change drastically? Also, the results changed with change in position of substring in the observation string. The matching text can appear directly within the selected element, in any of that element's descendants, or a combination thereof. A length greater than 0 will mean it finds an anchor tag and it will evaluate to true. The string can be contained directly in the element as text, or in a child element. This object contains an array of document elements that match the provided selector. – Find whether an element contains in a jquery selector. For a complete selector reference, visit the Selectors documentation on api. If an element that contains only text or comments should not be considered empty for your needs, then you should use one of the other solutions. What if you want to select an element with class A that doesn't contain elements with class B. value == 1) }); Feb 9, 2012 · Array B contains the following values. contains() method is straightforward: In the 10 years since you've asked this question, jQuery has added almost exactly the . It traverses through the descendants of the specified element and returns true if it finds the target element, otherwise false. If your website contains a lot of pages, and you want your jQuery Jul 11, 2012 · Ask questions, find answers and collaborate at work with Stack Overflow for Teams. addClass(). Feb 8, 2011 · -1 because this was already suggested in this answer plus that if condition will always be true so it's pointless. 0 == false, but 0 !== false), to check for the presence of value within array, you need to check if it's not equal to (or greater than) -1. test(element. Oct 10, 2010 · In my case , I used the 'is' a jQuery function, I had a HTML element with different css classes added , I was looking for a specific class in the middle of these , so I used the "is" a good alternative to check a class dynamically added to an html element , which already has other css classes, it is another good alternative. All browsers in use support one or the other (maybe both). You can use any the following examples inside an IF statement and they all produce the same result. has() method constructs a new jQuery object from a subset of the matching elements. If ANY of the selected elements has the specified class name, this method will return "true". I just tweaked a little bit that function since if the class specified in the parameter, is not present in the whole DOM, it fails when the recursion reaches the document object because is true that we control if the element has the parent node (in the last line, and when the document is the element the parent Jun 13, 2013 · @Alex Quite interresting indeed, thx for the input! BTW, re-reading the question, Is there any way in jQuery to check if any parent,, closest() check for element itself so this is not really answering question, OP could wish to explecitely exclude the element itself, so ya, answer was to use . You can pass the parent element and the element that you want Sep 18, 2013 · First time I work with jQuery. zkmqr qpivm yfpup zudwg ornf hqnjf axgsh xwmvti jjvs rpmm jng yyo dwtfdnf moe uoosg