0

I have a dynamically created page with repeated elements, like so:

<input type="file" name="file1">
<input type="file" name="file2">
<input type="file" name="file3">

I would like to add some data to each of these elements for use in JavaScript. Originally I used "spare" attributes that while valid on an input tag were not valid on a file type input tag, eg, size. I have now added it to the class attribute, but have to use a regex to get it out again. Neither of these methods seem very satisfactory, is there a better way?

2

4 Answers 4

4

Quite often I see data added through attributes that are prefixed with data-, for instance:

<input type="file" name="file1" data-filesize="871">

jQuery even has function to conveniently read that information, for instance like this:

var filesize = $('input[name="file1"]').data('filesize');

And, to write the data, attr can be used:

$('input[name="file1"]').attr("data-filesize", filesize );

See also: HTML 5 data atributes

Sign up to request clarification or add additional context in comments.

Comments

1

You can use attributes with a name starting with "data-"

Comments

1

Use the data attribute: <input data-something="somevalue" />

Comments

0

with attribute start with data- you can use your own other name appending to it.

Go through this examples

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.