Link to home
Start Free TrialLog in
Avatar of trickyidiot
trickyidiot

asked on

JS+Form - onClick populate text field with checkbox values - not working

Trying to get checkbox values to populate a text field (soon to be a hidden text field on an onClick event

I'm getting an error saying the function is not defined... help?

<script type="text/javascript">
function stringTogether(form) {
      // Checking if at least one period button is selected. Or not.
      var total=""
      for(var i=0; i < document.form1.statusList.length; i++){
            if(document.form1.scripts[i].checked)
                  total +=document.form1.scripts[i].value + ","
      }
      document.form1.strung.value = total;
}
</script>
<form name="form1">
<strong>Select which user status levels you would like to exclude:</strong>
<br /><br />
<%
'' get all status codes available
SET rsStatus = server.createObject("ADODB.Recordset")
sql = "sql query removed for experts exchange - not necessary"
rsStatus.Open sql,dbConn,1

While NOT rsStatus.eof
%>
<input type="checkbox" name="statusList" value="<%=rsStatus("id")%>"> <%=rsStatus("title")%><br />
<%
rsStatus.movenext
wend
%>
<br />
<input type="text" name="strung" value="">
<input type="button" onClick="javascript:stringTogether(this);" name="submit" value="submit">
</form>
Avatar of amit_g
amit_g
Flag of United States of America image

What is scripts? Change

            if(document.form1.scripts[i].checked)
                  total +=document.form1.scripts[i].value + ","

to

            if(document.form1.statusList[i].checked)
                  total +=document.form1.statusList[i].value + ","
Avatar of trickyidiot
trickyidiot

ASKER

Good catch, however I am still receiving the same error
What error and when do you get it? What browser are you using? Change

<input type="button" onClick="javascript:stringTogether(this);" name="submit" value="submit">

to

<input type="button" onClick="stringTogether(this);" name="submit" value="submit">

as javascript: is not needed. To further debug change

function stringTogether(form) {

to

function stringTogether(form) {
alert("Hello");

do you get this alert?
Also, you will have a problem if your result set only returns one result. The statement document.form1.statusList.length will be undefined, because you will only have one checkbox rendered by your ASP code.
ok, so if my function is bunk, do you know of a better way to achieve what I'm trying to do?
ASKER CERTIFIED SOLUTION
Avatar of amit_g
amit_g
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is there more content to this page? I feel like there might be an error in your ASP code that is causing portions of the page not to render.