345

How can I add an attribute into specific HTML tags in jQuery?

For example, like this simple HTML:

<input id="someid" />

Then adding an attribute disabled="true" like this:

<input id="someid" disabled="true" />
1

11 Answers 11

647

You can add attributes using attr like so:

$('#someid').attr('name', 'value');

However, for DOM properties like checked, disabled and readonly, the proper way to do this (as of JQuery 1.6) is to use prop.

$('#someid').prop('disabled', true);
Sign up to request clarification or add additional context in comments.

4 Comments

For me $('#someid').prop('disabled', true); doesn't work, but $('#someid').attr('disabled', true); works fine.
Agree with @VukašinManojlović.. for me the same.
Before you decide whether you're going to use attr or prop, it's important to understand the difference between properties and attributes. Without understanding the difference, you're likely to run into unexpected behavior. See stackoverflow.com/a/6004028/3367343
can prop be used as a replacement for attr now? or it is used in some special cases only?
57

best solution: from jQuery v1.6 you can use prop() to add a property

$('#someid').prop('disabled', true);

to remove it, use removeProp()

$('#someid').removeProp('disabled');

Reference

Also note that the .removeProp() method should not be used to set these properties to false. Once a native property is removed, it cannot be added again. See .removeProp() for more information.

Comments

43

You can do this with jQuery's .attr function, which will set attributes. Removing them is done via the .removeAttr function.

//.attr()
$("element").attr("id", "newId");
$("element").attr("disabled", true);

//.removeAttr()
$("element").removeAttr("id");
$("element").removeAttr("disabled");

2 Comments

Bonus for including the remove function!
IT DOESNT WORK!
17
$('#someid').attr('disabled', 'true');

Comments

8
$('#someid').attr('disabled', 'true');

Comments

5

Add attribute as:

$('#Selector_id').attr('disabled',true);

Comments

3
$('.some_selector').attr('disabled', true);

Comments

3

Use this code:

<script> 
   $('#someid').attr('disabled', 'true'); 
</script>

2 Comments

it would be nicer if you could have some explanation
Just include jQuery link after write code <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> Text inside $ symbol select all element with id = someid , disabled attribute of this id change to true
0

This could be more helpfull....

$("element").prop("id", "modifiedId");
//for boolean
$("element").prop("disabled", true);
//also you can remove attribute
$('#someid').removeProp('disabled');

1 Comment

Attributes and properties aren't exactly the same. For example, "id" is an attribute, not a property. You use .attr() to set or read it. See the paragraph "Attributes vs. Properties" under .prop() and .attr() in the jQuery docs. Also see Paul Rosania's answer above.
0
$('#yourid').prop('disabled', true);

Comments

0

If you are trying to use removeProp() for "checked", "disabled" or "selected", it might not work. To remove property use prop with 'false' parameter.

        $('.my-input').prop('disabled', true); //adding property
        $('.my-input').prop('disabled', false); //removing property

documentation: https://api.jquery.com/removeprop/

Comments

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.