I want to cancel any links and add extra attributes to each one. Below is how I've achieved this.
function anularEnlaces(){
$('nav a').each(function(){
var href = $(this).attr('href');
var id = $(this).attr('id');
$(this).attr('href','javascript:void(0)');
$(this).attr('hrefnot',href);
$(this).attr('nivel','uno');
$(this).attr('seccion','dos');
});
}
My question is, is that a valid/recommended way to add custom atributes?
Not like that. If you are using the HTML5 doctype you can use the new data-*
attributes, and then use the jQuery data
method:
$(this).data("hrefnot", href);
And HTML:
<a href="somewhere.html" data-hrefnot="something">A link</a>
If you don't need to store data on your tags initially, then it doesn't matter whether you use the HTML5 doctype or not (jQuery's data
method will still work).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With