www.webdeveloper.com
Results 1 to 4 of 4

Thread: A loop is probable necessary for this

Hybrid View

  1. #1
    Join Date
    Jan 2011
    Posts
    5

    A loop is probable necessary for this

    I want to simplify this code. I want to set everything inside a form of 100 input boxes to zero without making 100 lines of code. What would be the best approach.

    I have tried this...
    for (a=0; a<=100; a=a+1){ qtr1("pr"[a].value=0}

    I have also tried this....
    for (a=0; a<=100; a=a+1){ qtr1("pr"+a).value=0}



    qtr1.pr1.value=0;
    qtr1.pr2.value=0;
    qtr1.pr3.value=0;
    qtr1.pr4.value=0;
    qtr1.pr5.value=0;

    I am not sure what I am doing wrong.

    Thank you in advance for your reply. I appreciate your time.

  2. #2
    Join Date
    Jun 2007
    Location
    Washington D.C. Metro
    Posts
    76
    Code:
    var inputs = document.forms.qt1.getElementsByTagName('input');
    for(var i=0, len=inputs.length; i<len; i++){
    	inputs[i].value=0;
    }

  3. #3
    Join Date
    Jan 2011
    Posts
    5
    Thank you Trandrus, your help is appreciated. It is perfect.

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    As an alternate, you could have used getElementsByName(), if the names are continuously indented (from pr1, pr2... to prwhichever)
    Code:
    var inp, i=1;
    while(inp=document.getElementsByName('pr'+[i++])){
    inp.value=0;
    }

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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