jessica
jessica

Reputation: 1687

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>

Upvotes: 3

Views: 65

Answers (2)

Darrin Cullop
Darrin Cullop

Reputation: 1208

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.

Upvotes: 5

Sushil
Sushil

Reputation: 2835

your javascript code should be like

$(function() {

    function hide(id) {
        $("#" + id).hide(); // notice the '+'
    }

});

Upvotes: 0

Related Questions