item value

Discussion in 'JavaScript' started by tirso, Sep 1, 2008.

  1. #1
    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
     
    tirso, Sep 1, 2008 IP
  2. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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.
     
    xlcho, Sep 1, 2008 IP
  3. tirso

    tirso Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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):
     
    tirso, Sep 2, 2008 IP
  4. krzyk

    krzyk Peon

    Messages:
    61
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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" />
     
    krzyk, Sep 2, 2008 IP
  5. tirso

    tirso Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi krzyk,

    Thank you so much, it works!. You are great!

    Have a nice day.
     
    tirso, Sep 3, 2008 IP