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

  • Home
  • SEARCH
  • 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 5848713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:53:52+00:00 2026-05-22T12:53:52+00:00

ok. i have a dropdown menu. if i select student. an additional field should

  • 0

ok. i have a dropdown menu. if i select student. an additional field should be added to the form and when i choose teacher..the student specific fields should be remove and teacher input fields should be added.. in short i want to remove #student_profile if i choose #teacher_profile, and vice versa.

heres my code:
Javascript

<script>
$(function() {
    $(".fields").hide();
    $("#role").change(function() {
        switch($(this).val()){ 
            case "3":
                $(".fields").hide().parent().find("#student_profile").show();
                .find("#teacher_profile").addClass('not-active');
                break;
            case "2":
                $(".fields").hide().parent().find("#teacher_profile").show();
                .find("#student_profile").addClass('not-active');

                break;
            }
        });
    });   
    $('#submit').click(function(){
    $('div').remove('.not-active');
    });
</script>

html form

<form action="add" method="post"/>

<h3><a href="#">User</a></h3>
<div><input id="username" name="username"/></div>
<div><label for="password">Password </label> <input name="password" type="password"/></div>
div><label for="role">Role </label>
<select id="role" name="role" class="medium">
<optgroup label="Type of User">
<option value="3" />Student
<option value="2" />Teacher
 </optgroup>
</select>
</div>

<div id="student_profile" class="fields">
<div class="field">
<label for="type">Course</label>
<select id="type" name="stdntCourse" class="medium">
<optgroup  label="Choose Course">
<option value="BS IT" />BS IT
<option value="BS CS" />BS CS
<option value="BS IS" />BS IS
</optgroup>
</select>
</div>
</div>
<div id="teacher_profile" class="fields">
<div class="field">
<label for="type">Department/label>
<select id="type" name="stdntCourse" class="medium">
<optgroup  label="Choose Course">
<option value="BS IT" />cas
<option value="BS CS" />education
<option value="BS IS" />engineering
</optgroup>
</select>
</div>
</div>
<input type="submit" id="submit" value="Add User"/>
</form>
  • 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-22T12:53:53+00:00Added an answer on May 22, 2026 at 12:53 pm

    Your html code is quite a mess (invalid) (wrong nesting, wrong self closing tags etc..)

    <form action="add" method="post">
    
    <h3><a href="#">User</a></h3>
    
        <div><input id="username" name="username"/></div>
        <div>
            <label for="password">Password </label>
            <input name="password" type="password" />
        </div>
        <label for="role">Role </label>
        <select id="role" name="role" class="medium">
            <optgroup label="Type of User">
                <option value="3">Student</option>
                <option value="2">Teacher</option>
             </optgroup>
        </select>
    
        <div id="student_profile" class="fields">
            <div class="field">
                <label for="type">Course</label>
                <select id="type" name="stdntCourse" class="medium">
                    <optgroup  label="Choose Course">
                        <option value="BS IT">BS IT</option>
                        <option value="BS CS">BS CS</option>
                        <option value="BS IS">BS IS</option>
                    </optgroup>
                </select>
            </div>
        </div>
        <div id="teacher_profile" class="fields">
            <div class="field">
                <label for="type">Department</label>
                <select id="type" name="stdntCourse" class="medium">
                    <optgroup  label="Choose Course">
                        <option value="BS IT">cas</option>
                        <option value="BS CS">education</option>
                        <option value="BS IS">engineering</option>
                    </optgroup>
                </select>
            </div>
        </div>
        <input type="submit" id="submit" value="Add User" />
    
    </form>
    

    In your jquery you have some issues becaus eyou use the ; at wrong places making syntax-errors

    Here is a bit simplified version

    $(function() {
        $(".fields").hide();
        $("#role").change(function() {
            switch(this.value){
                case "3":
                    $(".fields").hide()
                                .filter('#student_profile')
                                .show()
                                .removeClass('not-active')
                                .end()
                                .filter('#teacher_profile')
                                .addClass('not-active');
                    break;
                case "2":
                    $(".fields").hide()
                                .filter('#teacher_profile')
                                .show()
                                .removeClass('not-active')
                                .end()
                                .filter('#student_profile')
                                .addClass('not-active');
                    break;
                        }
        }).change();
    
        $('#submit').click(function(){
            $('div').remove('.not-active');
        });
    });   
    

    You also need to invoke the .change() at the first time manually to initialize the form.

    Working demo at http://jsfiddle.net/gaby/qTgGY/1/

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

Sidebar

Related Questions

I have a simple html option/select (dropdown) menu. I want to use JQuery to
I have a little dropdown menu similar <select> , which contolled from external js-function.
I have a model of objects in a dropdown menu: <select id=group_select name=group_select> <option
I have a dropdown menu bounded with values from the db on form load.
I have a simple dropdown menu with some options eg: <select name='music_style' id='palmsForm-music_style'> <option
I have a system where the user can select templates from a dropdown menu
I have a dropdown menu that is filled by a mysql database. I need
The dropdown-menu (built by the select_tag) in my application should call the filter-category-action as
I have a dropdown menu. When I click on Italian in the dropdown, I
I have a dropdown menu and I want to disable/enable a button on my

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.