/    Sign up×
Community /Pin to ProfileBookmark

switch statement problem

Can switch statements only be used for two statements and also how do i connect my switch statements to my radio buttons so whenever i choose a certain option with the radio button it changes, i’m confused

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@VITSUSAJan 27.2020 — Click on the mentioned hyperlink to know more about switch statements - -

https://www.iitk.ac.in/esc101/05Aug/tutorial/java/nutsandbolts/switch.html

https://javascript.info/switch
Copy linkTweet thisAlerts:
@rootJan 29.2020 — Switch..case can be text tests, numeric tests to match specific terms and you have as many as you want.

Note that people often try using these in place of IF conditionals when the IF should be used, you will get odd results when you try to evaluate a bool as the case element of the statement.

Here is a tutorial : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
Copy linkTweet thisAlerts:
@SempervivumJan 30.2020 — @gavin505#1613684 how do i connect my switch statements to my radio buttons so whenever i choose a certain option with the radio button it changes[/quote]
By chance I coded a solution for this requirement some days ago :
<script>
function einblenden() {
const rbGroups = document.querySelectorAll('.radioGroup');
const idxSel = document.getElementById("Standort_mod").selectedIndex;
for (let i = 0; i < rbGroups.length; i++) {
const theGroup = rbGroups[i];
if (theGroup.getAttribute('data-idxsel') == idxSel) {
theGroup.style.display = 'block';
} else {
theGroup.style.display = 'none';
}
}
}
</script>

<i> </i>&lt;label&gt;Standort:&lt;/label&gt;
<i> </i>&lt;select class="cms_dropdown" name="Standort_mod" id="Standort_mod" value="Standort" onchange="einblenden();"&gt;

<i> </i> &lt;option value="auswahl_leer"&gt;Standort&lt;/option&gt;

<i> </i> &lt;option value="liestal"&gt;Liestal&lt;/option&gt;

<i> </i> &lt;option value="bruderholz"&gt;Bruderholz&lt;/option&gt;

<i> </i> &lt;option value="laufen"&gt;Laufen&lt;/option&gt;

<i> </i>&lt;/select&gt;

<i> </i>&lt;div id="mod_liestal" class="radioGroup" data-idxsel="1" style="visibility:hidden;"&gt;

<i> </i> &lt;label for="custom.modalitaet"&gt;Modalitaet:&lt;/label&gt; &lt;br /&gt;

<i> </i> &lt;input type="radio" name="custom.modalitaet" class="cms_textfield" value="Mod1" required="required"
<i> </i> onClick="document.getElementById('requiredField').value=this.value" /&gt;

<i> </i> &lt;span class="vvtmodalitaet" id="Mod1.name"&gt;Mod1 Liestal&lt;/span&gt;
<i> </i> &lt;span id="Mod1"&gt;&lt;/span&gt; &lt;br /&gt;

<i> </i>&lt;/div&gt;

<i> </i>&lt;div id="mod_bruderholz" class="radioGroup" data-idxsel="2" style="visibility:hidden;"&gt;

<i> </i> &lt;label for="custom.modalitaet"&gt;Modalitaet:&lt;/label&gt; &lt;br /&gt;

<i> </i> &lt;input type="radio" name="custom.modalitaet" class="cms_textfield" value="Mod1" required="required"
<i> </i> onClick="document.getElementById('requiredField').value=this.value" /&gt;

<i> </i> &lt;span class="vvtmodalitaet" id="Mod1.name"&gt;Mod2 Brunderholz&lt;/span&gt;
<i> </i> &lt;span id="Mod1"&gt;&lt;/span&gt; &lt;br /&gt;

<i> </i>&lt;/div&gt;

<i> </i>&lt;div id="mod_laufen" class="radioGroup" data-idxsel="3" style="visibility:hidden;"&gt;

<i> </i> &lt;label for="custom.modalitaet"&gt;Modalitaet:&lt;/label&gt; &lt;br /&gt;

<i> </i> &lt;input type="radio" name="custom.modalitaet" class="cms_textfield" value="Mod1" required="required"
<i> </i> onClick="document.getElementById('requiredField').value=this.value" /&gt;

<i> </i> &lt;span class="vvtmodalitaet" id="Mod1.name"&gt;Mod3 Laufen&lt;/span&gt;
<i> </i> &lt;span id="Mod1"&gt;&lt;/span&gt; &lt;br /&gt;

<i> </i>&lt;/div&gt;
(Might be subject to improvements, there are multiple IDs and one should better use fieldsets instead of divs)
×

Success!

Help @gavin505 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 4.26,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...