Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using custom attributes valid?

Tags:

html

jquery

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?

like image 948
Toni Michel Caubet Avatar asked Dec 16 '22 07:12

Toni Michel Caubet


1 Answers

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).

like image 151
James Allardice Avatar answered Dec 29 '22 13:12

James Allardice