[Javascript] Javascript and CSS

David Hucklesby davidh126 at writeme.com
Wed Mar 26 15:41:33 CDT 2008


On Mon, 24 Mar 2008 12:18:33 +1100, Professional Web Pages - Information wrote:
> To whom it may concern,
>
> I am trying to obtain an element with Javascript but would also like to include certain
> classs with the element to weed out the specific element
>

Using document.getElementById() - as you suggest - will get you the
one and only element in the document having that ID. You can then
see if that element has the class name you want, like this, perhaps:

function hasClass(element, myClass) {
  var myClass = myClass.replace(/\-/g, "\\-");   // escape hyphens
  var re = new RegExp("\\b ?"+myClass+"\\b");
  if (!element.className) return false;
  return re.test(element.className);
}


Cordially,
David
--





More information about the Javascript mailing list