Set attribute without value

JavascriptJqueryAttributesAttr

Javascript Problem Overview


How do I set a data attribute without adding a value in jQuery? I want this:

<body data-body>

I tried:

$('body').attr('data-body'); // this is a getter, not working
$('body').attr('data-body', null); // not adding anything

Everything else seems to add the second arguments as a string. Is it possible to just set an attribute without value?

Javascript Solutions


Solution 1 - Javascript

The attr() function is also a setter function. You can just pass it an empty string.

$('body').attr('data-body','');

An empty string will simply create the attribute with no value.

<body data-body>

> Reference - http://api.jquery.com/attr/#attr-attributeName-value

> attr( attributeName , value )

Solution 2 - Javascript

Perhaps try:

var body = document.getElementsByTagName('body')[0];
body.setAttribute("data-body","");

Solution 3 - Javascript

The accepted answer doesn't create a name-only attribute anymore (as of September 2017).

You should use JQuery prop() method to create name-only attributes.

$(body).prop('data-body', true)

Solution 4 - Javascript

You can do it without jQuery!

Example:

document.querySelector('button').setAttribute('disabled', '');

<button>My disabled button!</button>

> To set the value of a Boolean attribute, such as disabled, you can specify any value. An empty string or the name of the attribute are recommended values. All that matters is that if the attribute is present at all, regardless of its actual value, its value is considered to be true. The absence of the attribute means its value is false. By setting the value of the disabled attribute to the empty string (""), we are setting disabled to true, which results in the button being disabled. > > From MDN Element.setAttribute()

Solution 5 - Javascript

Not sure if this is really beneficial or why I prefer this style but what I do (in vanilla js) is:

document.querySelector('#selector').toggleAttribute('data-something');

This will add the attribute in all lowercase without a value or remove it if it already exists on the element.

https://developer.mozilla.org/en-US/docs/Web/API/Element/toggleAttribute

Solution 6 - Javascript

simply try this, it will definately work....

document.querySelector("audio").setAttribute("autoplay", "");

this will showed like below code;-

<audio autoplay>

</audio>

if you wrote like,

$("audio").attr("autoplay", "");

then, this will showed like below code;-

<audio autoplay="autoplay">

</audio>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid HellsingView Question on Stackoverflow
Solution 1 - JavascriptLixView Answer on Stackoverflow
Solution 2 - JavascriptCal McLeanView Answer on Stackoverflow
Solution 3 - JavascriptCharlieView Answer on Stackoverflow
Solution 4 - JavascriptLipESprYView Answer on Stackoverflow
Solution 5 - JavascriptBrent N.View Answer on Stackoverflow
Solution 6 - Javascriptveer vikramView Answer on Stackoverflow