2

How to add/remove attribute when click?

This is what I tried so far.

$('.btn').click(function() {
  $(this).attr("title", "selected").siblings().removeAttr("title", "selected");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>
    <button title="selected" class="btn">click</button>
  </li>
  <li>
    <button class="btn">click</button>
  </li>
  <li>
    <button class="btn">click</button>
  </li>
</ul>

When click button, add attribute "selected" and others remove attribute "selected".

This is DEMO, https://jsfiddle.net/a8egp275/3/

How do I need to fix it? please help.

1
  • The .removeAttr() hasn't second parameter. Commented Sep 17, 2018 at 8:17

6 Answers 6

4

The issue is because the .btn elements are not siblings, hence siblings() returns nothing.

To fix this you can use closest() to get the nearest common ancestor, then find() the .btn elements, excluding the one which was clicked. Also note that removeAttr() only requires a single argument; the attribute name to be removed. Try this:

$('.btn').click(function() {
  var $btn = $(this).attr("title", "selected");
  $(this).closest('ul').find('.btn').not($btn).removeAttr('title');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li>
    <button title="selected" class="btn">click</button>
  </li>
  <li>
    <button class="btn">click</button>
  </li>
  <li>
    <button class="btn">click</button>
  </li>
</ul>

Sign up to request clarification or add additional context in comments.

2 Comments

your code and text are inconsistent with each other. I personally wouldn't recommend the "clear all .btn" method because there may be others in different parts of the DOM.
@Alnitak you're correct - I changed my mind on my approach while writing the example and forgot to update the description :) I've put it back to how it originally was.
0

You can remove the title attribute from all dom with .btn class and then add the title to the selected element

$('.btn').click(function() {
  $('.btn[title ="selected"').removeAttr("title")
  $(this).attr("title", "selected");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>
    <button title="selected" class="btn">click</button>
  </li>
  <li>
    <button class="btn">click</button>
  </li>
  <li>
    <button class="btn">click</button>
  </li>
</ul>

Comments

0

When you remove the attribute you just specify the name of the attribute
so you need to do as follows:

    $('.btn').click(function() {
        $('.btn').removeAttr("title");
        $(this).attr("title", "selected");
    });

Comments

0

Select the current selected with an attribute selector

$('.btn').click(function() {
  $('[title=selected]').removeAttr("title");
  $(this).attr("title", "selected");
});
[title=selected] { color: red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>
    <button title="selected" class="btn">click</button>
  </li>
  <li>
    <button class="btn">click</button>
  </li>
  <li>
    <button class="btn">click</button>
  </li>
</ul>

Comments

0

You should do something like this:

$('.btn').click(function() {

  //remove the selected attr from all btns
  $('.btn[title]').removeAttr('title');

  //add the title attr to the current btn
    $(this).attr('title', 'selected');

});

https://jsfiddle.net/a8egp275/42/

Note: this code will alwasy have one button selected, if you want this to behave as checkbox rather than radio button, you will need to do something like this:

$('.btn').click(function() {

    //get the attr title and check later if it exist
    var attr = $(this).attr('title');

  //remove the selected attr from all btns
  $('.btn[title]').removeAttr('title');

  //add the attr only if it didnt exist before
  if(typeof attr === typeof undefined || attr === false){
        $(this).attr('title', 'selected');
  }

});

https://jsfiddle.net/a8egp275/45/

Comments

-1

your event handler is okay. But take a look in the jQuery documentation. It states, that the removeAttr() function only takes the attribute name as a parameter. Also afaik you have to make seperated statements for the add and remove, because the attr() function does not return the jQuery object.

TLDR:

$('.btn').click(function() {
  $(this).attr("title", "selected");
  $(this).siblings().removeAttr("title");
});

Also you need to change your HTML structure if you want to use the siblings() function :)

4 Comments

While this is true, it's not the problem
@RoryMcCrossan you are right, overlooked the first problem.
Unfortunately, this still has the same problem as the OP
@RoryMcCrossan third error found, in the html structure, the buttons are not siblings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.