Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<img> Attributes

I've got some java script code that implements a kind of slide show. It uses a series of img tags as a table to control its actions. I probably used some code I found some where as a basis. Anyway, the img tags contain a data-img attribute which I can't find in any definition of the img tag. Now I find I need to add more data to the img tags. So my questions are:

1) Is the data-img actually a real attribute or something adhoc?

2) Can I invent yet more attributes?

3) What is the danger of using such attributes?

like image 380
Mike D Avatar asked Feb 24 '26 00:02

Mike D


1 Answers

1) It's valid in HTML 5; it is an invalid attribute (that will, as far as I know, still work in all browsers, it but will break validation) in HTML 4

2) As far as I am aware, you can add arbitrary attributes and query them in Javascript, but chances are those attributes will not survive certain DOM manipulation and other operations where the browser creates markup - it's possible the invalid attributes will simply be dropped

3) Your pages will no longer validate.

Background: Custom attributes - Yay or nay?

like image 152
Pekka Avatar answered Feb 26 '26 13:02

Pekka