Maqk
Maqk

Reputation: 525

jQuery toggle function is deprecated. What should I use instead?

I have a jQuery .toggle() not working after the latest version of jQuery. How should I write to make it working with the new jQuery Versions. Please help in writing the new method.

jQuery('.ia_select_all').toggle( function(){
      var ia_attr_id = jQuery(this).attr( 'data-id' );
      jQuery('.' +ia_attr_id+ ' option').prop( 'selected',true );
},function(){
      var ia_attr_id = jQuery( this ).attr( 'data-id' );
      jQuery('.' +ia_attr_id+ ' option' ).prop( 'selected',false);
});

Upvotes: 0

Views: 307

Answers (2)

speciesUnknown
speciesUnknown

Reputation: 1753

Here is a post from the jquery forums which suggests an alternative.

Upvotes: -1

charlietfl
charlietfl

Reputation: 171679

toggle(function, function) just alternates function to call on alternate clicks

So you should be able to replace with something like:

jQuery('.ia_select_all').on('click', function(){
    var ia_attr_id = jQuery(this).attr( 'data-id' );
    var $option = jQuery('.' +ia_attr_id+ ' option');
    $option.prop('selected', !$option.prop('selected'));
});

If you were to provide some sample html we can refine this if needed

Upvotes: 2

Related Questions