JavaScript function which takes multiple ids and returns multiple elements

This tutorial shows how to implement a javascript function which takes multiple ids and returns multiple elements

document.getElementById() only supports one name at a time and only returns a single node not an array of nodes. You have several different options:

You could implement your own function that takes multiple ids and returns multiple elements.
You could use document.querySelectorAll() that allows you to specify multiple ids in a CSS selector string .
You could put a common class names on all those nodes and use document.getElementsByClassName() with a single class name.
Examples of each option:

 

or:

// put a common class on each object

 

or:

 

 

 

Thanks for your reading. Please do not forget to leave a comment.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.