Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide element with jQuery on button press

Tags:

jquery

I have buttons associated with a certain choice and I wanna display a form related to the choice and remove the buttons when one of them is clicked. I hide all the forms in the beginning so the user must click the button first. This is the code:

$(document).load(function () {
    $("#radial, #rect").hide();
});
$("#rectS").click(function () {
    $("#rect").show(slow);
    $(".confirm").remove();
});
$("#radialS").click(function () {
    $("#radial").show(slow);
    $(".confirm").remove();
});

But it doesn't do anything and no one can tell me why. The hiding at the start doesn't work either, by the way. jQuery is really frustrating...

Codepen: http://codepen.io/megakoresh/pen/HJEzx

like image 766
Megakoresh Avatar asked Mar 14 '26 07:03

Megakoresh


2 Answers

You need to change slow to 'slow' . And wrap your code within $(document).ready(function(){ }); to bind event after dom elements are loaded

$(document).ready(function () {
    $("#radial, #rect").hide();
    $("#rectS").click(function () {
        $("#rect").show('slow');
        //--------------^----^--
        $(".confirm").remove();
    });
    $("#radialS").click(function () {
        $("#radial").show('slow');
        //----------------^----^--
        $(".confirm").remove();
    });
});

Codepen Demo

Documentation : http://api.jquery.com/show/

like image 129
Pranav C Balan Avatar answered Mar 16 '26 22:03

Pranav C Balan


Try

Put all your code in DOM Ready

$(document).ready(function () {
    $("#radial, #rect").hide();
    $("#rectS").click(function () {
        $("#rect").show('slow');
              //        ^    ^ wrap show in quotes
        $(".confirm").remove();
    });
    $("#radialS").click(function () {
        $("#radial").show('slow');
        $(".confirm").remove();
    });
});
like image 21
Tushar Gupta - curioustushar Avatar answered Mar 16 '26 22:03

Tushar Gupta - curioustushar



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!