kchow23
kchow23

Reputation: 111

getElementById value not working

I am trying to pass a value from javascript to a field on a form. However, it doesn't seem to work. The html code is:

<html>
  <head>
     <title>Insert title here</title>
  </head>
  <body onload="splitter()">
    <p>Are you sure you want to delete?</p>
    <form name="myform" action="http://localhost:8080/EfsiDatabase/timer"
          method="post">
      <input type="hidden" name="action" value="delete">
      <input type="hidden" name="index" id="index">
      <input type="submit" name="submit" value="Delete">
    </form>
  </body>
</html>

On the javascript file, the issue I am having is the last line where I am trying to assign the value to the index input field. No value returns when I do this.

<script type="text/javascript">
function splitter() 
{
    var str=window.location.search;
    var replaced=str.replace("?entry=","");
    var n=replaced.split("&entry=");
    var i=0;
    var form = document.forms['myform'];
    while(n)
    {
       var x=n[i].split("%7C%7C");

       var e1 = document.createElement("input");
       e1.type = "hidden";
       e1.name = "staff"+i;
       e1.value = x[0];
       var e2 = document.createElement("input");
       e2.type = "hidden";
       e2.name = "date"+i;
       e2.value = x[1];
       var e3 = document.createElement("input");
       e3.type = "hidden";
       e3.name = "project"+i;
       e3.value = x[2];
       var e4 = document.createElement("input");
       e4.type = "hidden";
       e4.name = "task"+i;
       e4.value = x[3];
       var e5 = document.createElement("input");
       e5.type = "hidden";
       e5.name = "notes"+i;
       e5.value = x[4];
       var e6 = document.createElement("input");
       e6.type = "hidden";
       e6.name = "hours"+i;
       e6.value = x[5];

       form.appendChild(e1);
       form.appendChild(e2);
       form.appendChild(e3);
       form.appendChild(e4);
       form.appendChild(e5);
       form.appendChild(e6);
       i++;
    }
    document.getElementById("index").value=i+1;
}
</script>

How can I get a value with this method? Thanks for the help.

Upvotes: 0

Views: 402

Answers (1)

TechBytes
TechBytes

Reputation: 555

Instead of

while(n)
{
    var x=n[i].split("%7C%7C");
    // ...
}

Please use below code:

while(n[i])
{
    var x=n[i].split("%7C%7C");
    // ...
}

Upvotes: 2

Related Questions