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 6743679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:59:25+00:00 2026-05-26T11:59:25+00:00

I have a variety of divs with form fields. Based on a drop-down list,

  • 0

I have a variety of divs with form fields. Based on a drop-down list, i need it to display the div corresponding to that value, and hide all the other divs. (Perhaps there’s a more efficient way to even do that, rather than load all the divs and just hide them, i’m not sure).

What I have right now is this:

<select id="group" name="group">
   <option></option>
   <option value="8">Testing This Stuff</option>
   <option value="9">Testing Other Stuff</option>
</select>

<div id="8" style="display:none;">**form fields**</div>
<div id="9" style="display:none;">**different form fields**</div>

And the current, semi-working javascript:

<script type="text/javascript">
window.onload = function() {
  var eSelect = document.getElementById('group');
  eSelect.onchange = function() {
     var id = eSelect.selectedIndex;
     document.getElementById(id).style.display = 'block';
  }
}
</script>

What i’m getting is ‘id’ is null, so i haven’t been able to work past that part yet.

Part 2 would be hiding the old div and displaying the new div (if they were to change the option selected). Any suggestions?

Solution:

<script type="text/javascript">
    window.onload = function() {
        var current;
        var eSelect = document.getElementById('group');
        eSelect.onchange = function() {
            var id = eSelect.value;
            if (current && current != id) {
                document.getElementById(current).style.display = 'none'
            }
            document.getElementById(id).style.display = 'block';
            current = id; 
        }
    }
</script>

<select id="materialsgroup" class="formInput" name="materialsgroup">
   <option value=""></option>
   <option value="groupid_8">Testing This Stuff</option>
   <option value="groupid_9">Testing Other Stuff</option>
</select>

<div id="groupid_8">**form fields**</div>
<div id="groupid_9">**more form fields**</div>

To make the select and divs, this is what I’ve done:

foreach($groups as $key=>$value)
{
        $valueItem = "groupid_".$value['group_id'];
        $text = $value['title'];
        $htmlString .= sprintf('<option value="%s">%s</option>', $valueItem, $text);
}

echo '<tr>
  <td class="formLabelCell">Group</td>
  <td class="formInputCell">
  <select id="group" class="formInput" name="group">'.$htmlString.'</select></td></tr>';

foreach($groups as $gkey=>$gvalue)
{
   echo '<div id=groupid_'.$groups[$gkey]['group_id'].' style="display:none;">';
   //**Draw the form stuff here**
   echo '</div>';
}
  • 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-05-26T11:59:26+00:00Added an answer on May 26, 2026 at 11:59 am

    Try using

    var id = eSelect.value;
    

    For the second part, if you are using pure javascript (I mean, you’re not using a javascript framework like jQuery) maybe a good solution is to load an array containing the div’s ids and iterate it, hiding the div != eSelect.value

    var arrDivs = new Array("1","2","3");
    for (var i=0; i < arrDivs.length; i++) {
        if (arrDivs[i] == eSelect.value)
            document.getElementById(arrDivs[i]).style.display = 'block';
        else
            document.getElementById(arrDivs[i]).style.display = 'none';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Symfony2 form with a variety of fields, including one optional text
I have a program that works with a variety of files on both the
I have a loop that runs through a variety of websites and I'd like
I have a table that describes a variety of objects in my system (ie.
I have a web application that supports a variety of clients on various platforms
In my Rails application, I have a variety of database tables that contain user
Suppose I have attached a variety of event listeners to various form elements. Later,
I have an HTML page with a variety of divs on it. For instance,
I have a variety of image sets that I'm displaying on a web page
I have a variety of strings which I need to work with, these contain

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.