Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

Test if h2 contains specific css class

I want to test if the div with an id of "result" contains an h2 tag with a class of "error"

This should trigger an alert:
<div id="result"><h2 class="error">There Was an Error</h2></div>

This should NOT trigger an alert:
<div id="result"><h2 class="success">Success!</h2></div>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
<h1>Management Area</h1>
<div id="result"><h2 class="error">There Was an Error</h2></div>
<p>Content Here</p>
<script type="text/javascript">
	var obj = document.getElementById('result');
	if (obj contains an h2 with a class of error) {

Open in new window

1 Solution
Hi, here's a quick javascript to help you out. You need to modify it according to your page
In your case you just need to add slightly more complicated logic depending on what you want
to do if you find that type of error css class.
Fetch the element using script like this:
var myvar1;
myvar1 = document.getElementsByTagName('h2')

then you need to check its class property like this:

 trigger alert
do something else


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now