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 10-06-2005, 06:04 AM   PM User | #1
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
Checkbox to activate text box in form

I would like to have a checkbox and text box beside each other in a form. The text box would be greyed out and no info can be entered UNTIL the checkbox is checked. Once the checkbox is checked the text box becomes active and numbers then can be entered.

Can this be done with javascript? If not what are my options?

Thanks
__________________
Leonard Whistler
Len Whistler is offline   Reply With Quote
Old 10-06-2005, 06:19 AM   PM User | #2
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
Your original question would have been just fine. Now you have gone and cross posted which is against the forum rules.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
function checkMe(){
if(document.forms[0].thecheck.checked==1){
	document.forms[0].test.disabled=false;
	document.forms[0].test.value='1';
}
else if(document.forms[0].thecheck.checked==0){
	document.forms[0].test.disabled=true;
}
}	
</script>
</head>

<body>
<form action="#" method="post">
<input type="text" name="test" disabled="disabled" /><input type="checkbox" name="thecheck" onclick="checkMe()" />
</form>
</body>
</html>
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 10-06-2005, 06:20 AM   PM User | #3
Vladdy
Senior Coder

 
Join Date: Jun 2002
Location: Nashua, NH
Posts: 1,724
Thanks: 0
Thanked 0 Times in 0 Posts
Vladdy is an unknown quantity at this point
While it can be done with javascript to enhance user experience, you, first, need to design a usable form without relying on javascript.

Considering your post in HTML section, the checkbox seems to be a superfluous element. Why not default your quantity textboxes to 0 so that if a user wants an item he/she changes the text in that box to a different value. This scenario does not require client side scripting to be operational and reduces the amount of "clicks" user has to make to get the result.

As far as that code above goes it is way too inefficient. Here is how you implement this approach (points of emphasis in red)
Code:
<script type="text/javasript">
function buyThis(checkbox)
  {  // Use a sequence of nextSibling or previousSibling to reflect your markup 
     var textbox = checkbox.previousSibling;
     textbox.disabled = !checkbox.checked;
     textbox.value = checkbox.checked?'1':'0';
  }
</script>

<fieldset><legend>Products</legend>
....
<input type="text" name="qty5" value="0" /><input type="checkbox" name="prod5" onchange="buyThis(this)" />
....
</fieldset>
  }
__________________
Vladdy | KL
"Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

Last edited by Vladdy; 10-06-2005 at 06:38 AM.. Reason: Saw that code above after posting....
Vladdy is offline   Reply With Quote
Old 10-06-2005, 06:32 AM   PM User | #4
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
Ok...thanks for the replies in the morning I will be looking more into both suggestions. It's getting late.


Re:"Now you have gone and cross posted which is against the forum rules."

I always thought cross posting was cut & paste the exact same post within seconds in other forums. My original post was 1 hour or more before this one and then after some thought decided maybe the JavaScript section would be better, plus I rewrote it.
__________________
Leonard Whistler

Last edited by Len Whistler; 10-06-2005 at 06:35 AM..
Len Whistler 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 12:05 AM.


Advertisement
Log in to turn off these ads.