Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to make a div disappear with javascript

I am trying to make a div disappear with javascript. It just doesn't work. What am I doing wrong?

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">    
    document.getElementById("des").style.visibility = "hidden";    
</script>    

<div id="des">
  Text.
  <a href="">link</a>    
</div>
like image 673
user3860911 Avatar asked Nov 28 '22 00:11

user3860911


2 Answers

You are running the script before the DOM has loaded.

If you put the script after the div, it works

like image 59
Derongan Avatar answered Dec 09 '22 17:12

Derongan


Try and throw a document ready around your code.

And if you are loading jquery you can just do $('#des').css('visibility', 'hidden'); or $('#des').hide()

<script type="text/javascript">
    $(document).ready(function(){
        $('#des').css('visibility', 'hidden');
    });
</script>
like image 39
Evan Avatar answered Dec 09 '22 18:12

Evan