user1412615
user1412615

Reputation: 1

slideToggle: Change the text a second time (open -> close -> open -> close...)

<div id="sidebar_archive_infoline_bottom"><a id="showallnews" href="#">SHOW ALL NEWS</a></div>     

<script>
$('#newsdiv').hide();
$('#showallnews').click(function () {
$('#newsdiv').slideToggle('fast');
$('#showallnews').text('CLOSE')
}); </script>

The text "SHOW ALL NEWS" changes to "CLOSE" after clicking the first time on it. Now I need a method to change it from "CLOSE" to "SHOW ALL NEWS" and so on...

Best regards!

Upvotes: 0

Views: 728

Answers (3)

Ankur Verma
Ankur Verma

Reputation: 5933

<script>
    $('#newsdiv').hide();
    $('#showallnews').click(function() {
    $('#newsdiv').slideToggle('fast', function() {
        $('#showallnews').text().toLowerCase().indexOf('close') != -1 ? $('#showallnews').text('SHOW ALL NEWS') : $('#showallnews').text('CLOSE');
    });
});​
</script> 

Demo

Upvotes: 2

Cornel Urian
Cornel Urian

Reputation: 396

Try this

<script>
...
 $("#showallnews").toggle(function (){
            $("#showallnews").text("SHOW ALL NEWS")
            .stop();
        }, function(){

            $("#showallnews").text("CLOSE")
            .stop();
        });
</script>

Upvotes: 0

Akshat Goel
Akshat Goel

Reputation: 528

$('#showallnews').click(function () {
$('#newsdiv').slideToggle('fast');
$('#showallnews').addClass("closed");
$('#showallnews').text('CLOSE')
});

$('.closed').click(function(){
$('#newsdiv').slideToggle('fast');
$('#showallnews').removeClass("closed");
$('#showallnews').text('Show all news')

})

Not the best way but this can do.

Upvotes: 0

Related Questions