Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Check if classname exists

<div class="Trade">
    <div id="Offer">
        <div class="NoResults">No Result!</div>
        <div class="FooterPager"> <span id="Offer"><a disabled="disabled">First</a>&nbsp;<a disabled="disabled">Previous</a>&nbsp;<a disabled="disabled">Next</a>&nbsp;<a disabled="disabled">Last</a>&nbsp;</span>
        </div>
    </div>
</div>

Here is my javascript:

function Check(){
return !(iframe.contentDocument.getElementById("Offer").firstElementChild.tagName.toLowerCase() == "table");
}

Is it possible to return a true or false value to check if the class "NoResult" exists? If so, how do I do it? You guys rocks. I can't change the HTML coding, only the javascript.

like image 865
user2276965 Avatar asked May 10 '13 05:05

user2276965


People also ask

What is classList Add in JavaScript?

The Element.classList is a read-only property that returns a live DOMTokenList collection of the class attributes of the element. This can then be used to manipulate the class list. Using classList is a convenient alternative to accessing an element's list of classes as a space-delimited string via element.className .

How do you check if the ID exists in JavaScript?

getElementById() to get the ID and store the ID into a variable. Then use JSON. stringify() method on the element (variable that store ID) and compare the element with 'null' string and then identify whether the element exists or not.

How do I check if a div contains an element?

To check if a div element contains specific text:Use the includes() method to check if the specific text is contained in the div . If it is, the includes() method returns true , otherwise false is returned.

How do I know if a CSS class is applied?

In JavaScript, you can use the contains() method provided by the classList object to check if any element contains a specific CSS class. This method returns true if the class exists. Otherwise, false is returned.


3 Answers

Use classList.

var hasClass = element.classList.contains('some-class');

Further Reading (disclaimer: link to my own post).

If not supported in your target platforms, then try...

var hasClass = (" " + element.className + " ").indexOf(" some-class ") > -1;
like image 187
alex Avatar answered Oct 20 '22 11:10

alex


if ( ~(' ' + element.className + ' ').indexOf(' NoResult ') ) {
    // here you go. You've got that class...
}
like image 28
Joseph Silber Avatar answered Oct 20 '22 10:10

Joseph Silber


In Javascript without using a library like jQuery, you could do it by:

(' ' + MyElement.className + ' ').indexOf(' MyClassName ') != -1

This evaluates to true when "MyClassName" appears anywhere by itself inside the string as defined on the className property.

In your specific case, something like:

function Check()
{
    //Returns true when it exists
    return (' ' + iframe.contentDocument.getElementById('Offer').firstElementChild.className + ' ').indexOf(' NoResults ') != -1;
}

There was previously a mistake in my answer where it would incorrectly identify a partial match as pointed out in the comments. Basically, you need to consider in the check that the class name is whole. A neat way to do this (like the other answers show) is that if you spaces before and after both the entire className property and the class you are searching for, it will always find the whole class.

While this will work, I recommend Alex's answer as while classList isn't available in every browser (<= IE9 and a few others), it is a neater solution to the problem.

like image 1
Turnerj Avatar answered Oct 20 '22 09:10

Turnerj