PDA

View Full Version : problem with js code on a form



chulo
Feb 14th, 2011, 08:58 PM
<!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>
<title></title>
<style type="text/css">
.showHideDivs {
display: none
}
</style>
<script type="text/javascript">
function showHideDivs(){
var job = '';
for(i=0; i < oRadBtns.length; i++){
if(oRadBtns[i].checked){
job = oRadBtns[i].value;
}
}
switch (job){
case 'yes':
document.getElementById("nombre").style.display = "none";
document.getElementById("apellido").style.display = "none";
document.getElementById("empresa").style.display = "block";
document.getElementById("contacto").style.display = "block";
break;
case 'no':
document.getElementById("nombre").style.display = "block";
document.getElementById("apellido").style.display = "block";
document.getElementById("empresa").style.display = "none";
document.getElementById("contacto").style.display = "none";
}
}
window.onload=function(){
oRadBtns = document.getElementById('radBtnCont').getElementsByTagName('input');
for(i=0; i < oRadBtns.length; i++){
oRadBtns[i].onclick = showHideDivs;
}
showHideDivs();
}
</script>
</head>
<body>
<div id="radBtnCont">
<input type="radio" value="yes" name="job" checked="checked" />Yes
<br />
<input type="radio" value="no" name="job" />No
</div>
<div id="nombre" class="showHideDivs">This is nombre div</div>
<div id="apellido" class="showHideDivs">This is apellido div</div>
<div id="empresa" class="showHideDivs">This is empresa div</div>
<div id="contacto" class="showHideDivs">This is contacto div</div>
</body>
</html>

chulo
Feb 14th, 2011, 09:42 PM
here is the checkbox code sorry:


<div class="form_element">

<input type="checkbox" class="checkbox" name="<?php echo HTMLForm::FORM_ACCEPT_FIELD ?>" id="acceptance" <?php echo ($form->isAccepted()) ? 'checked="checked"' : '' ?> value="<?php echo HTMLForm::FORM_ACCEPT_VALUE ?>" />

<label for="acceptance" class="checkbox_label dselabel <?php echo ($form->isSubmitted() && !$form->isAccepted()) ? 'error':'' ?>">Doy mi conformidad con la declaraci&oacute;n de&nbsp;</label>

<a onclick="window.open('?open=privacy','privacy','scrollbars=yes,width=520,height=350');" class="dse">protecci&oacute;n de datos</a>

<label for="acceptance" class="checkbox_label dselabel <?php echo ($form->isSubmitted() && !$form->isAccepted()) ? 'error':'' ?>">&nbsp;. <?php echo $page['star'];?></label>

</div>

Philip M
Feb 14th, 2011, 10:39 PM
And your question/problem is what? :confused: We are not clairvoyant! :)

Old Pedant
Feb 14th, 2011, 10:41 PM
And the reason you posted that code is?

I assume there is a problem, but you don't say what it is.

Also, please don't show us the PHP code in a JavaScript forum. Bring up the web page and then do a VIEW==>>SOURCE to get the HTML and show us that.

Old Pedant
Feb 14th, 2011, 10:42 PM
And your question/problem is what? :confused: We are not clairvoyant! :)

Speak for yourself Philip! I, for one, knew you were going to say that. <grin/>

chulo
Feb 14th, 2011, 11:38 PM
I apologise I forgot to add my question:

relating to the original post.....

So the following code hides/shows fields in a form when radio buttons are clicked, the problem is on the same form I have a checkbox and when said check box is selected the showing and hiding of the fields doesn't work, any ideas? Thank you very much!