Mr. Fox
Mr. Fox

Reputation: 328

more than one elementID parameter HTML DOM getElementById() Method?

can you have more than one elemenID parameter HTML DOM getElementById() Method?and how? Base on w3schools this is the syntax of HTML DOM getElementById() Method but it deosn't show in a situation were in you need to call mutiple ID's.

Syntax document.getElementById(elementID)

enter image description here

I need to get the element of label 1-6 to form this output:

199 Freestone Road, Sladevale QLD 4370, Australia

Upvotes: 0

Views: 132

Answers (1)

Quentin
Quentin

Reputation: 943217

No. getElementById only allows a single element to be specified.

The more recent (i.e. less well supported) querySelectorAll method accepts a selector, which can include multiple ids.

var nodeList = document.querySelectorAll('#one, #two, #three, #four, #five, #six');

But you would probably be better off just calling getElementById in a loop (you could store your id values in an array).

Better still would be to adjust the markup. If you have a group of elements, then use some kind of grouping mechanism.

e.g.

 // Make them all members of a class
 var nodeList = document.getElementsByClassName('foo');

 // Put them all in a container
 var nodeList = document.getElementById('someContainer').getElementsByTagName('label');

Upvotes: 2

Related Questions