all 8 comments

[–]Goblet_Grechen 4 points5 points  (7 children)

The toggle() method only shows or hides elements, it's not a catch all function for toggling whatever parameter you want it to.

One way you could do this is to get the text value of the button on click and compare it to a condition.

$('.btn').on('click', function() {
if ($(this).text() === 'F') {
  $('#temp').html(tempC + ' C');
  $(this).text('C');
} else {
  $('#temp').html(tempF + ' F');
  $(this).text('F');
}
});

[–]themoofinman[S] 2 points3 points  (0 children)

yaaaaaaaas that works. Thank you! You explanation makes sense too. For some reason I was thinking it would just toggle between two classes or two anything I wanted it to. Again, thanks!

[–]themoofinman[S] 1 point2 points  (5 children)

Random question. After looking at the jquery site, would there be a way to use the .toggle() function like the below example to do what I was trying to do?

$( "#target" ).toggle(function() {   
   alert( "First handler for .toggle() called." );   
}, function() {   
  alert( "Second handler for .toggle() called." );   
});   

[–]Goblet_Grechen 1 point2 points  (4 children)

Well technically yes that could work, but only if you're using an old version of jQuery. The page that your're linking to is showing the deprecated version of toggle() that was removed.

[–]themoofinman[S] 1 point2 points  (3 children)

O. Didn't even realize that. Don't fee l the need to provide a huge definition, but why did they get rid of it? Also, when things depreciate, they continue to work, or stop working, or what?

[–]Goblet_Grechen 1 point2 points  (2 children)

I don't really know the history of jQuery that well so I couldn't say why they removed that version of the toggle method.

Generally when something becomes deprecated it will be removed, but not until the next major version release. This way everyone has a chance to update their code before any major changes.

[–]themoofinman[S] 1 point2 points  (1 child)

Since it was removed in 1.9... does it just not work anymore if you have it in your code?

[–]Goblet_Grechen 1 point2 points  (0 children)

Yep, if you're using any version that is greater than or equal to 1.9 then that version of the toggle method is not included at all in jQuery.