1

I can't get the following code to work that I copied from https://css-tricks.com/snippets/jquery/toggle-text/.

Can you also explain the purpose of ':visible'. Thanks.

Here's the fiddle and the code is copied below.

http://jsfiddle.net/thgv95da/

HTML:

<button id="more-less-options-button">more options</button>

JS

    $("#more-less-options-button").click(function() {
     var txt = $("#extra-options").is(':visible') ? 'more options' : 'less options';
     $("#more-less-options-button").text(txt);
     $("#extra-options").slideToggle();
});

3 Answers 3

2

:visible looks for the element in html page with id extra-options but you don't have any.

I think you are looking for this: http://jsfiddle.net/g6r0c8j5/1/

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

Comments

0

I created a span with id extra-options and now it works. Jquery checks if the content with id extra-options is visible if visible change the text and hide the extra-options

$("#more-less-options-button").click(function() {
  
    var txt = $("#extra-options").is(':visible') ? 'more options' : 'less options';
     $("#more-less-options-button").text(txt);
     $("#extra-options").slideToggle();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="more-less-options-button">more options</button>
<span id="extra-options">Text</span>

3 Comments

Thank you. How would I modify the code to remove span ? I don't need the span. Just need the button to change its text when I push the button.
@Danny I don't exactly know how to edit this however i can give you a new one'
Thanks. Can you create an alternate using .toggle() and the var txt? (Not the deprecated version of .toggle().)
0

You don't have the #extra-options in the HTML.

Here's a fork of your jsfiddle.

Visible checks to see if the div is open or not, and the text changes accordingly.

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.