How to Check the Specific Element in the JavaScript Class?At times, we must verify that the element does have the class 'className' ( Any specific name ). It is included the method of the class List entity. It can be used to determine whether an element has a particular class name. The single method is used to check the required element in the JavaScript class. SyntaxThe following syntax shows the available
How to Operate JavaScript "contains" MethodThe className is passed to the contains() method of the classList property of the element in this method. The method returns true if the element includes the className. Otherwise, false is returned.
Due to the absence of the error class on the div> element, the following example returns false. We can use it as a reference for the availability in the html tag. ExamplesThe following examples show specific elements of the page using JavaScript. Example 1 The following example shows available classes in the element using the JavaScript method. Here we use the alert tag to display the output message. Output The given image shows the available class result as an output. Example 2 The following example shows available classes in the element using the JavaScript method. Here, the class name is available in the element shown using the innerHTML function. Output The given image shows the available class result as an output. Example 3 The following example shows available classes in the element using the JavaScript method. Here, the class name is not available in the element shown using the innerHTML function. Output The given image shows the available class result as an output. Example 4 The following example shows available classes in the element using the JavaScript method. Here, the first class name is available in the element and the second class name is not available in the Html element. The result values are displayed using the innerHTML function. Output The given image shows the available class result as an output. ConclusionThe contains() function in JavaScript is used to check required class is available or not in the Html element. It is an easy, simple, and user-friendly method to run JavaScript code for checking the class name.
Next TopicJavaScript NaN Function
|