aaaz
aaaz

Reputation: 3

How to make the chosen value of select list visible

How I can make the selected choice still visible until I re-choose another one?The list may contain more than 10 options

=====

    <form method="get" action="Chairman.php" >

        <select name="courses" id="courses" class="styled-select"  >
         <option value="courses"><--Courses--></option>
         <option value="PHYS220">Physics for Engineers</option>
         <option value="MATH210">Calculus II</option>
         <option value="MATH225">Linear Algebra with Applications</option>
      </select>

       <input type="submit" value="Search Instructor"  
       onClick="checkDropdown()"></input>
        <div id="error" style="color:red"></div>

===== Also when I am trying to validate the select list,The error is displayed and then quickly it disappears

   <script>
    function checkDropdown () {

   var courses = document.getElementById('courses');
       if(courses.value==="courses") {  

    document.getElementById('error').innerHTML="PLEASE selecttt";

    return false;  
       }
         }

              </script>

Upvotes: 0

Views: 85

Answers (1)

Guffa
Guffa

Reputation: 700302

The select is reset and the error disappears because the form is still posted. The value that you return from the function doesn't stop the submission.

You should use the onsubmit event on the form instead of onclick on the button. Use return in the event code to convey the value from the function back to the event:

<form method="get" action="Chairman.php" onsubmit="return checkDropdown()">

Upvotes: 2

Related Questions