L McNally
L McNally

Reputation: 19

Show/Hide Form not working

I'm trying to create a button that when you click on it, it shows a small box with a newsletter sign up form in it.

I've written my JavaScript but it doesn't seem to work.

I'd appreciate any pointers you could give me on why this isn't working.

<script>
  function setVisibility('subscribe_form') {
     if (document.getElementById('bt1').value == 'Hide Layer') {
       document.getElementById('bt1').value = 'Show Layer';
       document.getElementById('subscribe_form').style.display = 'none';
     } else {
       document.getElementById('bt1').value = 'Hide Layer';
       document.getElementById('subscribe_form').style.display = 'block';
     }
   }
</script>

 <input type='button' name='type' id='bt1' value='Show Layer' onclick="setVisibility('subscribe_form');";>           
 <form action="%%GLOBAL_ShopPath%%/subscribe.php" method="post" id="subscribe_form" class="subscribe_form" name="subscribe_form">

Upvotes: 2

Views: 231

Answers (1)

Paul Roub
Paul Roub

Reputation: 36438

You've written a string ('subscribe_form') where there should be a variable name (subscribe_form) in your function declaration. That, plus some other typos (; in the input tag, for one) are leading you astray.

Your JavaScript console would have some messages about these errors.

A working version:

function setVisibility(subscribe_form) {
  if (document.getElementById('bt1').value == 'Hide Layer') {
    document.getElementById('bt1').value = 'Show Layer';
    document.getElementById(subscribe_form).style.display = 'none';
  } else {
    document.getElementById('bt1').value = 'Hide Layer';
    document.getElementById(subscribe_form).style.display = 'block';
  }
}
<input type='button' name='type' id='bt1' value='Show Layer' onclick="setVisibility('subscribe_form');" />
<form action="%%GLOBAL_ShopPath%%/subscribe.php" method="post" id="subscribe_form" class="subscribe_form" name="subscribe_form" style="display:none">
  <p>subscribe form</p>
</form>

Upvotes: 2

Related Questions