Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: What is wrong with this code?

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

    Default What is wrong with this code?

    Code:
            function show(){
    		document.getElementById('main02').style.filter = "none";
    	}
    	function hide(){
    		document.getElementById('main02').style.filter = "gray";
    	}
    	window.onload = function() {
    		//hide();
    		
    		var menu = document.getElementById('chromemenu');
    		var menuitems=document.getElementById('chromemenu').getElementsByTagName("a");
    		for (var i=0;i<menuitems.length;i++){			
    			menuitems[i].onmouseout = hide();
    			menuitems[i].onmouseover = show();
    		}
    		
    	}
    Is something wrong with this code?
    I want to add mouseover and mouseout events in all "a" tags inside the div with id "chromemenu".

    The problem is that instead of adding the events, it executes the hide() function..

    Any Help??

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    I think you just need to take off the parenthesis on the show and hide for these:
    menuitems[i].onmouseout = hide(); ---> menuitems[i].onmouseout = hide;
    menuitems[i].onmouseover = show(); ---> menuitems[i].onmouseover = show;

  3. #3
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default

    I think filters only apply to IE as well, so you're missing out on a lot of people.

  4. #4
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Try this:

    Code:
    menuitem[i].onmouseout = function(){
    hide();
    }
    menuitem[i].onmouseover = function(){
    show();
    }

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm not sure about 'gray' but 'none' is not a filter, I don't think 'gray' (in and of itself as you've written it there) is either. And, as noted earlier, filters are for IE only.

    Although the other two suggestions given above are good ones as far as general coding practices go, the entire code looks like garbage to me even with those improvements.

    Since this is a request for a modification to an existing DD script, it would be better to post a question in the dynamic drive scripts help section and, rather than offering your beginner's attempt at solving your problem, describe in detail, in plain English the effect you want to achieve.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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

    Default

    Quote Originally Posted by jscheuer1
    the other two suggestions given above are good ones as far as general coding practices go
    Quote Originally Posted by shachi
    menuitem[i].onmouseout = function(){
    hide();
    }
    menuitem[i].onmouseover = function(){
    show();
    }
    This one isn't. There's no need to create separate functions here.
    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!

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    This one isn't. There's no need to create separate functions here.
    Sure it is. Just not in this situation. Ooops, since this situation really isn't a situation, I guess it is up to interpretation. I simply meant they can work if needed in otherwise well written code, but that the whole thing was such a mess that these adjustments (both of them) are relatively meaningless.

    I do agree with what you seem to be saying in principal - If the function itself can be assigned, there is no reason to create another function simply to do so. However, there is no need to assign the functions directly either, as it still won't make any difference. Until the correct code to achieve the desired effect (whatever that is) can be determined, there's no way to be certain of the best way to assign it to events. I do prefer Trinithis' idea in those situations where it will do the trick. There are cases where the assignment needs to be determined by a test though (perhaps whether to use filters or some other method, depending upon availability). In some of these cases shachi's approach might be ideal.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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

    Default

    Ok thanx for your answers everybody...

    Sorry for late answering.. But i was a bit offended by jscheuer1 posts..

    He has helped many times in the past and i wasn't expecting such answers..

    Anyway, i guess the problem was that i have accidentaly used the word chromemenu in my code...

    My example is irrelevant with DD scripts. I post it here because it is javascript problem.

    I know filters are only for IE. If you look at http://www.w3schools.com/dhtml/dhtml_css.asp you will see that there is a gray filter like that i use inside my "garbage" code..

    My problem was to attach mouse events in all A tags inside a Div tag..

    That's it. Nothing more, nothing else...

    OK, and if u know any way to have a grayscale effect in a Div conpatible with all browsers i'll be happy to learn..

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You've gathered all the a tags but the events you are assigning to them only act directly on the element with the id of 'main02'.

    There still is no such filter as 'none'. The gray filter would be better applied using its formal name, if used at all. Images may be made gray scale in an image program and swapped with their color counterparts, then all browsers will see them as gray/colored depending upon their 'state'. The style color (and background-color) gray (and various shades of gray) are available for any text.

    Without seeing the markup, it is hard to be more specific.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default

    Hey,

    Unless you are a great writer, sometimes your writing is going to come across as rude; thats the reason most fights start on msn. Obviously jscheuer didn't mean to offend, but he knows code.

    Code:
    function filterSwap(id,type){
    document.getElementById(id).style.filter = type;
    }
    
    window.onload = function() {
    var menuitems = document.getElementById('chromemenu').getElementsByTagName("a");
    var i;
    for (i=0; i<menuitems.length; i++){
    	if(document.attachEvent){
    		menuitems[i].attachEvent('onmouseover',filterSwap('main02','none'));
    		menuitems[i].attachEvent('onmouseout',filterSwap('main02','gray'));
    		}
    	}
    }

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
  •