Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to toggle attr() in jquery

Tags:

jquery

I have a simple add attribute function:

$(".list-toggle").click(function() {
    $(".list-sort").attr('colspan', 6);
});

My question is: how can I turn this into a toggle, so colspan="6" is removed from the element on the next click?

like image 894
alias51 Avatar asked Sep 06 '13 19:09

alias51


People also ask

What is the use of toggle () method in jQuery?

jQuery toggle() Method The toggle() method toggles between hide() and show() for the selected elements. This method checks the selected elements for visibility. show() is run if an element is hidden.

Is ATTR deprecated in jQuery?

attr() deprecated/removed for use with "checked" and "disabled" properties. Many sites now use various means to update their version of jQuery to a later version than 1.4, the version in Drupal 7. jQuery deprecated (version 1.6) and removed (version 1.9) the use of .


7 Answers

If you're feeling fancy:

$('.list-sort').attr('colspan', function(index, attr){
    return attr == 6 ? null : 6;
});

Working Fiddle

ES6 Syntax (2021):

$('.list-sort').attr('colspan', (_, attr) => attr == 6 ? null : 6));
like image 116
RienNeVaPlu͢s Avatar answered Sep 23 '22 10:09

RienNeVaPlu͢s


$('.list-toggle').click(function() {
    var $listSort = $('.list-sort');
    if ($listSort.attr('colspan')) {
        $listSort.removeAttr('colspan');
    } else {
        $listSort.attr('colspan', 6);
    }
});

Here's a working fiddle example.

See the answer by @RienNeVaPlus below for a more elegant solution.

like image 45
musicnothing Avatar answered Sep 24 '22 10:09

musicnothing


For readonly/disabled and other attributes with true/false values

$(':submit').attr('disabled', function(_, attr){ return !attr});
like image 35
BitOfUniverse Avatar answered Sep 24 '22 10:09

BitOfUniverse


I know this is old and answered but I recently had to implement this and decided to make 2 simple jQuery plugins that might help for those interested

usage:

// 1
$('.container').toggleAttr('aria-hidden', "true");
// 2
$('.container').toggleAttrVal('aria-hidden', "true", "false");

1 - Toggles the entire attribute regardless if the original value doesn't match the one you provided.

2 - Toggles the value of the attribute between the 2 provided values.

 // jquery toggle whole attribute
  $.fn.toggleAttr = function(attr, val) {
    var test = $(this).attr(attr);
    if ( test ) { 
      // if attrib exists with ANY value, still remove it
      $(this).removeAttr(attr);
    } else {
      $(this).attr(attr, val);
    }
    return this;
  };

  // jquery toggle just the attribute value
  $.fn.toggleAttrVal = function(attr, val1, val2) {
    var test = $(this).attr(attr);
    if ( test === val1) {
      $(this).attr(attr, val2);
      return this;
    }
    if ( test === val2) {
      $(this).attr(attr, val1);
      return this;
    }
    // default to val1 if neither
    $(this).attr(attr, val1);
    return this;
  };

This is how you would use it in the original example:

$(".list-toggle").click(function() {
    $(".list-sort").toggleAttr('colspan', 6);
});
like image 26
Francisc0 Avatar answered Sep 26 '22 10:09

Francisc0


This would be a good place to use a closure:

(function() {
  var toggled = false;
  $(".list-toggle").click(function() {
    toggled = !toggled;
    $(".list-sort").attr("colspan", toggled ? 6 : null);
  });
})();

The toggled variable will only exist inside of the scope defined, and can be used to store the state of the toggle from one click event to the next.

like image 45
thirdender Avatar answered Sep 26 '22 10:09

thirdender


$(".list-toggle").click(function() {
    $(this).hasAttr('colspan') ? 
        $(this).removeAttr('colspan') : $(this).attr('colspan', 6);
});
like image 34
Zuks Avatar answered Sep 22 '22 10:09

Zuks


$(".list-toggle").click(function() {
    $(this).attr('colspan') ? 
    $(this).removeAttr('colspan') : $(this).attr('colspan', 6);
});
like image 22
user2398069 Avatar answered Sep 26 '22 10:09

user2398069