2

I've been fighting with this simple piece of code forever. I'm trying to use a <button> to call a simple JavaScript function to hide a <div>.

HTML:

<button type="button" onclick="close('wrong')">OK</button>

JavaScript:

function close(ID)
{
    document.getElementById(ID).style.display="none";
}

I'm sure it's something really simple, I just can't, for the life of me, find it.

famousgarkin
  • 13,687
  • 5
  • 58
  • 74
Daisy
  • 53
  • 1
  • 6

3 Answers3

5

Try changing the close function name to something else like closeit because it may be confusing it with the window.close() function.

Detect
  • 2,049
  • 1
  • 12
  • 21
-1

Try document.getElementById('wrong').setAttribute('style', 'display:none');

Jason
  • 15,915
  • 3
  • 48
  • 72
-1

Let's do something in jQuery:

<button type="button" id="btn">OK</button>
<div id="myDiv">Div's contents here</div>

Now, you need just use the code below to do what you want:

<script type="javascript">
$(document).ready(function(e) {    
   $("#btn").click(function () {
      $("#myDiv").hide();
   });
});
</script>

Notice: you can have some properties for hide() function: Fast and slow. This will affect on the speed of hiding action.

$("#myDiv").hide("slow");

I hope this can help you.

Mohammad Saberi
  • 12,864
  • 27
  • 75
  • 127