Hi to all I use javascript to get the value of my input tag. My problem now is instead of using tag name, I want to use a variable to get that value. The below code ("itemval1" is the name of my input tag). The reason why is I have 15 input tag and I want to get it by looping a number and contatenate it to the end of string. ctr = 1 instead of itemval1 , itemval+ctr valitem1 = document.ce.itemval1.value; this will works valitem1 = document.ce.itemval+ctr.value; doesn't work Thanks in advance
The best way to what you want is to use an array of elements, not 15 separate elements. The code above won't work unless you get the element with getElementById() or something similar. You cannot concatenate the name of the element when trying to access it trough DOM.
hi xlcho, Thank you so much for your immediate replied. I actually I used array on my first attempt but it doesn't work, I could not get the value of my array from php into javascript. Please see my code below. I putted the value of array in imput tag as hidden and get it in javascript. The purpose of this is if the user input value. let say ("2") then get the value ("500"). The values of array is not constant this is only an example. <? $array = new array("1"=>"200","2"=>"500"); ?> PHP: <td><input type="hidden" name="itemval1" value="<?=$array?>"/> <input name="discountitem" type="text" class="style5" id="discountitem" size="10" onchange="ChangeFieldName(this)"/></td> this inside of my validation.js function ChangeFieldName (val) { valitem1 = document.ce.itemval1.value; alert(valitem1[0]); } Code (markup):
You could try: document.getElementById("itemval" + ctr); But you have to give your input boxes id like "itemval1", "itemval2" (it is best to give the same id and name) e.g: <input type="hidden" name="itemval1" id="itemval1" />