Sam Sam - 29 days ago 5
Javascript Question

jQuery deprecated '.toggle()'. Seeking alternative method


was deprecated from jQuery, I am looking for a new simple solution which will enable me to create a "Read more" button which slides down a paragraph whilst changing the button text to "Read less".

I have put together this working code:

var moreText = "Read more";
var lessText = "Read less";

$(document).ready(function () {
$("a.readmorebtn").toggle(function () {

function () {

You'll see in the fiddle that 'Migrate 1.1.0' is checked on the options on the left, which is the only way to make
work in this way with this (newest) version of jQuery.

My question is, can you think of another way of performing the same function but without using
and Migrate?

Apologies if you feel this question has been answered elsewhere, but I keep finding hints at the workarounds being "relatively straightforward", without really seeing any definitive approaches. Unfortunately being new to jQuery it doesn't seem so straight forward to me!

Many, many thanks.


I personally think all the answers here are too elaborate for what you need. This should do the trick just fine and it works with multiple occurences.

var moreText = "Read more",
    lessText = "Read less",
    moreButton = $("a.readmorebtn"); () {
    var $this = $(this);
    $this.text($this.text() == moreText ? lessText : moreText);