dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: javascript doesn't retain change to textbox value

Hybrid View

  1. #1
    Join Date
    May 2010
    Posts
    17

    javascript doesn't retain change to textbox value

    I have a table with 15 rows containing a number of textboxes. If I change a numerical value in the last box in a row then, from the next row down to the end, the values must start from one more than that value, and increment in each row. Thus, eg, I want this change to occur when I alter the 2 to 5 in the second row:
    1 1
    2 5
    3 6
    4 7
    etc

    The cell code (generated with php) is:
    for ($i=0;$i<15;$i++){
    ........
    echo "<input name='$name1' id='$name1' value='$i'
    onchange='AdjustTracks($i);alert(\"here\");'>";
    ........
    }

    The javascript is:
    <head>
    <script type="text/javascript">
    function AdjustTracks(i){
    var box=document.getElementById("workData"+i+"_5");
    var endTr=parseInt(box.value);
    for (j=i+1;j<15;j++){
    endTr=endTr+1;
    box=document.getElementById("workData"+j+"_5");
    box.value=endTr;
    }
    return;
    }
    </script>
    </head>

    When I change one of the values, the javascript function is called and the other values change correctly. As soon as I release the alert, however, all the boxes return to their original values. What is happening?
    Thanks

  2. #2
    Join Date
    Mar 2011
    Posts
    1,160
    I suspect that your action in dismissing the alert() is causing the <form> to be "submitted" with no 'action' attribute set, so the page is reloading, which restores all of the default values. It's hard to say with just the code you posted. Try adding "return false;" after the alert() call in your 'onchange' setting.

  3. #3
    Join Date
    May 2010
    Posts
    17
    Rick
    Thanks. Your comment about the form submitting solved it. I realised I was clicking Enter to move off the changed field which resubmitted it. Thanks for the Christmas present.
    Tony

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles