Reputation: 6789
I'm adding HTML controls dynamically using java script. For instance, I have one button in my page. whenever I click the button, it will create on HTML control in UI page. so, I want to add click event for the controls. Is it possible to get all the UI controls ID?
Upvotes: 1
Views: 1058
Reputation: 25322
I don't think is a good idea obtain IDs of all elements, however:
var elements = document.querySelectorAll("[id]");
For a better cross browser support I suggest to you jQuery:
$("[id]");
Otherwise you have to get all elements and filter by id. Something like:
var allElements = document.getElementsByTagName("*");
var elements = [];
for (var i = 0, node; node = allElements[i++];)
if (node.id) elements.push(node);
Upvotes: 2
Reputation: 12405
The probably best solution to your problem is to use event delegate
Suppose you have jQuery (>= 1.7) introduced, things would be simply simple, just add a custom class such as control
to your control:
$(document).on(
'click',
'.control',
function(e) {
var control = e.target;
// Work you handler
}
);
Upvotes: 2
Reputation:
$('[id]')
returns all elements that have id set.
But Beware
this will be quite slow on a large dom
Upvotes: 1