Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For Loop mistyped?

Status
Not open for further replies.

Karl Blessing

Programmer
Feb 25, 2000
2,936
US
I have this function below (some ASP code inside to distinguish what to use if IE or if NS) if I check a checkbox , it will find it , and if a box is selected , it returns true, this works, and my aleart does show true, but if I uncheck a box(when no other boxes are checked), nothing happens, and it never reaches the alert box inside of the function that you see below.

this is an example of an input that trigers it
Code:
<input type=&quot;checkbox&quot; name=&quot;rn5695&quot; onclick=&quot;alert(refreshtop());&quot;>

example (using onclick event)
[ ] [ ] [ ]
to
[X] [ ] [ ]

alerts true

[X] [ ] [ ]
to
[X] [X] [ ]

alerts true

[X] [X] [ ]
to
[X] [ ] [ ]

alerts true

[X] [ ] [ ]
to
[ ] [ ] [ ]

nothing happens, I want to return false if no boxes are chosen.


Code:
function refreshtop()
{
	for(var j=0; j<=maxvar; j++)
	{
		<% if instr(ucase(Request.ServerVariables(&quot;HTTP_USER_AGENT&quot;)), &quot;MSIE&quot;) <> 0 then %>
		if (document.all('rn'+reportlst[j]).checked == true)
		{
			return true;
		}
		<% else %>
		if (document.forms[0]['rn'+reportlst[j]].checked == true)
		{
			return true;
		}
		<% end if %>
	}
	return false;
}


it will return true if any boxes are chosen, but it will not return false when I uncheck the final checked box.

also the variable maxvar does exist, and in this scenario it's an 8, the names do show ( I did an aleart at one time to show the value of every checkbox being tested, just to be sure it was checking it correctly )

any clues?
Karl Blessing aka kb244{fastHACK}
kblogo.jpg
 
Hi,

I took your code, simplyfied it and it works like you want it I.E.
Code:
<HTML>
<HEAD>
<META NAME=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 6.0&quot;>
<TITLE></TITLE>
<script language=&quot;JavaScript&quot;>
var maxvar = 1;
var reportlst = new Array(5695, 5696);

function refreshtop()
{
	for (j = 0; j <= maxvar; j++)
	{
		if (document.all('rn' + reportlst[j]).checked)
			return true;
	}
	return false;
}
</script>
</HEAD>
<BODY>
<input type=&quot;checkbox&quot; name=&quot;rn5695&quot; onclick=&quot;alert(refreshtop());&quot;>
<input type=&quot;checkbox&quot; name=&quot;rn5696&quot; onclick=&quot;alert(refreshtop());&quot;>
</BODY>
</HTML>
So I'm going to have one long hard stare at your code to see where the problem lies!
 
kb244, try to change onclick with onchange
i don't know exactly *why* but i guess this is it
 
actually, i know why : when you uncheck a box, onchange is fired for sure, whereas onclick seems not to be fired ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top