javascript - Simple jquery toggle (Not Working) - Stack Overflow

admin2025-04-19  2

I think I may be using the wrong js library. No idea.. I was following a tutorial online and I saw the guy code it, and watch it work. I reviewed twice and did it over and still didn't work.

Here's my html:

<html lang="en">
<head>
<meta charset="utf-8" />
<title>Title</title>
 <script src="//ajax.googleapis/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
  <script src="hideshow.js"></script>

</head>
<body>

<a href="#" id="hideshow">Hide </a>

<div id="message" style="width:300px; border:1px solid black; padding:10px">

This is the wording inside of the text for testing purposes.

</div>
</body>

Here's my js from hideshow.js:

$('#hideshow').toggle(function () {

$('#hideshow').text('Show');

}, function () {

$('#hideshow').text('Hide');
});

I appreciate it, Thanks for whoever can figure this out. :(

I think I may be using the wrong js library. No idea.. I was following a tutorial online and I saw the guy code it, and watch it work. I reviewed twice and did it over and still didn't work.

Here's my html:

<html lang="en">
<head>
<meta charset="utf-8" />
<title>Title</title>
 <script src="//ajax.googleapis./ajax/libs/jquery/1.11.0/jquery.min.js"></script>
  <script src="hideshow.js"></script>

</head>
<body>

<a href="#" id="hideshow">Hide </a>

<div id="message" style="width:300px; border:1px solid black; padding:10px">

This is the wording inside of the text for testing purposes.

</div>
</body>

Here's my js from hideshow.js:

$('#hideshow').toggle(function () {

$('#hideshow').text('Show');

}, function () {

$('#hideshow').text('Hide');
});

I appreciate it, Thanks for whoever can figure this out. :(

Share asked Feb 23, 2014 at 18:22 some guy named davesome guy named dave 111 silver badge2 bronze badges 1
  • 1 The toggle event was removed from jQuery 1.9. See api.jquery./toggle-event – j08691 Commented Feb 23, 2014 at 18:27
Add a ment  | 

3 Answers 3

Reset to default 5

Looks like you're trying to use the toggle event, which was deprecated in jQuery 1.8 and removed on jQuery 1.9. The new toggle function has many different overloads, but none that behaves like the old one. Take a look at it and choose the one that suits you the most. For example, you could do something like this:

$('#hideshow').click(function () {
    $('#message').toggle();
    if($('#message').is(':visible')) {
        $('#hideshow').text('Hide');        
    } else {
        $('#hideshow').text('Show');   
    }
});

And here's a jsfiddle for you to play with. http://jsfiddle/74GzV/1/

As j08691 pointed out, toggle is removed, but even if it wasn't <script src="hideshow.js"></script> is running before the page has loaded, and therefore #hideshow would turn up undefined. Try putting the whole thing in a $(document).ready after working around the lack of toggle

var toggler = $('#hideshow');
toggler.text('Hide');

toggler.click(function(e) {
    e.preventDefault();
    $('#message').slideToggle('slow', function () {
      var togglerLabel = $(this).is(':visible') ? 'Hide' : 'Show';
      toggler.text(togglerLabel);
    });
});

http://jsfiddle/4TqQT/46/

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745072357a283359.html

最新回复(0)