Results 1 to 6 of 6

Thread: Chaining CSS class with Javascript in IE problems

  1. #1
    Join Date
    Oct 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Chaining CSS class with Javascript in IE problems

    Hi all,

    I've recently found out that you can change an element's class using Javascript and thought I'd try it out. Here's the Javascript code I used which is called on an onMouseOver event:

    Code:
    var testArray=Array(), d=document;
    
    function init(){
    	index=0;
    	num=d.getElementsByTagName("span");
    	
            for(i=0;i<num.length;i++){
    		if(num[i].id=("test")){
    			testArray[index]=num[i];
    			index++;
    		}
    	}
    }
    
    // onMouseOver
    function show(i){
    	for(x=0; x<testArray.length; x++){
    		if(x == i){
    			testArray[x].className="normal";
    		}
    		else{
    			testArray[x].className="hideMe";
    		}	
    	}
    }
    which is all well and works a treat with FireFox, but when I tried it out in IE (ver6) it didnt work. I've spent ages trying to figure out why, but I dont know why it doesnt work with IE. Does anyone have any ideas?

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You're using a lot of undefined variables and some odd logic there. I'm surprised it works in FX.
    Code:
    if(num[i].id=("test")){
    className, not id: only one element can have a given ID. Also, note that this is an assignment, which would have had the effect of setting the class of every span on the page to "test" and then adding them to the array.
    Code:
    var testArray = [];
    
    function init() {
            for(var i = 0, els = document.getElementsByTagName("span"); i < els.length; ++i)
    		if(els[i].className === "test")
    			testArray.push(els[i]);
    }
    
    function show(n) {
    	for(var i = 0; i < testArray.length; ++i)
    		testArray[i].className = "hideMe";
    	testArray[n].className = "normal";
    }
    Last edited by Twey; 10-01-2006 at 02:05 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Oct 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanks for the reply. I was under the impression that you can get away with having undefined variables in javascript. I havent played around with javascript for ages!

    Anyway what I was doing was trying to recreate something I saw on here: http://www.virginradio.co.uk/ when a user hovers over a link the news item appears, whereas all the other news items remain hidden. I thought I could use ids to recreate this effect

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I was under the impression that you can get away with having undefined variables in javascript.
    You can. It's just not a good idea.

    Undefined variables automatically become global, meaning that memory is used much less efficiently than needs to be the case.
    I thought I could use ids to recreate this effect
    You can, but not that way.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Oct 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ha ha thanks. Well, I'll have another bash at it. Any pointers would be appreciated though

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You should use classes, as I showed above.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •