SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist vhogarth's Avatar
    Join Date
    Nov 2003
    Location
    Taxachussets
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript variable in loop

    Im having a little trouble getting the desired reslut with this snippet of code.
    Im trying to cycle through a set of numbers and use the current number to be used as the name of a particular text box i want to change the value of. HOpe that made some sort of sense. Here's the code:

    Code:
    for(i=0; i<10; i++)
    {
    //i want varname to be input_1,input_2,input_3,etc
    var_name="input_"+i;
    
    //i want this to set the value of the textbox to 00
    document.form1.var_name.value="00";
    
    
    }
    When I tried my example. It didnt work. Any thoughts?

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In js code, strings are written with quotes around them and "references" to html elements are written without quotes around them. You write:

    window.opener.document

    not

    "window"."opener"."document"

    Don't confuse how strings are displayed(i.e. without quotes) with the way they are written in js code. document and "document" are not the same thing in js. To demonstrate that difference, run this code:
    Code:
    alert(document)
    alert("document");
    When you have an id or name that is a string, you can use a js array to obtain the element. For instance, you can write:

    document.forms["myFormName"].elements["myElementName"].disabled = false;

    The forms[] array contains all the forms on the page, and you can use numerical index values where 0 is the first form on the page, 1 is the second form on the page, etc.; or you can use a string as the index value, which is the name of the form. The elements[] array contains all the user interaction elements in a form. Once again, you can use numerical indexes for the form elements, e.g. 0, 1, 2; or you can use a string as the index value, which is the element's name. Remember that in js, strings have quotes around them. Since the index can be a string, you can also use a variable that contains a string instead, e.g.:

    var theForm = "userInfo";
    document.forms[theForm]....

  3. #3
    SitePoint Evangelist vhogarth's Avatar
    Join Date
    Nov 2003
    Location
    Taxachussets
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud
    In js code, strings are written with quotes around them and "references" to html elements are written without quotes around them. You write:

    window.opener.document

    not

    "window"."opener"."document"

    Don't confuse how strings are displayed(i.e. without quotes) with the way they are written in js code. document and "document" are not the same thing in js. To demonstrate that difference, run this code:
    Code:
    alert(document)
    alert("document");
    When you have an id or name that is a string, you can use a js array to obtain the element. For instance, you can write:

    document.forms["myFormName"].elements["myElementName"].disabled = false;

    The forms[] array contains all the forms on the page, and you can use numerical index values where 0 is the first form on the page, 1 is the second form on the page, etc.; or you can use a string as the index value, which is the name of the form. The elements[] array contains all the user interaction elements in a form. Once again, you can use numerical indexes for the form elements, e.g. 0, 1, 2; or you can use a string as the index value, which is the element's name. Remember that in js, strings have quotes around them. Since the index can be a string, you can also use a variable that contains a string instead, e.g.:

    var theForm = "userInfo";
    document.forms[theForm]....
    Thanks 7stud! Excellent explanation. I got it figured out first try. Works like a charm. Thanks a mil.


Bookmarks

Posting Permissions

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