Skip to content
Advertisement

Set attribute without value

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

JavaScript

I tried:

JavaScript

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

Advertisement

Answer

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

JavaScript

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

JavaScript

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

attr( attributeName , value )

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement