Set attribute without value

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 )

difference between prop() and attr() in jQuery and when to use attr() and prop() [duplicate]

from docs The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes. example … Read more

How to change href of tag on button click through javascript

Without having a href, the click will reload the current page, so you need something like this: <a href=”#” onclick=”f1()”>jhhghj</a> Or prevent the scroll like this: <a href=”#” onclick=”f1(); return false;”>jhhghj</a> Or return false in your f1 function and: <a href=”#” onclick=”return f1();”>jhhghj</a> ….or, the unobtrusive way: <a href=”#” id=”abc”>jhg</a> <a href=”#” id=”myLink”>jhhghj</a> <script type=”text/javascript”> … Read more

.prop() vs .attr()

Update 1 November 2012 My original answer applies specifically to jQuery 1.6. My advice remains the same but jQuery 1.6.1 changed things slightly: in the face of the predicted pile of broken websites, the jQuery team reverted attr() to something close to (but not exactly the same as) its old behaviour for Boolean attributes. John … Read more