0

I was working on a simple show and hide function and it kept telling me that it's not defined.

Here's the HTML:

<address>
<a id="windows" href="#" onclick="toggle();">
<img src="imges/c980e3f5-badf-432a-ae6c-9e5341d13462.png" alt="" />
</a>
</address>
         <p class="right_page">
           this is sample of the book container<br/>
           thats suppose to read from ajax
            </p>

and here the function

    <script type="text/javasript">
function toggle() 
{
    var ele = document.getElementById("right_page");
    var text = document.getElementById("windows");
    if(ele.style.display == "block") {
            ele.style.display = "none";
        text.innerHTML = "show";
    }
    else {
        ele.style.display = "block";
        text.innerHTML = "hide";
    }
} 

</script> 
6
  • Please post the exact error message you're getting (i.e. what exactly is not defined). Commented Jul 18, 2011 at 8:07
  • did you define the function in the head of the document? Commented Jul 18, 2011 at 8:08
  • Try to debug with getfirebug.com Commented Jul 18, 2011 at 8:09
  • thats the error toggle is not defined Commented Jul 18, 2011 at 8:09
  • i define the function on the html body Commented Jul 18, 2011 at 8:10

3 Answers 3

2

I think it is due to a typo in your script tag. Change type="text/javasript" to type="text/javascript" (missing the c).

Update

Your image is being removed from the DOM due to you setting the innerHtml to a string. This overwrites the image which is part of the innerHtml.

The first time that you run toggle() there is not an inline style for display so ele.style.display will be an empty string. This means that it will drop into the logic for showing the paragraph. On subsequent calls the property will have a value so it should behave as you expect.

Sign up to request clarification or add additional context in comments.

2 Comments

oops yeah its work but work very strange its replace the link background image with hide and show word and so its hide the image of the link not the div i want it to hide
@aly1984: Does the div also have the ID windows? IDs have to be unique.
1

There are a couple of things wrong here:

  1. Did you paste this in directly from your code? If so, your <script type... line contains a typo (should be "javas*c*ript")

  2. You can't use getElementById() for right_page as it stands, because right_page is only defined as a class in your mark-up.

1 Comment

You need to sort the rest of your code out too. For example, setting innerHTML on windows means that your image is effectively removed from the page and replaced with some text.
0

First you missed the C change type="text/javascript", second the right_pae is not a ID, can't use document.getElementById because there are something wrong with your function, so it tells you the function is undefined

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.