---
title: "Get getElementById from variable using JavaScript"  
description: "Get getElementById from variable using JavaScript"  
author: "Anonymous User"  
published: 2013-08-16  
updated: 2013-08-17  
canonical: https://www.mindstick.com/forum/1395/get-getelementbyid-from-variable-using-javascript  
category: "javascript"  
tags: ["javascript"]  
reading_time: 2 minutes  

---

# Get getElementById from variable using JavaScript

Hi [developers](https://www.mindstick.com/articles/12875/blunders-you-must-avoid-while-hiring-java-developers-to-get-the-best-fulfilling-your-needs)!

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](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) I want to be able to [select](https://www.mindstick.com/forum/160534/orderby-then-select-vs-select-then-orderby-performance) the input of each, but only on [request](https://www.mindstick.com/blog/255/post-get-and-request-function-in-php) 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](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) (placed outside my [function as](https://www.mindstick.com/forum/212/can-be-declare-a-static-function-as-virtual) 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](https://www.mindstick.com/forum/331/can-anyone-help-me-out-how-to-use-session-concept) out on this on how to get the next input box value ?

Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)!

## Replies

### Reply by shreesh chandra shukla

Hi!

current_item is a string and when you use the + operator, it does concatenation instead of addition.

"1" + 1

"11"

If you use var current_item = 1;, it'll be an integer and you can add it by doing +1

Also,

var current_item = "1";

current_item = current_item*1 + 1;

2

When you multiply by 1, it casts it to an int, then adds 1, which results in 2 as you would expect.

thanks


---

Original Source: https://www.mindstick.com/forum/1395/get-getelementbyid-from-variable-using-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
