/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Conditional Elements Script

Good Morning,

I am not familiar with Javascript and have been working on creating conditional elements for a web form. We have a Joomla site and have been using RSForm Pro to create the form. The developer of the program has a tutorial on creating these types of hidden/visible fields which after a lot of trial and error I was mostly able to make work.

I basically have a radio field that triggers a set of fields to be hidden or visible. My problem is that by default, those fields are displayed and only go away when the user selects “no”. I’d like to reverse that and have the fields hidden until the user checks “yes” at which time they display.

The script I am using is below, and the {2nd Location} is the field name for the radio button. Can anyone help me with this issue?

<script type=”text/javascript”>
function displayField()
{
if(document.getElementsByName(‘form[2nd Location]’)[0].checked)
document.getElementById(‘hide’).style.display=”none”;

if(document.getElementsByName(‘form[2nd Location]’)[1].checked)
document.getElementById(‘hide’).style.display=””;
}
</script>

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@FangFeb 10.2010 — function displayField()
{
document.getElementById('hide').style.display = (document.getElementsByName('form[2nd Location]')[0].checked)? 'none' : '';
}
Copy linkTweet thisAlerts:
@bscivoletteauthorFeb 10.2010 — Hi Fang,

Thank you for replying. I tested the code and it runs the same as the previous code, it still switches correctly but the fields are still present when the form loads. Could I be doing something wrong outside of the script?
Copy linkTweet thisAlerts:
@FangFeb 10.2010 — You may need to set the onload differently in Joomla<i>
</i>window.onload=function() {
document.getElementsByName('form[2nd Location]')[0].checked = true; //1st is 'no'
displayField();
};
Copy linkTweet thisAlerts:
@bscivoletteauthorFeb 10.2010 — Thanks again, Fang. That's definitely not it, screen just showed blank. I'm sure that had to do the with program and it's limitations. What about calling the "no" answer as a default so when the form loads, "no" will be selected and the fields will be hidden. RSForm Pro has an "Additional Attributes" box that I can plug code into.

If this helps, this is the code from their documentation I am working off of.

http://www.rsjoomla.com/customer-support/documentations/34-custom-scripting/93-show-or-hide-fields.html
Copy linkTweet thisAlerts:
@FangFeb 10.2010 — Place this after the function script and after the form:&lt;script type="text/javascript"&gt;
document.getElementsByName('form[2nd Location]')[0].checked = true; //1st is 'no'
displayField();
&lt;/script&gt;
the "no" answer as a default so when the form loads, "no" will be selected and the fields will be hidden[/QUOTE]You would still have to call the function
Copy linkTweet thisAlerts:
@bscivoletteauthorFeb 10.2010 — Amazing. I figured I was going to do something wrong but just as you said and it worked. Thank you very, very much!
×

Success!

Help @bscivolette 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.25,
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,
)...