Jump to content

Help with DOM and Forms - changing input values conditionally


bronzemonkey

Recommended Posts

I'm trying to alter the values of inputs in an XHTML form depending on the selections of the user. If the user selects "yes" for then the input fields for the remaining questions remain blank, but if they select "no" I want "N/A" to be automatically written into the input fields for the next questions.

 

The previous way the desired effect was being achieved was by using this code...where "mainform" was the name of the form and the other values were the names of the inputs.

 

<script type="text/javaScript">

function setPrev()

{
if(document.mainform.input1.value=='yes')
{
	document.mainform.input2.value="";
	document.mainform.input3.value="";
	document.mainform.input4.value="";
}
else
{
	document.mainform.input2.value="N/A";
	document.mainform.input3.value="N/A";
	document.mainform.input4.value="N/A";
}
}

</script>

<select name='input1' onchange='setPrev()'>

 

This isn't acceptable because I need the XHTML to validate and the JS to be separate from the document structure. I've been trying to get the JS to work by the form's id (removing the name attribute) and var but with no success.

 

Ideally all the JS needs to be separate from the document structure but I'm new to JS (this is certainly a relatively simple problem) and not having any luck with getting this to work. Any help in this regard would be most welcome! Thanks

Link to comment
Share on other sites

<select name="input1" id="input1">

//the rest of the select block

 

function fill(){

 

var input_sel = document.getElementById('input1');

input_sel.onchange = function(){

//use ids instead of the way you're referencing the form fields

if(input_sel.value == 'yes'){

document.getElementById('input2').value = 'N/A';

...

}else{

....

}

}

}

 

onload = function(){

fill();

}

Link to comment
Share on other sites

Thanks, this works perfectly with the xhtml I already had...glad to be rid of the old js handed down to me.

 

I've just changed the last part of the script to...

 

window.addEvent('domready', function(){
fill();
});

 

...so that I can use it in my site.js file.

 

Thanks for your help!

Link to comment
Share on other sites

cool, you're using mootools :)

 

id rewrite fill as:

 

function fill(){

var input_sel = $('input1');

input_sel.onchange = function(){

//use ids instead of the way you're referencing the form fields

if(input_sel.value == 'yes'){

$('input2').value = 'N/A';

...

}else{

....

}

}

}

 

only a minor change in using the $() function instead of document.getElementById()

Link to comment
Share on other sites

input_sel.addEvent('change',function(){

//your if statements

});

 

This didn't work...played around with it and couldn't figure out what the error was. Thanks for the previous suggestion though, I've incorporated it. Below is my current code.

 

function fill(){

var input_sel = $('input1');
input_sel.onchange = function(){

if(input_sel.value == 'no'){
$('input2').value = 'N/A';
$('input3').value = 'N/A';
$('input4').value = 'N/A';
$('input5').value = 'N/A';

}else{
$('input2').value = '';
$('input3').value = '';
$('input4').value = '';
$('input5').value = '';
}
}
}

window.addEvent('domready', function(){
fill();
});

 

Since you know Mootools you might be able to shed some light on another simple question. Can I use "smoothScroll" to smoothly reveal content that is revealed from simple hide/show JS? Right now I have a menu that uses the accordionFX but it always opens at the first part of the menu. I'm looking to achieve the same effect as the accordionFX but without any part of the "accordion" being open by default. Any tips?

 

Thanks again

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.