0

I am super new to jQuery and JaVascript and I love it but I'm having issues,,,

I have an element called button1 that I want to make another element called bubble1 disappear when clicked once and re-appear when clicked again.

I tried this but it did not work

$(document).ready(function(){
    $('#button_1').click(function (){
        $('#bubble1').css('visibility','hidden');
        $('#button_1').click (function(){
            $('#bubble1'.css('visibility'.'visible');
        });
    });
});

This second one worked but the object disappear and re-appears with one click.

$(document).ready(function (){
    $('#button_1').click(function (){
        $('#bubble1').fadeOut();
    });
});
$('#button_1').click(function (){
    $('#bubble1').fadeIn();
});
0

3 Answers 3

5

You shouldn't re-bind click event, you can just use toggle() or fadeToggle():

$("#button_1").click(function() {
    $("#bubble1").fadeToggle();
});

DEMO: http://jsfiddle.net/E9PRa/

Sign up to request clarification or add additional context in comments.

Comments

1

You can use toggle instead of click.

Live Demo

$('#button_1').click(function (){
   $('#bubble1').toggle();
});

You are binding click twice you can put condition and binding it twice will cause double action.

Comments

0

The toggle in the prior answers works great and is what most would do. If you want something explicit which does the same thing and would allow you to do more depending on the state of the other element:

$("#button_1").click(function() {
    if ($("#bubble1").is(':visible')) 
       $('#bubble1').fadeOut();
    else
       $('#bubble1').fadeIn();
});

3 Comments

all this answers look great , SPECIALLY the if statement,, but it moves out place. Is it possible to use something like the .css(visibility) or something similar that holds the element in place maybe with the if statement? thanks guys
Of course. You can do whatever you want inside the if statement.
Thank YOU , I was able to do it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.