Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
154
rated 0 times [  160] [ 6]  / answers: 1 / hits: 64969  / 13 Years ago, sun, february 12, 2012, 12:00:00

I want to pass certain hidden parameters in a request when some conditions are met.



For example I want to pass these if this condition is true:



<script type=text/javascript> 
function checkClosureLevel()
{
var openLevel = document.getElementById('openLevel');
var phyCompLevel = document.getElementById('phyCompLevel');
var finCompLevel = document.getElementById('finCompLevel');

if (openLevel.checked) {
//PASS HIDDEN FORM VARIABLES HERE AND SUBMIT FORM
}
}
</script>

<form action=process.det_details method=post name=detParameterForm>
<fieldset class=det>
<legend>Closure Level</legend>
<input type=checkbox name=openLevel >Open</input><br/>
<input type=checkbox name=phyCompLevel >Physically Complete</input>
<br/>
<input type=checkbox name=finCompLevel >Financially Complete</input>
</fieldset>
</form>

More From » javascript

 Answers
19

The document.forms object is a collection of all <form> elements in the page. It has numeric indexes, and named items. The named items correspond to a name attribute of each <form>.



var theForm = document.forms['detParameterForm'];


To make appending data easier, you can create a function which adds data for a given form.



function addHidden(theForm, key, value) {
// Create a hidden input element, and append it to the form:
var input = document.createElement('input');
input.type = 'hidden';
input.name = key; // 'the key/name of the attribute/field that is sent to the server
input.value = value;
theForm.appendChild(input);
}

// Form reference:
var theForm = document.forms['detParameterForm'];

// Add data:
addHidden(theForm, 'key-one', 'value');
addHidden(theForm, 'another', 'meow');
addHidden(theForm, 'foobarz', 'baws');

// Submit the form:
theForm.submit();

[#87520] Friday, February 10, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leonardok

Total Points: 114
Total Questions: 94
Total Answers: 103

Location: France
Member since Thu, Oct 27, 2022
2 Years ago
;