0

I have this slideToggle code. When I click the button that says "open", it changes the text to "close". It stays as "close"d even if you click it again so I want to change the text back to "open" when I click on "close" and vice versa.

  <head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script> 
$(document).ready(function(){
    $("#flip").click(function(){
        $("#panel").slideToggle("slow");
        $('#flip').html("close");
        reset ();
    });
});
</script>

<style> 
#panel,#flip
{
    padding:5px;
    text-align:center;
    background-color:#e5eecc;
    border:solid 1px #c3c3c3;
}
#panel
{
    padding:50px;
    display:none;
}
</style>
</head>
<body>

    <div id="flip">open</div>
    <div id="panel">Hello world!</div>

3 Answers 3

1

try below code.

$(document).ready(function(){
    $("#flip").click(function(){
        $("#panel").slideToggle("slow");
        var t=$('#flip').html()==='close'?'open':'close';
        $('#flip').html(t);
        reset ();
    });
});

Better you can have a boolean to check what you are showing. like:

var showingClose=false;

$(document).ready(function(){
......
    $("#panel").slideToggle("slow");
    if(showingClose){
        $('#flip').html('open');
    }else{
        $('#flip').html('close');
    }
    showingClose = !showingClose;
.......

As pointed out by @filoxo in comment, better to use $('#flip').text() to get/set value.

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

2 Comments

While this works for the current case, this could break easily and cause issues if another html element is placed inside the div with id="flip". Something like <div><p>Open</p></div>. This is because the .html() method will return the entire inner html value, tags and all. Just a consideration!
@filoxo sure, but as it is being set from here only there is lesser changes. Moreover seems he is just experimenting. This does not look like production code.
0

Just toggle the css class. Check http://api.jquery.com/toggleclass/

$(document).ready(function(){
    $("#flip").click(function(){
        $("#panel").toggleClass("flip");
    });
});

css:

#panel
{
    padding:50px;
    display:none;
}
.flip
{
    padding:5px;
    text-align:center;
    background-color:#e5eecc;
    border:solid 1px #c3c3c3;
}

Comments

0

You can leverage jQuery's .is() function in conjunction with the visibility selector to determine if the element is displayed or not. This is better than a boolean in my opinion and is also better for readability. Lastly, using a ternary operator can help you set the button text correctly based on said visibility.

$(document).ready(function(){
    $("#flip").click(function(){
        $("#panel").slideToggle("slow");
        var btnTxt = $("#panel").is(":visible") ? "close" : "open";
        $('#flip').text( btnTxt );
        reset ();
    });
});

Comments

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.