Referencing list boxes with dynamically assigned names

Results 1 to 5 of 5

Thread: Referencing list boxes with dynamically assigned names

  1. #1
    Justin Wong Guest

    Default Referencing list boxes with dynamically assigned names

    I have an asp form in which I populated a number of list boxes. The exact number of list boxes is determined by the number of records in a database table, and the name of each list box is pulled from a field in the table. All this was done using server side VBscript.<BR> <BR>The difficulty I am having now is: I need to write a client-side Javascript to check the value of each of these list boxes before the user submits the form, and I don&#039t know how I can reference these list boxes because how many of them their are and their names are not pre-defined. Any suggestion on this would be greatly appreciated. (I have posted the same questions in other newsgroups but received not answer. Is it because it cannot be done?)<BR><BR>Thanks!

  2. #2
    Joseph Hatcher Guest

    Default RE: Referencing list boxes with dynamically assigned names

    Well I&#039ll give it a shot.<BR><BR>First off each list box is determined by the number of records in a table. So I assume you are looping through the table dynamically creating the List Boxes. While you are doing this also have a counter to count how many you will have and append the counter number to the List Box so later you know how to reference it.<BR><BR><BR>&#060;%Counter = 1<BR>DO While Not RS.EOF%&#062;<BR>&#060;INPUT type="button" value="&#060;%=RS("info")%&#062;" name="button&#060;%=Counter%&#062;"&#062;<BR>&#060 ;%Counter=Counter+1<BR>RS.MoveNext<BR>loop%&#062;< BR><BR><BR>You can call these what ever name you want. Then in your client-side Javascript you could check to see which is referenced.<BR><BR>If this does not help please provide more detail :)<BR><BR>Joseph Hatcher<BR><BR>

  3. #3
    Justin Wong Guest

    Default RE: Referencing list boxes with dynamically assigned names

    Hi Joseph:<BR><BR>Thanks for the help, but I do need more help on this! Specifically, I have created n number of listboxes, as indicated in the codes below:<BR><BR>&#060;%Counter = 1<BR>DO While Not RS.EOF<BR>options="&#060;select name=&#039" & RS("ProdID") & "&#039 size=1&#062;" <BR>For j = 0 To 99<BR> options = options & "&#060;option value=" & j & "&#062;" & j & "&#060;/option&#062;"<BR>next<BR>options = options & "&#060;/select&#062;"<BR>Counter=Counter+1<BR>RS.MoveNext< BR>loop<BR>RS.Close<BR>%&#062;<BR><BR><BR>Let&#039 s say I have a &#039submit&#039 button, and when user clicks the submit button, I need a client side Javascript to check the value of all the list boxes created using the above script, such as:<BR><BR>&#060;input type="button" value="Check Value" name="Button1"<BR> OnClick="chkValue(this.form)"&#062;<BR><BR>&#060;s cript language=javascript&#062;<BR>&#060;!--<BR>function chkValue(objForm) {<BR> Add all the selected values of all the option boxes. <BR>}<BR>//--&#062;<BR>&#060;/script&#062;<BR><BR>The problem is: Within the chkValue function, how to I refer the counter variable created by the server-side script (so that I can loop over all the option boxes), and how do I refer the name of all the list boxes (which equals RS("ProdID"), but the recordset is already closed and cannot be referenced to by the client-side script)?<BR><BR>I hope this clarifies the challenge I&#039m facing. I really would appreciate some help!

  4. #4
    Joseph Hatcher Guest

    Default RE: Referencing list boxes with dynamically assigned names

    Just drop the following code into an asp page, understand what it does then modify it to fit your need.<BR><BR>If it still does not make sense let me know :)<BR> <BR><BR><BR><BR>&#060;%@ Language=VBScript %&#062;<BR>&#060;script language=javascript&#062;<BR>function change_array(the_value,the_name)<BR>{<BR> myarray[the_name]=the_value<BR>}<BR>function Check_val()<BR>{<BR>total=0;<BR>for (i=0;i&#060;document.select_boxes.Counter.value;i+ +)<BR>{<BR>total=total+myarray[i];<BR>}<BR>alert(total)<BR>}<BR>//--&#062;<BR>&#060;/script&#062;<BR>&#060;html&#062;<BR>&#060;body&#06 2;<BR><BR>&#060;form name=select_boxes&#062;<BR>&#060;%Counter=0<BR>Do While Counter&#060;5%&#062;<BR>&#060;select name=&#039&#060;%=Counter%&#062;&#039 size=1 onChange="change_array(this.selectedIndex,&#060;%= Counter%&#062;)"&#062;<BR>&#060;%<BR>For j = 0 To 99%&#062;<BR>&#060;option value=" &#060;%=j%&#062; "&#062;&#060;%=j%&#062;&#060;/option&#062;<BR>&#060;%<BR>next<BR>Counter=Counter +1%&#062;<BR>&#060;/select&#062;<BR>&#060;%Loop%&#062;<BR>&#060;input type="hidden" name="Counter" value=&#060;%=Counter%&#062;&#062; <BR><BR><BR>&#060;INPUT type="button" value="Button" id=button1 name=button1 onclick="Check_val()"&#062;<BR>&#060;/form&#062;<BR><BR>&#060;script language=javascript&#062;<BR>var myarray=new Array();<BR>for (i=0;i&#060;document.select_boxes.Counter.value;i+ +)<BR>{<BR>myarray[i]=0;<BR>}<BR>&#060;/script&#062;<BR>&#060;/body&#062;<BR>&#060;/html&#062;


  5. #5
    Join Date
    Dec 1969
    Posts
    33

    Default RE: Referencing list boxes with dynamically assigned names

    Dont know if this is exactly what you need but...<BR><BR>If all you need to do is go through all the selects in a form (assuming thats all thats in form), this JS code will do it. You dont even need to know the name you gave it via asp.<BR><BR>say you want to add all the selects in form &#039f&#039 this function will add up all their values together.<BR><BR>function loop_through()<BR>{<BR> sum=0<BR> for (j=0;j&#060;document.f.length;j++)<BR> sum += eval(document.f.elements[j].value);<BR><BR> alert(&#039The sum of all selects is &#039 + sum);<BR>}

Posting Permissions

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