Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / CSS / June 2007



Tip: Looking for answers? Try searching our database.

javascript does not work with multiple classes?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Amedee Van Gasse - 28 Jun 2007 08:52 GMT
I have a css+javascript question.

I have this script:

function showIT() {
    for (i=0;i<document.getElementsByTagName("li").length; i++) {
        if (document.getElementsByTagName("li").item(i).className == "it"){
            document.getElementsByTagName("li").item(i).style.display = "list-
item";
        }
    }
}

this style:

.it        {
        display:            none;
        }

and this html:

<ul>
<li class="it ibo"><h2>header1</h2>
<ul>
    <li>item1</li>
    <li>item2</li>
</ul></li>

<li class="it"><h2>header2</h2>
<ul>
    <li>item1</li>
    <li>item2</li>
    <li>item3</li>
</ul></li>
</ul>

Somewhere else I have a link:
<a href="#" onClick="showIT()">for IT</a>

This works for elements with one class, but not for elements with
multiple classes. (like "it ibo")
Am I doing something wrong here?

--
Amedee
Darko - 28 Jun 2007 09:11 GMT
On Jun 28, 9:52 am, Amedee Van Gasse <Amedee.VanGa...@gmail.com>
wrote:
> I have a css+javascript question.
>
[quoted text clipped - 42 lines]
> --
> Amedee

I didn't know you can even assign an HTML object more than one css
class. But even if you can, then you probably access it the same way
(object.className == "it ibo")...
One remark about incredibly bad piece of code:

>         for (i=0;i<document.getElementsByTagName("li").length; i++) {
>                 if (document.getElementsByTagName("li").item(i).className == "it"){
>                         document.getElementsByTagName("li").item(i).style.display = "list-item";

Wouldn't it have been better if it was:
var items = document.getElementsByTagName( "li" );
var i, length = items.length;
for ( i = 0; i < length; i++ )
    if ( items[i].className == "it" )
          items[i].style.display = "list-item";
?
Amedee Van Gasse - 28 Jun 2007 09:15 GMT
> I didn't know you can even assign an HTML object more than one css
> class.

Yes you can. They have to be separated by spaces.

> But even if you can, then you probably access it the same way
> (object.className == "it ibo")...

Tried that, doesn't work.

> One remark about incredibly bad piece of code:
>
[quoted text clipped - 9 lines]
>            items[i].style.display = "list-item";
> ?

Of course.
I'm lazy, I copied it from the lazyweb.
This is not production quality code, only proof of concept.

--
Amedee
d d - 28 Jun 2007 09:31 GMT
> if (document.getElementsByTagName("li").item(i).className == "it"){
> <li class="it ibo"><h2>header1</h2>
[quoted text clipped - 3 lines]
> Am I doing something wrong here?
> Amedee

You're checking if the classname is exactly equal to "it".
It seems like you want to be checking if the classname
STARTS WITH "it". Here's a way of doing that:

if(document.getElementsByTagName("li").item(i).className.indexOf("it")==0)

That's checking if the classname starts with "it".

~dd
RobG - 28 Jun 2007 09:34 GMT
On Jun 28, 5:52 pm, Amedee Van Gasse <Amedee.VanGa...@gmail.com>
wrote:
> I have a css+javascript question.
>
> I have this script:
>
> function showIT() {
>         for (i=0;i<document.getElementsByTagName("li").length; i++) {

Doing document.getElementsByTagName("li") 3 times on every loop is
extremely poor coding.  Get the collection once and re-use it:

 var el, els = document.getElementsByTagName("li");
 for (var i=0, len=els.length; i<len; i++){
   el = els[i];

>                 if (document.getElementsByTagName("li").item(i).className == "it"){

The classname attribute is returned as a string of all the class
names.  You are essentially saying:

 if ('it ibo' == 'it')

which clearly returns false.  You can use a regular expression like:

 var re = /(^|\s)it(\s|$)/
 for ( ...) {
   if (re.test(el.className)) {
     \\ element has "it" as one of its CSS classes
   }
 }

>                         document.getElementsByTagName("li").item(i).style.display = "list-
> item";

   el.style.display = 'list-item';

But if you are attempting to show and hide the element, toggle between
'none' and '' (empty string) which allows the element to return to its
default:

    el.style.display = (el.style.display=='none')?'':'none';

--
Rob
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.