Problem storing value in textbox array

Please can someone assist me with the solution to this problem. I need to store the result of the hdvest array. What is wrong with my code.

function getval() {
    var e = document.getElementsByName("investresult[]");
    for(var i = 0; i < e.length; i++) {
      var f = e[i];
      alert(f.value);
      document.getElementByName("hdinvest[i]").value = f.value;
    }
}

There’s an ‘s’ missing from the second use of document.getElementsByName()

Also, to get your code formatting to work, you can either highlight it and click on the </> icon on the editor toolbar, or use three backticks ( ``` ) on the line before and the line after your code.

The problem is yet to be resolved

you have no element with the name hdinvest[i].

What HTML are you applying this JS to? Can you post the relevant content here so we can make better sense of what should be happening.

i do have element bearing that name

Does the alert show what you expect it to?

A NodeList (as returned by getElementsByName()) doesn’t have a value property, and assigning one won’t have any effect per se. Did you mean something like

document.getElementsByName("hdinvest[]")[i].value = f.value;

Note that naming input elements with square brackets doesn’t have any effect on the JS, it only makes PHP recognise them as an array within the action script.

            <SELECT class="form-control invest" name="invest[]" id="invest[]" >
                <OPTION value="">Select Investigation</OPTION>
                <?php 
                $query2="select laboption,fee from investigation group by laboption order by laboption asc";
                $resultquery2=mysql_query($query2) or die(mysql_error());
                while($row2=mysql_fetch_array($resultquery2)){
                extract($row2);
                ?>
                <OPTION value="<?php echo $fee;?>"><?php echo $laboption;?></OPTION>
                <?php }?>
                
            </SELECT>
   <INPUT class="form-control" type="text" name="investresult[]" id="investresult[]" placeholder="Investigation Result" onchange="getval();"/>

Can you do a rendered version of the resulting HTML from that? I don’t think anyone is going to try interpreting your PHP to work out what HTML comes out of it, and then has some JS applied to it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.