[Javascript] Javascript and CSS

Information info at professionalwebpages.biz
Wed Mar 26 20:56:26 CDT 2008


Dear Paul,

Thank you

Regards

----- Original Message -----
From:    Paul Novitski +ADw-paul+AEA-juniperwebcraft.com+AD4-
Subject: Re: +AFs-Javascript+AF0- Javascript and CSS
Date:    Thu, 27 Mar 2008 01:48:00 -00:00

At 3/26/2008 01:41 PM, David Hucklesby wrote:
+AD4-Using document.getElementById() - as you suggest - will get you the
+AD4-one and only element in the document having that ID. You can then
+AD4-see if that element has the class name you want, like this, perhaps:
+AD4-
+AD4-function hasClass(element, myClass) +AHs-
+AD4-   var myClass +AD0- myClass.replace(/+AFw--/g, +ACIAXABc--+ACI-)+ADs-   // escape hyphens
+AD4-   var re +AD0- new RegExp(+ACIAXABc-b ?+ACIAKw-myClass+-+ACIAXABc-b+ACI-)+ADs-
+AD4-   if (+ACE-element.className) return false+ADs-
+AD4-   return re.test(element.className)+ADs-
+AD4AfQ-



At 3/26/2008 03:12 PM, +AD0-?utf-7?Q?Information?+AD0- wrote:
+AD4-So there is no way that is built into JS to get classes?


As David points out, there very definitely is a way to get the class 
name of a page element using JavaScript.  It isn't built into 
JavaScript, it's built into the DOM (Document Object Model) which 
JavaScript can access.

The class of an element is held in its className property, cf. 
http://developer.mozilla.org/en/docs/DOM:element.className

You can get the class of a known element simply by referencing its className:

         var oExample +AD0- document.getElementById('example')+ADs-
         var sClass +AD0- oExample.className+ADs-

You can get a list of all the elements on the page with a given class 
by cycling through them:

         var aElements +AD0- document.getElementsByTagName('+ACo-')+ADs-

         for (var iElement+AD0-0+ADs- iElement +ADw- aElements.length+ADs- iElement+-+-)
         +AHs-
                 var sClass +AD0- aElements+AFs-iElement+AF0-.className+ADs-

                         if (sClass +ACYAJg- re.test(sClass)
                         +AHs-
                                 aSelected+AFsAXQ- +AD0- aElements+AFs-iElement+AF0AOw-
                         +AH0-
         +AH0-

(Where re is a regular expression such as the ones David and I have suggested.)

Regards,

Paul
+AF8AXwBfAF8AXwBfAF8AXwBfAF8AXwBfAF8AXwBfAF8AXwBfAF8AXwBfAF8AXwBfAF8AXw-

Paul Novitski
Juniper Webcraft Ltd.
http://juniperwebcraft.com 

+AF8AXwBfAF8AXwBfAF8AXwBfAF8AXwBfAF8AXwBfAF8AXwBfAF8AXwBfAF8AXwBfAF8AXwBfAF8AXwBfAF8AXwBfAF8AXwBfAF8AXwBfAF8AXwBfAF8AXwBfAF8AXw-
Javascript mailing list
Javascript+AEA-lists.evolt.org
http://lists.evolt.org/mailman/listinfo/javascript




More information about the Javascript mailing list