Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9003699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:44:41+00:00 2026-06-16T00:44:41+00:00

I already have the following code to show/hide two form elements based on a

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:44:42+00:00Added an answer on June 16, 2026 at 12:44 am

    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>​
    

    ​

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've have already implemented pagination using the following code: public Paginacao<Anuncio> consultarPaginado(int pagina, Integer
I have the following code: public partial class Form1 : Form { public BindingList<Class>
I have the following (javascript/jquery) code to show a busy indicator (after a delay)
I have the following code that is used to show the name of the
So I have the following code that I am using to create a form
I have the following code to show event on a calendar Private scheduleData As
I have the following Problem: I have already opened a JQuery dialog and I
sending mail along with embedded image using asp.net I have already used following but
Currently I have to write the following to update an element already contained in
I was already running a sub-website under following path http://example.com/sub-site/ Now, I have created

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.