-1

Why .prop will not set the attribute value in the example? Bootstrap 3.1.1 and jQuery 2.1 are used.

HTML

<div class="btn-group" data-toggle="buttons">
    <label class="btn btn-primary">
        <input type="checkbox"> Option 1
    </label>
    <label class="btn btn-primary">
        <input type="checkbox"> Option 2
    </label>
    <label class="btn btn-primary">
        <input type="checkbox"> Option 3
    </label>
</div>

jQuery

$('label').click
(
    function()
    {
        // Does not set attribute
        $(this).prop('foo','bar');
    }
);

Live demo

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
Kolyunya
  • 5,973
  • 7
  • 46
  • 81

3 Answers3

2

You need to use attr() not prop() like,

$('label').click(function(){
    // Use attr as disabled is not a property of a label
    $(this).attr('disabled',true);
});

disabled is not a property of a label, so it will not work on label, as your tried. And if you want to add disabled as an attribute then you need to use attr()

Live Demo

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • OP already said his aim is to add a attribute to the element. He just used disabled as an example – asprin May 27 '14 at 12:35
0

labels do not have a disable property inherently built in. You will need to add a class to disable them yourself.

Something like:

.disabled {
   background-color: #428bca;
}

And to add the class to your element:

$(this).addClass('disabled');
Raju Padhara
  • 687
  • 1
  • 7
  • 20
0

hi i thing you are confused between attr and prop, both are different in many cases this link will solve your problem

Community
  • 1
  • 1
Jeetendra Chauhan
  • 1,977
  • 2
  • 15
  • 21