0

I know this is a question that's been asked a hundred times but I can't figure out why it's not working on me site.

Javascript:

<script>
  function show(boxid){
    document.getElementById(boxid).style.visibility="visible";
  }

  function hide(boxid){
    document.getElementById(boxid).style.visibility="hidden";
  }
</script>

HTML (PHP generated):

echo '<div id="selectedBookingActionLink">';
  echo '<a href="#" onClick="show(cancelPopUp)">Cancel</a>';
echo '</div>';

echo '<div id="cancelPopUp">';
  echo '<div class="question">Cancel?</div>';
  echo '<div class="answer">Yes</div>';
  echo '<div class="answer">No</div>';
echo '</div>';

CSS:

#cancelPopUp
{
  width: 260px;
  height: 80px;
  visibility: hidden;
}

As you can see, I'm trying to change the visibility property of the cancelPopUp div when the user clicks the "Cancel" link. I've done some research and found that why I'm doing should work. Yet the pop up box does not appear.

1
  • 5
    Not related to this issue, but I think it is better to use display: none for hiding a div than using visibility: hidden because the latter only makes it invisible but the height will be used. Commented Sep 3, 2013 at 17:25

2 Answers 2

4

You need to use quotes when passing the ID of the div to the show function:

echo '<a href="#" onClick="show(\'cancelPopUp\')">Cancel</a>';
Sign up to request clarification or add additional context in comments.

1 Comment

+1 I initially didn't notice that boxid was a parameter to the function :)
0

I've posted a working version of your code here → http://jsfiddle.net/matbloom/uHQsd/

  1. First, make sure you have positioned your JS in the head of your document, above the body.

  2. Second, don't forget to add single quotes inside the onClick.

    <a href="#" onClick="show('cancelPopUp')">Cancel</a>
    
  3. Third, I would suggest using the 'display' property vs 'visibility'.

Also, I've provided a simple toggle function which may work better for your application.

Hope this helps!

HTML:

<div id="selectedBookingActionLink"> 
    <a href="#" onClick="show('cancelPopUp')">Cancel</a>
</div>

<div id="cancelPopUp" style="display:none;">
    <div class="question">Cancel?</div>
    <div class="answer">Yes</div>
    <div class="answer">No</div>
</div>

CSS:

#cancelPopUp {
    width: 260px;
    height: 80px;
}

JS:

function show(boxid) {
    document.getElementById(boxid).style.display = "block";
}

function hide(boxid) {
    document.getElementById(boxid).style.display = "none";
}

/* Optional toggle function */
function toggle(boxid) {
    var e = document.getElementById(boxid);
    if (e.style.display == "block") {
        e.style.display = "none";
    } else { 
        e.style.display = "block";
    }
}

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.