JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 31st, 2001, 12:43 PM
mstembri mstembri is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Norcross, GA
Posts: 458 mstembri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
Using javascript to write additional form elements based on user input

Say I have a form that asks for a name and favorite color. There is a third elemet that asks for favorite food, but it is disabled (greyed out). Pretty basic html so far.

Now, add to the form a checkbox beside the name field. If a user clicks this checkbox the greyed out element becomes active.

Before the checkbox is selected the form looks like this:

Code:

<form action=test.cgi method=post>

Name? <input type=text name=name> <input type=checkbox name=addelement value=yes> List your favorite food?

Color? <input type=text name=color>

Food? <input type=text name=food disabled>

<input type=submit value=submit>

</form>



If the checkbox is selected the form looks like this:

Code:

<form action=test.cgi method=post>

Name? <input type=text name=name> <input type=checkbox name=addelement value=yes> List your favorite food?

Color? <input type=text name=color>

Food? <input type=text name=food>

<input type=submit value=submit>

</form>



The user can select and unselect the checkbox and the food input text box will select/deselect with each subsequent click.

I've done something vaguely similar - but only to change a value in an existing textbox, not to change an option for a text box.

Can this be done?
__________________
- Mike Stembridge
http://www.georgiaoffroad.com

Reply With Quote
  #2  
Old May 31st, 2001, 04:34 PM
mstembri mstembri is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Norcross, GA
Posts: 458 mstembri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
This sort of works, but it rewrites the entire page...



<form>
Enter: <input type="text" value="hello" name=test>

<SCRIPT LANGUAGE="JavaScript">
<!--

var count = 1;
var text = "disabled";

function changeit() {
count = count +1;
if (count%2 == 0) {
var text = "disabled";
document.write("<INPUT NAME='test' TYPE='text' SIZE='15' " + text + " value='test'>");
} else {
var text = "";
document.write("<INPUT NAME='test' TYPE='text' SIZE='15' " + text + " value='test'>");
}
}

//-->
</script>

<input type="checkbox" name=check value="yes" onclick="javascript: changeit();" >

</form>

Reply With Quote
  #3  
Old May 31st, 2001, 04:43 PM
mikenz mikenz is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Christchurch, New Zealand
Posts: 30 mikenz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
Send a message via Yahoo to mikenz
Hey there

You can toggle the disabled property of that Food text box depending on the clicked state of the checkbox like this:

<script>
function food(){
if(document.formname.checkboxname.checked){
document.formname.textboxname.disabled=false
}
else{
document.formname.textboxname.disabled=true
}
}
</script>

Then, you can call the food() function onclick from the checkbox:

<input type=checkbox name=addelement value=yes onclick="food()">

Note that you'll need to give the form a name so that you can reference the textbox & checkbox.

__________________
www.mikenz.com - Inside My Mind

Reply With Quote
  #4  
Old May 31st, 2001, 05:00 PM
mstembri mstembri is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Norcross, GA
Posts: 458 mstembri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
Thanks Mike, it works like a charm.

Reply With Quote
  #5  
Old May 31st, 2001, 05:06 PM
mikenz mikenz is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Christchurch, New Zealand
Posts: 30 mikenz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
Send a message via Yahoo to mikenz
Great!

Reply With Quote
  #6  
Old May 31st, 2001, 07:45 PM
mstembri mstembri is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Norcross, GA
Posts: 458 mstembri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
Hmm....

I thought of something else that I'd like to consider doing with this.

Disabling the text box is not visably apparent to the average point & click user -- I'm thinking it would be a good idea to change the background color of the box to grey when the text boxes are deselected.

Using... style="background:gray" ...would do the trick but again we have the problem of writing the info to the element on the fly. In my first try (above), using document.write wrote the modified form element in a new window.

I wonder if there is another way to modify element properties on the fly, without reloading the entire page?

Reply With Quote
  #7  
Old May 31st, 2001, 07:59 PM
mikenz mikenz is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Christchurch, New Zealand
Posts: 30 mikenz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
Send a message via Yahoo to mikenz
Sure is

You can change form element styles dynamically through javascript like this:

e.g. 1

document.formname.textname.style.backgroundColor="red"

e.g. 2

document.formname.textname.style.textDecoration="underline"



Reply With Quote
  #8  
Old May 31st, 2001, 09:16 PM
mstembri mstembri is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Norcross, GA
Posts: 458 mstembri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
Cool, I just was looking at the innerHTML tag. That seems to be a different way of doing the same thing - but your javascript example looks cleaner to me.

Thanks again,
- Mike

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Using javascript to write additional form elements based on user input

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap