user2413244 user2413244 - 9 days ago 7
jQuery Question

JQUERY: save and restore attribute data

I want to get/save all the elements that have attribute "tabindex".

Then, when the user clicks one element to open the popup, I need to set all the elements to "tabindex = -1" so when the user hits the tabkey they won't jump to all other fields that have "tabindex".

After the user closes the popup I need to restore all the tabindexes to each element.

So, for "tabindex = -1" I use:

$("*[tabindex]").each(function (i) { $(this).attr('tabindex', '-1'); });


But how can I save and the restore back for each element the "tabindex" was before?

Answer

Use .data() to save per-element data.

$("*[tabindex]").each(function (i) { $(this).data('tabindex', $(this).attr('tabindex')).attr('tabindex', '-1'); });

And then to restore:

$("*[tabindex]").each(function (i) { $(this).attr('tabindex', $(this).data('tabindex')); });