Forum Moderators: open

Message Too Old, No Replies

Javascript to control forms

must be compatible to any browser

         

mespejo

5:41 am on Dec 8, 2006 (gmt 0)

10+ Year Member



Hi,

I would like to make my form a bit interactive for the user. I mean I want to create forms that display additional fields on the form according to the answer given.

For example, using a radio button. If the user tick on Yes, the invisible fields will be displayed but if they select on No, it will disappear.

Much appreciated for anyone who can help me out?

MichaelBluejay

8:19 am on Dec 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



By must be compatible with "any" browser, do you actually mean with "all" browsers? To be compatible with "any" browser means that if it worked in even one browser that would be good enough. That's easy. But if you want it to work in "all" browsers, that's likely impossible. Version 1.0 browsers don't support CSS, and maybe even version 2 & 3 don't, I'm not sure exactly. Me, I support Version 5+ browsers and that's it. If that's good enough for you, then this will get you started.

<form name=theform>
<input type=checkbox name=boobo value=yes onclick="if (this.checked) { document.getElementById('secondcheckbox').style.display='block';} else {document.getElementById('secondcheckbox').style.display='none';}"> First checkbox
<div id=secondcheckbox style="display:none"><input type=checkbox name=secondcheckbox> Second checkbox</div>
</theform>

[edited by: MichaelBluejay at 8:20 am (utc) on Dec. 8, 2006]

Fotiman

4:10 pm on Dec 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




I mean I want to create forms that display additional fields on the form according to the answer given.

For example, using a radio button. If the user tick on Yes, the invisible fields will be displayed but if they select on No, it will disappear.

Well, first, consider the base case. You should design your pages to work without JavaScript enabled, so first layout your form to include all of the inputs:


<form action="#">
<div>
Got Milk?
<input type="radio" name="gotMilk" id="gotMilkYes"> Yes
<input type="radio" name="gotMilk" id="gotMilkNo"> No
<div id="milkOptions">
Is it chocolate milk?
<select name="chocolate" id="chocolate">
<option value="false" selected="selected">No</option>
<option value="true">Yes</option>
</select>
</div>
</div>
</form>

Now all the options will be available even if the user has JavaScript disabled. Next, attach your behaviors. Note, I've defined the script in the page, but you would be better to put it in it's own file and link it in. Also, I'm using a someone complex notation... if you have any questions about this, please ask:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset:utf-8">
<title>Example</title>
</head>
<body>
<form action="#">
<div>
Got Milk?
<input type="radio" name="gotMilk" id="gotMilkYes" > Yes
<input type="radio" name="gotMilk" id="gotMilkNo" checked="checked"> No
<div id="milkOptions">
Is it chocolate milk?
<select name="chocolate" id="chocolate">
<option value="false" selected="selected">No</option>
<option value="true">Yes</option>
</select>
</div>
</div>
</form>
<script type="text/javascript">
/**
* Your Page Behaviors
*/
var YourPage = function(){
// Private variables
var choc = null;
// Public methods
return {
/**
* Initialize the behaviors for the page
*/
init : function(){
// Attach your listeners
attachListeners();
},
/**
* Show or hide the milk options
*/
toggleChocolate : function(bShow){
if(!choc ) return;
choc.style.display = (bShow?'block':'none');
}
};
// Private methods
function attachListeners(){
// Get the Chocolate Milk section
choc = document.getElementById("milkOptions");
// Get the Got Milk radio buttons
var gmYes = document.getElementById("gotMilkYes");
var gmNo = document.getElementById("gotMilkNo");
// If they were found, attach a listener to their onclick event handlers
if( gmYes && gmNo )
{
gmYes.onclick = function(){YourPage.toggleChocolate(true);};
gmNo.onclick = function(){YourPage.toggleChocolate(false);};
// Call toggleChocolate once with the initial value
YourPage.toggleChocolate(gmYes.checked);
}
};
}();
window.onload = YourPage.init;
</script>
</body>
</html>

[edited by: Fotiman at 4:13 pm (utc) on Dec. 8, 2006]

mespejo

2:00 am on Dec 11, 2006 (gmt 0)

10+ Year Member



Thank you for your help. I was now able to make my form the way I wanted it to work.