2

I already have the following code to show/hide two form elements based on a dropdown selection. It worked when there was a single instance of a form on the page using IDs. Now there's multiple forms, which use classes. I've tried getElementsByClass but for some reason it's not working.

Here is the Javascript:

function Toggle(obj){
    var val=obj.value;

    if (!obj.m){ obj.m=''; }
    if (!obj.m.match(val)){ obj.m+=','+val+','; }

    var hide=obj.m.split(',');

    for (var zxc0=0;zxc0<hide.length;zxc0++){
        if (document.getElementsByClassName(hide[zxc0])){
            document.getElementsByClassName(hide[zxc0]).style.display='none';
        }
    }

    var show=val.split(',');

    for (var zxc1=0;zxc1<show.length;zxc1++){
        if (document.getElementsByClassName(show[zxc1])){
            document.getElementsByClassName(show[zxc1]).style.display='';
        }
    }
}

And the HTML:

<form class="contact" name="contact" action="#" method="post">
    <label>How did you hear about us:</label>
    <div id="styled-select">
        <select name="how" onChange="Toggle(this);" class="dropdown">
            <option value="Internet Search">Internet Search</option>
            <option value="Facebook" >Facebook</option>
            <option value="Twitter" >Twitter</option>
            <option value="LinkedIN" >LinkedIN</option>
            <option value="Referral,Referral2" >Referral</option>
            <option value="Other,Other2">Other</option>
        </select>
    </div>
     <label class="Referral" style="display:none;">Referred By:</label>
     <input name="Referral2" style="display:none;" class="hidden-txt Referral2">
     <label class="Other" style="display:none;">Please Specify:</label>
     <input name="Other2" value="" style="display:none;" class="hidden-txt Other2">
     ...
</form>

When referral is selected from the dropdown, the label class=Referral and input class=Referral2 should appear. Upon selection of Other, label class=Other and input class=Other2 should appear (and referral should hide).

2
  • You missed putting the code Commented Dec 18, 2012 at 3:34
  • @raghow Sorry, I updated the post with more code Commented Dec 18, 2012 at 4:00

2 Answers 2

2

try this script just change the variables used.

<script type="text/javascript">
function ChangeDropdowns(value){
    if(value=="los"){
        document.getElementById('provider').style.display='none';
    }else if(value=="icm"){
        document.getElementById('provider').style.display='block';
    }
}
</script>

<select id="abonnement" name="abonnement" onchange="ChangeDropdowns(this.value);">
    <option value="">Selecteer soort</option>
    <option value="los">Los toestel</option>
    <option value="icm">Toestel icm abonnement</option>
</select>

<select id="provider" name="provider">
    <option value="">Selecteer een provider</option>
    <option value="">Maakt niet uit</option>
</select>
Sign up to request clarification or add additional context in comments.

1 Comment

I'm no longer using IDs. Now the elements are selected by class
1

I think it might be quicker/easier to use JQuery unless pure JS is a requirement.

Working Example: http://jsfiddle.net/p8ARq/2/

JQuery

$("select").change(function () {
    // hide all optional elements
    $('.optional').css('display','none');

    $("select option:selected").each(function () {
        if($(this).val() == "Referral") {
            $('.referral').css('display','block');
        } else if($(this).val() == "Other") {
            $('.other').css('display','block');
        }
    });
});

HTML

<form class="contact" name="contact" action="#" method="post">
    <label>How did you hear about us:</label>
    <div id="styled-select">
        <select name="how" class="dropdown">
            <option value="Internet Search">Internet Search</option>
            <option value="Facebook" >Facebook</option>
            <option value="Twitter" >Twitter</option>
            <option value="LinkedIN" >LinkedIN</option>
            <option value="Referral" >Referral</option>
            <option value="Other">Other</option>
        </select>
     </div>
    <label class="optional referral" style="display:none;">Referred By:</label>
    <input class="optional referral" name="Referral2" style="display:none;" class="hidden-txt">
    <label class="optional other" style="display:none;">Please Specify:</label>
    <input class="optional other" name="Other2" value="" style="display:none;" class="hidden-txt">
    <label for="signup" class="text-checkbox">Add me to your email list</label>
    <input type="checkbox" name="signup" value="Yes" class="checkbox">
    <button class="send">Submit</button>                 
</form>​

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.