PoseLab
PoseLab

Reputation: 1899

refer to the id of an element

I have several links to play videos in a player with javascript. What would be the best way to refer to the id of the player from the link to make these links accessible and syntactically correct? It would be similar to using the for attribute in a label element.

    <a href="video" rel="playerid">video</a>
    <a href="video" data-playerid="playerid">video</a>
    <a href="video" itemref="playerid">video</a>

Upvotes: 2

Views: 85

Answers (2)

T.J. Crowder
T.J. Crowder

Reputation: 1075447

Your second one, using the data-* attribute:

<a href="video" data-playerid="playerid">video</a>

I don't believe your first example using rel works if the player ID varies, because although you can add to the list of valid values (thank you for pointing that out!), the rel still expresses the kind of relationship (e.g., rel="icon" or rel="canonical"), not the ID of something (which is the URI in the href).

Your third one uses a custom attribute without a data- prefix, making the HTML invalid.

So I think the second one, the data-*, is the way to go.

Upvotes: 2

mkey
mkey

Reputation: 535

I would use something like this

<a href="/js_error.html" onclick="Play('id'); return false;">...</a>

This is usually my preferred method of mixing javasript and links. If something fails with js on user end, the error page will be shown. The "return false;" bit should keep you on the page, as long as the Play() function executes properly. In my experience this is a durable and compatible way of handling such matters.

Regards

Upvotes: 0

Related Questions