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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:33:27+00:00 2026-06-09T16:33:27+00:00

I have browsed the forum and tried afew things mentioned in these links: Check

  • 0

I have browsed the forum and tried afew things mentioned in these links:
Check if dropdown's selected option is not the first with JavaScript
Get index of select option in an optgroup with jquery

I am still rather basic in my knowledge of Javascript, mostly just using it for form actions. I’ve never used jQuery (yet), and from what I’ve read I don’t need to for what I’m doing (so please no jQuery solutions!)

What I am trying to do is have content show based on which button/option is clicked/selected.
Step 1 works fine (it just shows the dropdown box when the “not a student” button is clicked. No problems there.

Step 2 I am currently stuck on. (And I know there will be problems with step 3, but I have not got to looking at them yet).

Step 2 needs to show a form based on whether an option in the dropdown was selected. Possibly (I am not sure yet) I will require to have a different form for each option selected (but I think once i have the base code that i can work this out using “if” and “else”).

Here is my HTML document, with the JS coding in the head.

<head>
<title>Tims Form</title>
<style type="text/css">
.errormsg {
    color: red;
    font-style: italic;
    display:none;
    }

#interest {
    display:none;
    }

#msgform {
    display:none;
    }
</style>

<script type="text/javascript">
    function reveal()
        {
            document.getElementById("interest").style.display="block";
        }

    function hide()
        {
            document.getElementById("interest").style.display = "none";
        }

    function showForm()
            /* to display form only when dropdown selection made and submit-button pressed */
        {
            var sel=document.getElementById('interest_choice');

            if (sel.options[sel.selectedIndex].value == "0")
                {
                    document.getElementById("msgform").style.display = "block";
                    document.getElementById("selection_error").style.display = "none";
                }

            else { document.getElementById("msgform").style.display = "none"; 
                    document.getElementById("selection_error").style.display = "block";
                }
        }

    function checkForm()
        {
            var is_valid=true;
            var email=document.getElementById("email");

            /* not a blank field validation */
            if (document.getElementById("firstname").value == "")
                {
                    document.getElementById("firstname_error").style.display = "block";
                    is_valid=false;
                }
            else { document.getElementById("firstname_error").style.display = "none"; }

            if (document.getElementById("lastname").value == "")
                {
                    document.getElementById("lastname_error").style.display = "block";
                    is_valid=false;
                }
            else { document.getElementById("lastname_error").style.display = "none"; }

            if (document.getElementById("subject").value == "")
                {
                    document.getElementById("subject_error").style.display = "block";
                    is_valid=false;
                }
            else { document.getElementById("subject_error").style.display = "none"; }

            if (document.getElementById("message").value == "")
                {
                    document.getElementById("message_error").style.display = "block";
                    is_valid=false;
                }
            else { document.getElementById("message_error").style.display = "none"; }

            /* email validation */
            if (email.value =="")
                {
                    document.getElementById("email_blank").style.display = "block";
                    is_valid=false;
                }
            else { document.getElementById("email_blank").style.display = "none"; }

            if (email.value.indexOf("@") == -1)
                {
                    document.getElementById("email_valid").style.display = "block";
                    is_valid=false;
                }
            else { document.getElementById("email_valid").style.display = "none"; }

            if (email.value.indexOf("@") ==0)
                {
                    document.getElementById("email_valid").style.display = "block";
                    is_valid=false;
                }
            else { document.getElementById("email_valid").style.display = "none"; }

            if (email.value.lastIndexOf(".")<email.value.indexOf("@") )
                {
                    document.getElementById("email_blank").style.display = "block";
                    is_valid=false;
                }
            else { document.getElementById("email_blank").style.display = "none"; }

            if (email.value.lastIndexOf(".")>((email.value.length*1)-3))
                {
                    document.getElementById("email_valid").style.display = "block";
                    is_valid=false;
                }
            else { document.getElementById("email_valid").style.display = "none"; }

            /* Phone data must be numeric validation */
            if (document.getElementById("phone").value="")
                {
                    document.getElementById("phone_error").style.display = "block";
                    is_valid=false;
                }
            else { document.getElementById("phone_error").style.display = "none"; }

            if (isNaN(document.getElementById("phone").value))
                {
                    document.getElementById("phone_error").style.display = "block";
                    is_valid=false;
                }
            else { document.getElementById("phone_error").style.display = "none"; }

        return is_valid;
        }

