Jquery text () change onggle ()?

I want to make a script that changes the text of the switching link depending on the visibility of the element.

So, while #form apparently, I want the # form-container text to be "Hide ...", and while it is hidden, I want the text to be "Show ...".

I tried this line - if ($ ('# form'). Is (": visible")) {another way: if ($ ('# form'). Is (": visible") = = "true") {- but it also doesn't work.

What happened? How to change text each time another item is switched?

$('.toggle').click(
    function()
    {
        $('#form').slideToggle();

            if($('#form').is(":visible")){
                $('#form-container a').text("Hide form container");
            }
            else {
                $('#form-container a').text("Show form container");  
            } 
    });

Thank.

+3
source share
2 answers

, .slideToggle(), , , :

$('.toggle').click(function() {
  $('#form').slideToggle(function() {
    $('#form-container a').text(
      $(this).is(':visible') ? "Hide form container" : "Show form container"
    );
  });
});
+14

.

$("#form").slideToggle(
  function () {
    //Hide
  },
  function () {
    //Show
  }
);

: http://api.jquery.com/toggle/

0

Source: https://habr.com/ru/post/1759416/


All Articles