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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Activating a function from a link

Status
Not open for further replies.

meeble

Programmer
Sep 24, 2002
137
GB
I have a JavaScript function. How do I activate this function by clicking on a text link IF a particular check box in a form is checked? If it is NOT checked then the link does nothing.

Cheers

James
 
I know this doesn't answer your query directly, but it would yield the same result ;o)

It might be easier to call the function regardless, and then test for the checkbox being unselected, and issue a "return" statement.

Hope this helps,
Dan
 
Well, you have to know which form the checkbox is in and what the name of the checkbox is. Let's say that the checkbox is in the first form on the page and its name is "Box". First of all, here is the IF statement you must put in your javascript function:

Code:
function FUNCTION_NAME(){
var checked;
checked = document.forms[0].Box.checked;
if (checked == true){
HERE IS ALL THE STUFF
}
}
Just replace the capitalized letters with the proper names and the word "Box" with the name of your checkbox. Next, here is the code for the link:

Code:
<a onclick=FUNCTION_NAME()>Click this to do stuff!</a>

Hope this helps!

Peace out,
Peace Co.
 
Hello.

This doesn't work.

I have

<script>

function checkSubmit(){
var checked;
checked = document.forms[0].box.checked;
if (checked == true){
url = 'appwin.php#man'
winMap = window.open(url,'winNffew','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,top=0,left=20,width=340,height=130')
}
}

</script>


And then:

<a href="#" onClick="checkSubmit()">Management</a><input type="checkbox" name="box" value="">


But nothing happens.

Any ideas?

Cheers

James
 

Do you have a form tag? Your source doesn't show one. if you have no form tag, then document.forms[0] will not work.

Hope this helps,
Dan
 
The form tag is <form action="applynow1a.php" method="post" name="theform" id="theform">
 
Using the code you've given above, I've simplified it, but changed nothing structurally, and it works fine for me:

Code:
<html>
<head>
	<script type="text/javascript">
	<!--
		function checkSubmit() {
			if (document.forms[0].box.checked) var winMap = window.open('appwin.php#man', 'winNffew', 'scrollbars,top=0,left=20,width=340,height=130');
		}
	//-->
	</script>
</head>
<body>
	<form action="applynow1a.php" method="post" name="theform" id="theform">
		<a href="javascript:void(0);" onClick="checkSubmit()">Management</a>
		<input type="checkbox" name="box" value="">
	</form>
</body>
</html>

So I imagine it's something else on your page that's breaking it. Unfortunately, it's hard to say without seeing any code. Give this code a go and see how you go.

Hope this helps,
Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top