</script>

</head>

<body>
<!-- onclick to show the div with id="interest" when Not A Student is selected -->
    I am: <br/>
    <button id="student" onclick="hide()">A Student</button>
    <button id="not_student" onclick="reveal()">Not A Student</button>

<!-- if not a student -->
<div id="interest">
What area are you interested in studying?

<!-- dropdown (select) list with option groups 
need a submit button to return the value before the form will show?
-->
<form>
<select id="interest_choice">
    <optgroup label="Arts">
        <option value="arts">Arts degree</option>
    </optgroup>
    <optgroup label="Business/Economics">
        <option value="be_undergrad">My first degree (undergraduate)</option>
        <option value="be_postgrad">Postgraduate</option>
    </optgroup>
    <optgroup label="Science">
        <option value="science">Science degree</option>
    </optgroup>
    <optgroup label="Human Sciences">
        <option value="hs_undergrad">Undergraduate</option>
        <option value="psych_postgrad">Postgraduate Psychology</option>
        <option value="ling_postgrad">Postgraduate Linguistics</option>
        <option value="edu_postgrad">Postgraduate Education</option>
    </optgroup>
</select>
<button onclick="showForm()">Enquiry Form</button>
<div class="errormsg" id="selection_error">Please make a selection above.</div>
</form>
</div>

<!-- form -->
<div id="msgform">
<form onsubmit="return checkForm();">
    <fieldset>
        <label>
            First Name:
            <input type="text" name="firstname" id="firstname">
            <div class="errormsg" id="firstname_error">Please enter your first name</div>
        </label>
        <br/>
        <label>
            Last Name:
            <input type="text" name="lastname" id="lastname">
            <div class="errormsg" id="lastname_error">Please enter your last name</div>
        </label>
        <br/>
        <label>
            Email:
            <input type="text" name="email" id="email">
            <div class="errormsg" id="email_blank">Please enter your email address</div>
            <div class="errormsg" id="email_valid">Please enter a valid email address</div>
        </label>
        <br/>
        <label>
            Phone:
            <input type="text" name="phone" id="phone">
            <div class="errormsg" id="phone_error">Please enter a numeric phone number. Do not include ().</div>
        </label>
        <br/>
        <label>
            Subject: (Please only submit one question per enquiry. You can submit multiple enquiries.)<br/>
            <input type="text" name="subject" id="subject">
            <div class="errormsg" id="subject_error">Please enter a subject</div>
        </label>
        <br/>
        <label>
            Message:
            <textarea name="message" id="message"></textarea>
            <div class="errormsg" id="message_error">Please enter a message</div>
        </label>
    </fieldset>
    <input type="submit" value="Submit Form">
</form>
</div>

</body>

Currently when I click “Enquiry Form” button it just refreshes the page back to the first buttons.

And explanation and help would be appreciated!
Thanks.

  • 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-09T16:33:28+00:00Added an answer on June 9, 2026 at 4:33 pm

    Two things…

    Firstly, with your button that triggers showForm, as in:

    <button onclick="showForm()">Enquiry Form</button>
    

    put it outside of the form element, as in:

    </form>
    <button onclick="showForm()">Enquiry Form</button>
    <div class="errormsg" id="selection_error">Please make a selection above.</div>
    

    The button automatically submits the form when it’s within the form tag, therefor it refreshes the page.

    Secondly in your checkForm function, the value you are checking seems to be wrong… Try this instead:

    if (sel.options[sel.selectedIndex].value)
    {
        document.getElementById("msgform").style.display = "block";
        document.getElementById("selection_error").style.display = "none";
    }
    
    else { document.getElementById("msgform").style.display = "none"; 
        document.getElementById("selection_error").style.display = "block";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have browsed thru other postings on S/O, but I can't find a solution
I have browsed lift's MegaProtoUser and encountered this construction: ??(Last Name) . Can anyone
I have an ASP.NET web application the entire site is browsed over HTTPS using
I definetely stuck here because i've tried almost everything on these posts but no
Scrollviewcustomcontrol not sending correct ID to display the desired output. We have two textviews
Have the following for my contact form im building only its not sending... Ive
I have tried using fopen, file-get-contents and a curl request but I always get
Does someone know how should I have my System.out.println() displayed in the logcat.I tried
So far in this project I have two reports in VS2008/BIDS. The first one
Hello i am using Zend Framework Form and have tried to get this example

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.