forum

Home / DeveloperSection / Forums / Get getElementById from variable using JavaScript

Get getElementById from variable using JavaScript

Anonymous User282016-Aug-2013
Hi developers! 

I have 10 input boxs on my page.

<input type="text" id="box1">

<input type="text" id="box2">

<input type="text" id="box3">

<input type="text" id="box4">

<input type="text" id="box5">

<input type="text" id="box6">

<input type="text" id="box7">

<input type="text" id="box8">

<input type="text" id="box9">

<input type="text" id="box10">

Using JavaScript I want to be able to select the input of each, but only on request using JavaScript.

I have used the following code:

document.getElementById("box1").value

which returns the value (which I want)

However I want to be able to do something with it then request the next item then do something with that.

So I have created a dummy Variable (placed outside my function as I don't want to reset the value back to 1 each time the function is called:

var current_item = "1";

I then wanted to be able to select a item using this variable and I have used it like:

document.getElementById("box" + current_item).value

current_item = current_item + 1;

however it's not working. If I alert the current_item variable it returns undefined.

If I add the current_item variable to my function it works and does what I want it to do, but keeps resetting back to 1 (as the function is recreating the variable).

Can anyone help me out on this on how to get the next input box value ?

Thanks in advance!


Updated on 17-Aug-2013
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By