0
function addition(){

     var count = 2;
     for(var i = 0 ; i <=count ; i++ ){
           var val = document.getElementById('num');
           alert(val.value);
     }

}
<input type="text" id="num" name="name[]">
<input type="text" id="num" name="name[]">
<input type="submit" id="num"  onclick="addition">

I Want to get values of input with the help of single id or name please help me how to get it..

6
  • Your question's title and code are not in sync. Commented Mar 29, 2018 at 6:38
  • 2
    It's not a good solution to use same id , Id should be unique Commented Mar 29, 2018 at 6:38
  • Ur count and i are same 0 is not less than 0 as i know Commented Mar 29, 2018 at 6:41
  • @StupidKid It’s <=, not <. Commented Mar 29, 2018 at 6:42
  • Just use class instead of id and then querySelectorAll instead of getElementById Commented Mar 29, 2018 at 6:43

4 Answers 4

2

There can only be one id in a page. Use class, element or attribute selectors instead.

Using a class for the inputs (the button doesn't need a class/id in this case), you can get all elements using Document.getElementsByClassName(), and than you can iterate the collection:

function addition(){
     var val = document.getElementsByClassName('num');
         
     for(var i = 0 ; i < val.length ; i++ ){
           console.log(val[i].value);
     }

}
<input type="text" class="num" name="name[]">
<input type="text" class="num" name="name[]">
<input type="submit" onclick="addition()">

Sign up to request clarification or add additional context in comments.

Comments

1

There are a couple of problems,

  • onclick of submit button is not invoking addition.

i.e. add () after addition

<input type="submit" id="num" onclick="addition()"> 
  • Loop's condition is invalid, it will ensure that there is never an iteration for(var i = 0 ; i <=count ; i++ ){ since your count is 0.

Increase the count to 2

 var count = 2;
  • document.getElementById will only return one element since Ids are supposed to be unique.

Use data-id (or class or any other data- attribute) instead and use querySelectorAll to query those elements

Demo

function addition() {
  var allInputs = document.querySelectorAll("input[data-id='num']");
  for (var i = 0; i < allInputs.length; i++) {
    var val = allInputs[i];
    console.log(val.value);
  }
}
<input type="text" data-id="num" name="name[]" value="1">
<input type="text" data-id="num" name="name[]" value="2">
<input type="submit" id="num" onclick="addition()"> 

Comments

0

if this is your code, then id cannot be duplicated. you can have multiple fields by same class name but Id should be different.

Comments

0

For javascript one id for one element is a good practice. If you want to get all value then you can use class attribute. So you also can use document.querySelectorAll like this -

<input type="text" class="num" name="name[]">
<input type="text" class="num" name="name[]">
<input type="submit" onclick="addition()">

function addition(){
     var val = document.querySelectorAll('.num');

     for(var i = 0 ; i < val.length ; i++ ){
           console.log(val[i].value);
     }

}

1 Comment

Thanks mate you saved my life <3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.