Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
89
rated 0 times [  94] [ 5]  / answers: 1 / hits: 154313  / 12 Years ago, wed, october 31, 2012, 12:00:00

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?


More From » jquery

 Answers
4

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 )



[#82264] Monday, October 29, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
annaw

Total Points: 18
Total Questions: 91
Total Answers: 98

Location: Guam
Member since Fri, Jun 18, 2021
3 Years ago
;