-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

11
  • 1
    Code has to go in the question. Your question cannot depend on 3rd party links to be meaningfully answered.
    – user229044
    May 27, 2014 at 12:10
  • I added the code from the link to the question, not sure if edits are meant to do that though May 27, 2014 at 12:13
  • 1
    How the hell is this off-topic?
    – Kolyunya
    May 27, 2014 at 12:13
  • 3
    You are trying to disable a label, not the input element. Use .find() to get the input element. Reason why this question was closed was because it didn't contain any code just a link to your website.
    – Anton
    May 27, 2014 at 12:14
  • 2
    @عثمانغني I've already voted to reopen the question.
    – Anton
    May 27, 2014 at 12:18

3 Answers 3

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

1
  • OP already said his aim is to add a attribute to the element. He just used disabled as an example
    – asprin
    May 27, 2014 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');
0

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.