12

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?

user247702
  • 23,641
  • 15
  • 110
  • 157
RetroCraft
  • 327
  • 1
  • 2
  • 13

3 Answers3

9

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.

James Mason
  • 4,246
  • 1
  • 21
  • 26
5

You can use data-. So in your HTML, you'd define it like:

<span class="navLink" data-link="google.com">click me</span>

And then via jQuery you just do:

window.location.href = "/index.php?page=" + $(this).data("link");
Shahar
  • 1,687
  • 2
  • 12
  • 18
2

pure js:

el.setAttribute('value', the-value);

jquery:

$(el).attr('value', the-value);

but better make something easily recognisable like my-value as a name of your attributes, to avoid conflicts and make them easier to notice in the code

Nik Terentyev
  • 2,270
  • 3
  • 16
  • 23