Web Analytics Made Easy -
StatCounter Unable to reference array - wrong syntax? - CodingForum

Announcement

Collapse
No announcement yet.

Unable to reference array - wrong syntax?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unable to reference array - wrong syntax?

    Hi,

    I have a server side generated form which looks like:

    <input type='checkbox' class='col3' name='monitor[1]' value='1'><input type='text' class='col4 text' name='monitor_url[1]' value=''><input type='text' class='col5 text' name='monitor_string[1]' value=''><br><br><input type="button" class="negativeButton col5" value="Test Monitoring" onClick="javascript:win2col('/fetching/test_monitoring.php?url=' + monitor_url[1].value + '&string=' + monitor_string[1].value)"><br><br><br><div class='greyBold col1'>Key Facts</div><textarea class='col2 text' rows='3' name='fields[2]' >
    </textarea>

    <input type='checkbox' class='col3' name='monitor[2]' value='1'><input type='text' class='col4 text' name='monitor_url[2]' value=''><input type='text' class='col5 text' name='monitor_string[2]' value=''><br><br><input type="button" class="negativeButton col5" value="Test Monitoring" onClick="javascript:win2col('/fetching/test_monitoring.php?url=' + monitor_url[2].value + '&string=' + monitor_string[2].value)"><br><br><br><div class='greyBold col1'>Google Ranking</div><input class='col2 text' name='fields[3]' >

    Where I am having the problem is that when I click on the button, it says that monitor_url is not defined. Can anybody tell me where I am going wrong in referencing the form values and how to correct this?

    Many thanks

  • #2
    first off to reference a form you need to do it the following way

    document.formName.ElementName.Value

    or use the form array method...


    Also the naming convention you are using is also going to cause in error.... avoid using [] in your names since it is considered an array......

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

    Comment


    • #3
      Hi,

      Thanks for the reply.

      My problem is that these fields need to be an array, as they are dynamically generated and will point to database ids on the server when they are posted.

      Any idea how I can get the code to work using the [] syntax?

      Cheers

      Comment


      • #4
        don't now if this works but should be something like this
        document.formName['monitor_url[2]'].value
        or perhaps
        document.formName.monitor_url[2].value

        Comment


        • #5
          <input type="button" class="negativeButton col5" value="Test Monitoring" onClick="win2col('/fetching/test_monitoring.php?url=' + this.form.elements['monitor_url[2]'].value + '&string=' + this.form.elements['monitor_string[2]'].value)">

          For more info on square bracket notation, read this.
          Glenn
          vBulletin Mods That Rock!

          Comment


          • #6
            glenngv, you're a star, worked a treat!

            thank you very much indeed.

            Comment

            Working...
            X