View Full Version : Simple FOR loop question from js newbie
I haven't had much experience with JS but have to convert all my client-side vbs to js to work with Netscape. Unfortunately, I can't get out of it (not b/c of js but b/c netscape sucks).
Anyway, I have this:
Sub b1_onclick
var x=" ";
for(var i=0;i<=10;i++){
if(document.frmMetricsSelect.metricused(i).checked==true){
x=x & document.frmMetricsSelect.metricused(i).value & ",";
}
}
document.frmMetricsSelect.action="ProcessSubmitMetrics.asp?FY=" & <%=intFY%> & "&MetricsSelected=" & Mid(x,1,len(x) -1)
End Sub
IE keeps hollering at me "expected end of statement" on the declaration of x. I think I'm supposed to use single quotes but in Visual Interdev single quotes depict comments so it ignores the line.
Please please please help!!!!
The point of the script is to count MetricUsed on frmMetricsSelect where MetricUsed is checked. For every time its checked, add that MetricUsed value (identifier) to x thusly making a string I need in the next page.
TIA,
Dia
Roy Sinclair
09-09-2002, 06:52 PM
You are mixing VB and Javascript, it's a wonder anything works.
Sub b1_onclick
var x=" ";
for(var i=0;i<=10;i++){
if(document.frmMetricsSelect.metricused(i).checked==true){
x=x & document.frmMetricsSelect.metricused(i).value & ",";
}
}
document.frmMetricsSelect.action="ProcessSubmitMetrics.asp?FY=" & <%=intFY%> & "&MetricsSelected=" & Mid(x,1,len(x) -1)
End Sub
The parts marked in red are VBScript, not JavaScript. You cannot mix the two languages like this.
Like I said, I'm a newbie and don't know which end is up or down with JS.
Despite the changing the notation to Function for js, my loop still doesn't produce what I need it to.
boywonder
09-09-2002, 07:31 PM
try using + instead of &
Using + instead of & didn't make any difference. I added junk text after the start of the loop and it didn't even error out there. Why is it not even hittng the loop?
Roy Sinclair
09-09-2002, 07:38 PM
Show your current code, without that all we can do is just guess.
function submit1_onclick() {
var x='';
for(var i=0;i<=document.frmMetricsSelect.metricused(i).length-1;i++){
if(document.frmMetricsSelect.metricused(i).checked==true){
x=x + document.frmMetricsSelect.metricused(i).value + ',';
}
}
document.frmMetricsSelect.action="ProcessSubmitMetrics.asp?FY=" & <%=intFY%> & "&MetricsSelected=" & Mid(x,1,len(x) -1)
}
Roy Sinclair
09-09-2002, 08:07 PM
You didn't swap the "&"s out for "+"s as noted by boywonder and you left the "MID" call in there as well and that's a VBScript function, not a Javascript function.
Try this:
function submit1_onclick()
{
var x='';
for(var i=0;i<=document.frmMetricsSelect.metricused(i).length-1;i++)
{
if(document.frmMetricsSelect.metricused(i).checked==true)
{
x=x + document.frmMetricsSelect.metricused(i).value + ',';
}
}
document.frmMetricsSelect.action="ProcessSubmitMetrics.asp?FY=" + <%=intFY%> + "&MetricsSelected=" + x
}
It may not be perfect yet but it should get you closer to the result you're looking for.
Thanks for the editing. I'm having difficulty today changing languages in my head. Oracle eats my brain for a snack.
Now its erroring on the following in bold with the error "Object expected": <INPUT type="Submit" value="Submit" name="B1" LANGUAGE="javascript" onclick="return B1_onclick()">
urgh.... :mad:
Roy Sinclair
09-09-2002, 09:24 PM
If it's supposed to be calling the function then you need to change the name of the function or the name being called so they match each other (remember, the names are case sensitive too!).
submit1_onclick <> B1_onclick
Again, my typo posting here. The function is called b1_onclick and the button is calling b1_onclick.
Roy Sinclair
09-09-2002, 09:33 PM
Originally posted by DiaH
Again, my typo posting here. The function is called b1_onclick and the button is calling b1_onclick.
Double check, make sure they are both b1_onclick. If one of them is B1_onclick then the problem you're describing is possible. Another possibility is some other invalid javascript in the same <script></script> block where you defined the b1_onclick function which is causing the interpretation of your script to be aborted before it gets to the b1_onclick function.
joh6nn
09-09-2002, 09:40 PM
i think the problem is that you've got onclick="return B1_onclick()", and your function doesn't return a value.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.