Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a custom attribute to a <span> and read it in JavaScript? [duplicate]

I want to have a jQuery/PHP site that uses <span> elements for the navigation links. I need to have a value attribute to use

$(".navLink").click(function() {
    window.location.href = "/index.php?page=" + this.value //or whatever
});

I can't use this.id because it conflicts with other HTML elements. Is there any way to create and use a "dummy" HTML attribute?

like image 456
RetroCraft Avatar asked Jun 18 '14 01:06

RetroCraft


1 Answers

Use a data-* attribute. Something like data-href would be good. jQuery gives you a nice interface for reading the values.

this.data('href')

will read the value of

data-href="some.value"

See http://api.jquery.com/jquery.data/ for more.

like image 129
James Mason Avatar answered Sep 30 '22 13:09

James Mason