/    Sign up×
Community /Pin to ProfileBookmark

problem with js code on a form

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!

[CODE]<!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>[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@tirnaFeb 14.2011 — There's no checkboxes there
Copy linkTweet thisAlerts:
@chuloauthorFeb 14.2011 — sorry I didn't include it in the code, but in my version there is a checkbox, any idea why this might be happening? thanks!
Copy linkTweet thisAlerts:
@tirnaFeb 14.2011 — sorry I didn't include it in the code, but in my version there is a checkbox, any idea why this might be happening? thanks![/quote]

yes, but without seeing your checkbox code I can't tell if it will work.
Copy linkTweet thisAlerts:
@chuloauthorFeb 14.2011 — here is the checkbox code:

[CODE]<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>[/CODE]
×

Success!

Help @chulo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...