1

I'm trying to write a javascript plugin to detect the state of a code block, and if it's not in his normal state, trigger some kind of notification.

I've used document.getElementById("alert"); to retrieve the code:

"Normal" state:

<div id="alert" class="tooltip normal" title="">
    <a href="https://"></a>
</div>

"Special" state:

<div id="alert" class="tooltip special" title="">
    <a href="https://"></a>
</div>

I've thought on keeping the "normal" state on a variable, and compare it to the actual state to see if it's different.

Any suggestions? Thanks!

Sachith Muhandiram
  • 2,819
  • 10
  • 45
  • 94
DHON JOE
  • 23
  • 3

3 Answers3

0
function checkNormal(){
    var node = document.getElementById('alert');
    return node.className.indexOf('normal') > -1 ;
}
Srikant Sahu
  • 839
  • 1
  • 6
  • 16
0

If you need to find the elements and check their class names to detect the state, the code below should solve your problem

var elems = document.querySelectorAll("[id=alert]");

for (var i=0;i<elems.length;i++)
{

  var elem = elems[i];
  if (elem.className=='tooltip special')
  {alert('special one');}
  else
  {alert('not special');}
}

jsfiddle: https://jsfiddle.net/yzq6gp7w/4/

danielarend
  • 1,379
  • 13
  • 26
0
function containsClass(element, className) {
  return element.classList.contains(className)
}

var result = containsClass(document.getElementById('alert'), 'special')

or just simply...

var result = document.getElementById('alert').classList.contains('special')
grzesiekgs
  • 463
  • 2
  • 11