get all html elements (the names or ids) in order of appearance on a form using javascript
At work I have a a few html forms (classic asp back) with normal html elements and classic asp dynamic check boxes.
We want to do reporting on this and create pivot tables so I need to provide a list of all the names or ids (names is probably better) of html objects (static and dynamic) in the order they appear on the form. Some are hidden until the user interact开发者_JAVA百科s with depending object (we want those too).
I found multiple posts about getting all withing a class but could find this exact answer because the order is very important!
jQuery is already being used on the page so that would be fine to use.
Thanks in advance!
Use either option to create a list of element IDs/names within #myForm
:
var orderedArrayId = [];
$("#myForm *[id]").each(function(){
orderedArrayId.push(this.id);
})
var orderedArrayName = [];
$("#myForm *[name]").each(function(){
orderedArrayName.push(this.name);
})
Instead of using .push(this.id)
, you can also use .push(this)
, if you want to get a list of DOM elements.
After running this code, you can loop through all elements:
$.each(orderedArrayId, function(index, element){
//element holds the ID of the element
// (or a reference to the DOM element, if you've used `.push(this)` instead
// of `.push(this.id)`)
//Do something
});
精彩评论