Denis Kralj
Denis Kralj

Reputation: 633

getting a list of id's for elements in a html document

So i have this html:

 <select id="CompanyMultiSelect" name="CompanyMultiSelect" multiple="multiple">
</select>

and i have JavaScript that dynamically fills it up with data. The data is filled with, among other things, inputs that have id's. They are generated dynamically, so i don't know them until the page is loaded.

i need to select the id of all the inputs within the select so that i can parse their values to another location, and i want to do it with either JS or jQuery.

how would i go about selecting all ids as an array to use later on?

EDIT1:

The select segment is a placeholder for a plugin that creates a dropdown menu filled with checkboxes, so the fact that inputs are within a select shouldn't bother you :)

Upvotes: 0

Views: 355

Answers (2)

roel
roel

Reputation: 2003

getElementsByTagName returns an array of elements.

Upvotes: 0

Viktor S.
Viktor S.

Reputation: 12815

Suppose you already have a jquery, so you can do something like this:

var ids = [];
$("parent_element_selector").find("input[type=checkbox]").each(function(){
   ids.push(this.id);
});
//here ids will be filled with ids of checkboxes.

Please note that parent_element_selector can't be #CompanyMultiSelect as select element can't contain inputs, so your plugin will create a new wrapper element. You need to figure out how you can get it with firebug or dev tools

Upvotes: 1

Related Questions