Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding an element using jQuery

I'm trying to hide an element using jQuery, but I think I did something wrong. Please take a look at my code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script type = "text/javascript"> 

$(function(){

function hide(id) { 
$("#"+id).hide();
}

});

hide("test");

</script>

<div id = "test"> Hello </div>
like image 838
jessica Avatar asked Jun 24 '26 14:06

jessica


1 Answers

Sushil is right, but also, your "hide" function should be outside the $() function and the call to it goes on the inside. The whole thing looks like this:

function hide(id) {    
   $("#" + id).hide();
}

$(function(){
    hide("test");
});

Putting the hide() function inside of the $() makes it so you can only call it from inside of the $(). So, put it on the outside, and then you can call it from anywhere, including, from inside the $() part.

like image 54
Darrin Cullop Avatar answered Jun 26 '26 03:06

Darrin Cullop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!