Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

HTML Forum


You are currently viewing our HTML Forum as a guest. Please register to participate.
Login



Post a Project »

Find a Professional HTML Freelancer!

Find a Freelancer to help you with your HTML projects

FREE Outsourcing eBook!

Reply
disable text fields when checkbox is checked
Old 09-14-2006, 07:13 PM disable text fields when checkbox is checked
Skilled Talker

Posts: 54
Trades: 0
Hi i want to disable three textareas when one checkbox is ticked. how would i go about doing this?

My code:

Code:
<p align="left">centre: 
                        <input name="centre"  type="checkbox" id="centre" value="true" checked />
                      </p>
                      <p align="left">y co-ordinates
                        <input type="text" name="y" id="y" value="0" style="width:117px;" />
                      </p>
                      <p align="left">x-co-ordinates
                        <input type="text" name="x" id="x" value="0" style="width:117px;" />
 
                      </p>
                      <p align="left">angle
                        <input type="text" name="angle" id="angle" value="0" style="width:117px;" />
                      </p>

I want the centre checkbox to disable the x, y and angle field when it is checked. I've tried different solutions but can only see to disable one by doing:



Code:
<p align="left">centre: 
                        <input name="centre" onclick="y.disabled= centre.checked; return true;" else ("y.disabled=false") type="checkbox" id="centre" value="y" checked />
                      </p>
                      <p align="left">y co-ordinates
                        <input type="text" name="y" id="y" value="0" style="width:117px;" />
                      </p>
                      <p align="left">x-co-ordinates
                        <input type="text" name="x" id="x" value="0" style="width:117px;" />
 
                      </p>
                      <p align="left">angle
                        <input type="text" name="angle" id="angle" value="0" style="width:117px;" />
                      </p>
However you have to uncheck and then recheck the box for this to work, and i cant get it to work with the other fields. PLEASE HELP.
digi duck is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-15-2006, 05:27 PM Re: disable text fields when checkbox is checked
funkdaddu's Avatar
Web Design Snob

Posts: 635
Trades: 0
Here, try this:
HTML Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1"/>
		<title>Untitled Page</title>
		<script type="text/javascript"><!--
function showHide() {
	trueFalse = !document.FormName.centre.checked; 
	document.FormName.x.disabled = trueFalse;
	document.FormName.y.disabled = trueFalse;
	document.FormName.angle.disabled = trueFalse;
}
//-->
</script>
	</head>

	<body>
		<form id="FormName" action="" method="get" name="FormName">
			<p align="left">centre: <input name="centre" onclick="showHide();" type="checkbox" id="centre" value="true" checked /></p>
			<p align="left">y co-ordinates <input type="text" name="y" id="y" value="0" style="width:117px;" /></p>
			<p align="left">x-co-ordinates <input type="text" name="x" id="x" value="0" style="width:117px;" /></p>
			<p align="left">angle <input type="text" name="angle" id="angle" value="0" style="width:117px;" /></p>
		</form>
	</body>

</html>

Last edited by funkdaddu; 09-15-2006 at 05:30 PM..
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
Old 09-16-2006, 11:51 AM Re: disable text fields when checkbox is checked
Ultra Talker

Posts: 256
Location: Auckland, New Zealand
Trades: 0
Funkdadu,

That's not correct XHTML 1.1, using attributes that are not correct for some elements, were deprecated and also unneccessary. Should not use the prolog line due to it throwing IE into quirks mode, the function showHide and variable trueFalse is an oxymoron and confusing and should be better named.

Instead of being unhelpful, here's the things to fix in the above:

Remove prolog line, <?xml ... ?> also it should be UTF-8 or UTF-16 when using XHTML, UTF-8 preferred because of browser support, this means updating the <meta http-equiv="Content-Type" charset to reflect it.

In <html> let them know what language the document it is by using the attribute, e.g. xml:lang="en"

The names for the function, showHide might be better reflecting what it is doing, disableInputs() and for the variable trueFalse possibly bValue, would have used bool but it could be a keyword, so I'd avoid it.

There's no name attribute for form, there's no align attributes for paragraph elements and also it should default to left anyways unless inheriting from a parent element. If needs be you should add to the style text-align: left;

And the last problem is the checkbox attribute checked, it should be checked="checked" or don't set it at all for it to default to being unchecked.

digi_duck,

You mention textareas but your code only reflects textboxes.

Here's a working example that shows a similar way of tackling the problem, it might seem a little confusing so I'll explain it, here's the code:

HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-NZ">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>Javascript, Disable multiple textboxes when checkbox is checked.</title>
		<script type="text/javascript">
		function disable(inputs,bValue)
		{
			for(var i = inputs.length, j = 0; i > j; i--)
			{
				inputs[i-1].disabled = bValue;
			}
		}
		function disableNames(chkbox)
		{
			disable(document.getElementById('names').getElementsByTagName('input'),chkbox.checked ? true : false);
		}
		</script>
	</head>
	<body>
		<form id="form1" action="" method="post">
			<fieldset>
				<legend>Name Details</legend>
				<p>Do you want to fill in you name? <input id="noway" name="noway" type="checkbox" value="noway" onclick="disableNames(this);" /> <label for="noway">NO WAY!</label></p>
				<div id="names">
					<label for="fname">First:</label> <input id="fname" name="fname" type="text" /> <label for="mname">Middle:</label> <input id="mname" name="mname" type="text" /> <label for="lname">Last:</label> <input id="lname" name="lname" type="text" />
				</div>
				<p><input id="submit" name="submit" type="submit" value="Send Details" alt="Send Details" /></p>
			</fieldset>
		</form>
	</body>
</html>
We'll look at the form first, it's pretty straight forward, the onclick action of noway (the checkbox) sends the element object of that checkbox to the function disableNames.

disableNames then calls the disable function passing all the inputs found inside the names divider (div) as an array of elements, it also sends the value of the checkbox checked as either a true or false (boolean) value.

We then loop through the elements and set them disabled based on the status of the checkbox, so if the checkbox is checked, the inputs will be disabled, if unchecked the inputs will be enabled.

You should also know that elements that are disabled, their values should not be passed when submitted so it should not matter if someone enters details in those textboxes and then disables the inputs, but if you are concerned about this, you can just clear the value of those inputs just by adding it to the disable function.

What might make this confusing is how I've written the script, usually trying to do less steps avoiding variable usage for one off things, doing the for loop in reverse which is only a performance/optimisation thing as well as storing the length in a variable again it's a performance/optimisation thing, just with lookup times.

Cheers,

MC
__________________
#------------------------------signature---------------------------------------------------------------------------------#
Quote:
I am well recognised for what I don't do than what I do. Chores are just one of those things.
mastercomputers is offline
Reply With Quote
View Public Profile Visit mastercomputers's homepage!
 
Old 09-17-2006, 02:25 PM Re: disable text fields when checkbox is checked
Skilled Talker

Posts: 54
Trades: 0
wow thanks a lot
digi duck is offline
Reply With Quote
View Public Profile
 
Old 09-18-2006, 10:37 AM Re: disable text fields when checkbox is checked
funkdaddu's Avatar
Web Design Snob

Posts: 635
Trades: 0
I'm new to XHTML, - I just used a template from a WYSIWYG program, and I validated it using the validator at: validator.w3.org. For the left alignment, that was his code, so I just left it as is and it validated fine as well.

Know any good resource for seeing what's different when using JS between HTML 4 and XHTML?
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
Old 09-18-2006, 08:47 PM Re: disable text fields when checkbox is checked
Ultra Talker

Posts: 256
Location: Auckland, New Zealand
Trades: 0
Quite funny you should ask that funkdaddu, I actually just started on writing up a Quick Reference Guide for XHTML 1.1 and I completely forgot about JS differences but I'll make sure I cover that area too and the other languages like embedding flash and other objects. The main goal is basically to get them to understand XHTML and also how to make the change from HTML 4.01 to XHTML while still maintaining validity.

Basically, the main difference is that some methods (functions) have been removed (document.write/writeln) and others properties (variables/objects) have been switched to more reasonable objects, I think document.href use to be one but now it's window.href which makes more sense.

The only alternative for working in the document is through DOM API, which has it's good and bad points, IE and FF still have differences though IE seems to have some things I believe to be correct than FF does (nextSibling property), but FF has far better support of the DOM. All I can say is it's no walk in the park, as a lot of it relies on your ability to walk through the DOM. You could still use proprietary means since they are faster for those specific browsers (and there's no standards validation for JS), but the DOM is far more compatible for all the other browsers.

QuirksMode.org is a good place to look up DOM API and compatibility list.

The best guide for Javascript would be reading EMCAScript 262, it's the Standards for Javascript/JScript and should work directly with XHTML. The other things to read would now involve DOM manipulation, the W3C Recommendations on this is quite good though vague also. Most DOM Levels work except browsers still have their own proprietary methods like innerHTML, which IE came up with though most browser support this too.

I remember a quote, maybe from Tim Berner-Lees "It's now easier to create websites" which was said around the time HTML was booming, but the direction today is far from that, it seems it's getting more complex and we're now being made to be more specific and direct about what we do as well as not only trying to make it understandable for humans, but also machines.

If you use FF, then you should use the DOM Inspector or Firebug extension as well as JS Console. These help with debugging and understanding the documents. Just wish IE had the same thing, as I find it's trying to get it to work with IE that makes it the hardest thing to do.

Cheers,

MC
__________________
#------------------------------signature---------------------------------------------------------------------------------#
Quote:
I am well recognised for what I don't do than what I do. Chores are just one of those things.
mastercomputers is offline
Reply With Quote
View Public Profile Visit mastercomputers's homepage!
 
Reply     « Reply to disable text fields when checkbox is checked
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.38286 seconds with 12 queries