How To Get Multiple Elements By Id Using Js

In this tutorial, we are going to find out, that javascript get multiple elements and how to get multiple elements by id using js.Sometimes we need to get the value of the same ID of the multiple elements.But We cannot use the ID to get multiple elements because ID is only used to get the First Element if the same ID is declared to the multiple elements.

javascript get multiple elements

Javascript Get Multiple Elements-

In above example, we have taken three input all of them have different-different value but same id. But when you take their value only first input value is coming not other. So in order to get the multiple elements, we need to use a class or we can also use name attribute. Let see one by one.

get multiple elements using js.

Javascript Get Multiple Elements By class:

First, we look at the class attribute which is used instead of id for getting multiple elements using js.Let’s take an example. First their HTML code.

 

We just replace id by class attribute. Now the Js script which is used to get multiple elements.

Now the complete code to see how it works.

Javascript Get multiple elements by name:

All the things are same the only change is to replace the class by the name attribute.

RESULT:

value-1value-2value-3value-4

Conclusion:

So this is how we can achieve the task of getting the value of multiple elements using js.If you have any query related this post feel free to comment on the comment box.Do not forget to share this post because someone else also needs this. Also, like our Facebook Page.

 

Also Read:

  1.  PHP Tutorial – PHP Introduction
  2.  Php Form Submit Using Post And Get Method

Leave a Reply

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