homepage Welcome to WebmasterWorld Guest from 54.205.254.108
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / CSS
Forum Library, Charter, Moderators: not2easy

CSS Forum

    
Need help with javascript
Having a few DOM, CSS, JS related probs
Soupisgoodfood




msg:1202886
 10:59 pm on Aug 7, 2002 (gmt 0)

Hi. I'm having a few probs getting a script to work.

In code 1 (at the bottom) I basicly want to hide all elements of a certain class.
I can get the 'for' loop to work successfully. But when I use 'i' to address each one. I get an error. It seems as if you can't place a var in there for 'item()' like you can with an array.
If that's the case, why did they do such a thing? Any work-arounds?
If not. What am I doing wrong?

Also. I want to filter divs with a certain class. I was planning to include an 'if{}else{}' inside the loop. Is there anyway of attaching this as a condition to the 'for' loop?

In code 2, I want to have a function that will hide or show the current element, without the need to have an ID for each element.
I have an version of the code that passes an ID and uses 'getElementById' instead of the 'srcElement', and that's working fine.
Oh yeah, In case you're thinking that once you hide something, you won't be able to click on it to show it again. I'm planning to target a sibling or child of the 'event.srcElement', just have to get this working first ;)

Thanks,
Justin.

Code 1
###########

for (var i in document.getElementsByTagName('div')) {

alert(document.getElementsByTagName('div').item(i).getAttribute("class"));

}

###########

Code 2
###########

function twist() {

if (event.srcElement.style.display == "none") {
event.srcElement.style.display == "block")
} else {
event.srcElement.style.display == "none")
}
}

###########

 

Purple Martin




msg:1202887
 12:20 am on Aug 8, 2002 (gmt 0)

To get the first one working, I'd have a go at using eval() to parse a string version of the expression. Use "+" to concatenate bits of string with variables such as i.

HTH

moonbiter




msg:1202888
 2:03 am on Aug 8, 2002 (gmt 0)

Try changing your alert in code 1 to:

alert(i);

and you'll see why it doesn't work. The for/in statement is looping through the properties of the HTMLCollection object you have created. What you want to do is use a regular for loop to iterate all of the nodes in the object, like this:

var divNodeList = document.getElementsByTagName('div');
for (i = 0; i < divNodeList.length; i++) {
if (divNodeList[i].getAttribute('class')){
alert(divNodeList[i].getAttribute('class'));
} else {
alert(divNodeList[i].className);
}
}

Longer, but it will give you the array you want. It's actually more verbose than it needs to be, but some browsers have difficulty finding the class attribute using the getAttribute() method, so you want to check the className property just to be safe.

Can't help you much with the #2, since I frankly don't understand event handling all that well. Besides, I've posted too much code already ...

Soupisgoodfood




msg:1202889
 2:41 am on Aug 8, 2002 (gmt 0)

Thanks for that. It's working now. But only with 'className' (which I did look for, but couldn't find).
I acctualy tried another appoach ealier using 'document.classes'. But that came out undefined (worked OK for 'document.images' though). Could that be an IE 5.5 (win) bug?

It makes sence after reading your post a few times.
I've come from a PHP background. And couldn't figure out why they had both [] and () since they seemed to do the same thing. Is () for properties of an object?
What's the point point in having the item() method on the first place? Does it offer any advantages to accessing the array?

Thanks again,
Justin.

moonbiter




msg:1202890
 2:29 pm on Aug 8, 2002 (gmt 0)

Thanks for that. It's working now. But only with 'className' (which I did look for, but couldn't find).

This isn't surprising. Microsoft didn't claim to fully support the W3C DOM until IE 6 (and even then, it doesn't completely).

I acctualy tried another appoach ealier using 'document.classes'. But that came out undefined (worked OK for 'document.images' though). Could that be an IE 5.5 (win) bug?

The classes property is a proprietary Netscape 4 extension. It is no longer supported, and should be avoided.

Is () for properties of an object?

() is used to enclose arguments of a function (and methods).

What's the point point in having the item() method on the first place? Does it offer any advantages to accessing the array?

item() is a method used by several nodeList objects in the W3C DOM that allows you to specify the index of a particular node. In Javascript, you can use the array notation of [] to do the same thing.

Thre reason for this is that the W3C DOM is meant for more than Dynamic HTML. It is a general purpose API that describes XML documents, and the item() method reflects this.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / CSS
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved