Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-15-2005, 04:44 AM   PM User | #1
dniwebdesign
Regular Coder

 
dniwebdesign's Avatar
 
Join Date: Dec 2003
Location: Carrot River, Saskatchewan
Posts: 838
Thanks: 15
Thanked 9 Times in 9 Posts
dniwebdesign is an unknown quantity at this point
Enable/Disable Fields

Code:
function changecontent(which)
{
	if(document.addflyer.flyer.value == "add")
	{
		document.addflyer.uploader.disabled = true;
	}
	else
	{
		document.addflyer.uploader.disabled = false;
	}
}
Alright... I got 2 different radio buttons which hold the values (I have shown them below). Uploader is a "file" input type. I want to disable if the user selects the remove radio button and I want it enabled (so users can choose a file) if they select the add button.

However the code I have doesn't do squat... if I have the "uploader" file input set as disabled both options enable it. Which should do. What is wrong?

Code:
                  <input type="radio" name="flyer" value="add" onClick="changecontent(this)">
                  Add Flyer 
                  <input type="radio" name="flyer" value="remove" onClick="changecontent(this)">
                  Remove Flyer<br>
                  <br>
                  <input type="file" name="uploader" size="25" class="textinput">
__________________
Dawson Irvine
CEO - DNI Web Design
http://www.dniwebdesign.com
dniwebdesign is offline   Reply With Quote
Old 04-15-2005, 05:32 AM   PM User | #2
hemebond
Senior Coder

 
Join Date: Jul 2004
Location: New Zealand
Posts: 1,315
Thanks: 0
Thanked 2 Times in 2 Posts
hemebond is an unknown quantity at this point
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
	<head>
		<title>57010</title>
	</head>
	<body>
		<form action="57010" method="get">
			<fieldset>
				<div>
					<input id="flyer_add" name="flyer" type="radio" value="add" checked="checked">
					<label for="flyer_add">Add Flyer</label>
				</div>
				<input id="uploader" name="uploader" type="file" size="25">
				<div>
					<input id="flyer_rem" name="flyer" type="radio" value="remove" onClick="changecontent(this)">
					<label for="flyer_rem">Remove Flyer</label>
				</div>
			</fieldset>
		</form>

		<script type="text/javascript">
			var flyer = document.getElementsByName('flyer');
			for(var i = 0; i < flyer.length; i++)
			{
				flyer[i].onclick = toggleUploader;
			}

			function toggleUploader()
			{
				document.getElementById('uploader').disabled = (document.getElementById('flyer_add').checked) ? false : true;
			}
		</script>
	</body>
</html>
__________________
Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
I code to specs, and test only in Firefox (unless stated otherwise).
hemebond is offline   Reply With Quote
Old 04-15-2005, 05:45 AM   PM User | #3
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
You have a parameter named "which" but you did not use it.

if(which.value == "add")
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 04-15-2005, 05:46 AM   PM User | #4
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
Asside from hemebond's code, I think you just had a little mistake with your original code:
Code:
<html>
<head>
<script type="text/javascript">
function changecontent(which)
{
	if(which == "add")
	{
		document.addflyer.uploader.disabled = true;
	}
	else
	{
		document.addflyer.uploader.disabled = false;
	}
}
</script>
</head>
<body>
<form name="addflyer">
<input type="radio" name="flyer" value="add" onClick="changecontent(this.value)">
Add Flyer
<input type="radio" name="flyer" value="remove" onClick="changecontent(this.value)">
Remove Flyer<br>
<br>
<input type="file" name="uploader" size="25" class="textinput">
</form>
</body>
</html>
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 04-15-2005, 05:53 AM   PM User | #5
dniwebdesign
Regular Coder

 
dniwebdesign's Avatar
 
Join Date: Dec 2003
Location: Carrot River, Saskatchewan
Posts: 838
Thanks: 15
Thanked 9 Times in 9 Posts
dniwebdesign is an unknown quantity at this point
Quote:
Originally Posted by glenngv
You have a parameter named "which" but you did not use it.

if(which.value == "add")
Thanks glen, it works now...

As far as the "<form>" fields, I did have them, just didn't include it with the rest of the code.
__________________
Dawson Irvine
CEO - DNI Web Design
http://www.dniwebdesign.com
dniwebdesign is offline   Reply With Quote
Old 04-15-2005, 06:00 AM   PM User | #6
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
To optimize the function a bit, you can make the form reference generic.
Code:
function changecontent(which)
{
	if(which.value == "add")
	{
		which.form.uploader.disabled = true;
	}
	else
	{
		which.form.uploader.disabled = false;
	}
}
All HTML fields have form property that refers to the form they belong to.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:59 AM.


Advertisement
Log in to turn off these ads.