javascriptfunction

Javascript functions - what could be wrong?


I've some experience with compilable languages, but I'm new to Javascript, and it really messes with me. So probably I'm generally doing something wrong, but I don't know what it is.

A code example (not working):

function show_element(element_name){
    document.getElementsByName(element_name)[0].style.display="";
}
function hide_element(element_name){
    document.getElementsByName(element_name)[0].style.display="none";
}
function switch_display(element_to_hide, element_to_show){
    hide_element(element_to_hide);
    show_element(element_to_show);          
}

The HTML code looks like this:

<area shape="rect" coords="0,252,98,337" onMouseOver="switch_display("content_navigator_1","content_navigator_2")">

And debugging in Firefox gives me the error message while onMouseOver:

syntax error: switch_display (

Thanks for helping me!

(Secondary questions: Do you also experience problems with Javascript? Is it really a logical language, or just a trial & error language? And is there somewhere a JS compiler, for good sake?)


Solution

  • Your specific error is here (u have other issues, see bottom of my answer)

    <area shape="rect" coords="0,252,98,337" onMouseOver="switch_display("content_navigator_1","content_navigator_2")">
    

    Switch it to

    <area shape="rect" coords="0,252,98,337" onMouseOver="switch_display('content_navigator_1','content_navigator_2')">
    

    Let me know if I need to explain why.

    One more problem, besides the cause of your error, is u try to call an HTML Element by name, better would be to do it by id, and then use:

    document.getElementById(element_id).style.display="";