1

I have a dropdown list of, for instance occupations, and when the user clicks the option "Other" , a textbox will show up and the user will have to specify his occupation. The part where the user chooses "Other" and input his occupation works, it saves what he input, but when the user is going to just choose or select from the dropdown list, the value is not being recorded when I submit the form.

How will get the value from the select option? Because I only get the value when the user select "Other" and input his occupation. Here's my code.

function CheckOccupation(val){
 var element=document.getElementById('occupation');
 if(val=='Other')
   element.style.display='block';
 else  
   element.style.display='none';
}
<select class="form-control" name="occupation" onchange='CheckOccupation(this.value);'>
 <option> Select Occupation</option>
 <option> Lawyer </option>
 <option> Nurse </option>
 <option> Lawyer </option>
 <option> Teacher </option>
 <option> Programmer </option>
 <option> Accountant </option>
 <option> Other </option>
</select>

<input type="text" name="occupation" id="occupation" class="form-control" placeholder="please specify your occupation..." style='display:none;'/>
James
  • 21
  • 4

8 Answers8

0

The contents of the <option> element aren't automatically their value. You have to specify a value attribute.

function CheckOccupation(val){
 var element=document.getElementById('occupation');
 if(val=='Other')
   element.style.display='block';
 else  
   element.style.display='none';
}
<select class="form-control" name="occupation" onchange='CheckOccupation(this.value);'>
 <option> Select Occupation</option>
 <option> Lawyer </option>
 <option> Nurse </option>
 <option> Lawyer </option>
 <option> Teacher </option>
 <option> Programmer </option>
 <option> Accountant </option>
 <option value="Other"> Other </option>
</select>

<input type="text" name="occupation" id="occupation" class="form-control" placeholder="please specify your occupation..." style='display:none;'/>
user3297291
  • 22,592
  • 4
  • 29
  • 45
0

I think you might need to specify some value within the option tag. Try this:

<select class="form-control" name="occupation" onchange='CheckOccupation(this.value);'>
 <option value="select"> Select Occupation</option>
 <option value="lawyer"> Lawyer </option>
 <option value="nurse"> Nurse </option>
 <option value="lawyer"> Lawyer </option>
 <option value="teacher"> Teacher </option>
 <option value="programmer"> Programmer </option>
 <option value="accountant"> Accountant </option>
 <option> Other </option>
</select>
rubentd
  • 1,245
  • 11
  • 25
0

You have set the name="occupation" for both the fields. Try using different names for both .

<select class="form-control" name="occupation" onchange='CheckOccupation(this.value);'>

<input type="text" name="occupation" id="occupation" class="form-control" placeholder="please specify your occupation..." style='display:none;'/>
Atul Sharma
  • 9,397
  • 10
  • 38
  • 65
0

Set a value attribute for your option fields and also provide different name for both the fields

JS

function CheckOccupation(val){
 var element=document.getElementById('occupation');
 if(val==='Other')
   element.style.display='block';
 else  
   element.style.display='none';
}

HTML

<select class="form-control" name="occupation1" onchange='CheckOccupation(this.value);'>
    <option> Select Occupation</option>
    <option value="Lawyer"> Lawyer </option>
    <option value="Nurse"> Nurse </option>
    <option value="Lawyer"> Lawyer </option>
    <option value="Teacher"> Teacher </option>
    <option value="Programmer"> Programmer </option>
    <option value="Accountant"> Accountant </option>
    <option value="Other"> Other </option>
</select>

<input type="text" name="occupation2" id="occupation" class="form-control" placeholder="please specify your occupation..." style='display:none;'/>
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
0

See this. I've created a Plunker for you: Plunker link

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>

  <body>
    <select class="form-control" name="occupation" onchange='CheckOccupation(this.value);'>
      <option value="select"> Select Occupation</option>
      <option value="lawyer"> Lawyer </option>
      <option value="nurse"> Nurse </option>
      <option value="lawyer"> Lawyer </option>
      <option value="teacher"> Teacher </option>
      <option value="programmer"> Programmer </option>
      <option value="accountant"> Accountant </option>
      <option value="other"> Other </option>
    </select>

    <input type="text" name="occupation" id="occupation" class="form-control" placeholder="please specify your occupation..." style='display:none;'/>

    <script>
      function CheckOccupation(val) {
        console.log(val);
         var element=document.getElementById('occupation');
         if(val=='other')
             document.getElementById('occupation').style.display='block';
           else  
             document.getElementById('occupation').style.display='none';
      }
    </script>

  </body>
</html>

Try not to use var element.

karel
  • 5,489
  • 46
  • 45
  • 50
Kaushal Suthar
  • 410
  • 5
  • 24
0

This should work:

function CheckOccupation(selectedType){
  if (selectedType == 'Other'){
  document.getElementById('occupation').style.display = 'block';
  
 } else{
  document.getElementById('occupation').style.display = 'none';
   
 }
}
<select class="form-control" name="occupation" onchange='CheckOccupation(this.value);'>
 <option> Select Occupation</option>
 <option value="Lawyer"> Lawyer </option>
 <option value="Nurse"> Nurse </option>
 <option value="Teacher"> Teacher </option>
 <option value="Programmer"> Programmer </option>
 <option value="Accountant"> Accountant </option>
 <option value="Other"> Other </option>
</select>

<input type="text" name="occupation_other" id="occupation" class="form-control" placeholder="please specify" style='display:none;'/>
Marin N.
  • 366
  • 3
  • 12
0

the problem is that you named select (occupation) and then named input (occupation)

they couldn't be sent by same name , try to change any one of them

Mohammed Elhag
  • 4,272
  • 1
  • 10
  • 18
  • Thank you for submitting an answer, my problem is solved. I hope I can rate everyone who gave a correct response, but my privilege is limited. – James Apr 25 '16 at 14:52
0

Try substituting event for this.value as parameter passed to CheckOccupation ; including value attribute at option elements; changing name value at #occupation to "other" ; also removing duplicate <option> having .innerHTML "Lawyer"

<script>
  function CheckOccupation(event) {
    var val = event.target.value;
    var element = document.getElementById("occupation");
    if (val == "Other")
      element.style.display = "block";
    else
      element.style.display = "none";
    console.log(val)
  }
</script>

<select class="form-control" name="occupation" onchange='CheckOccupation(event);'>
  <option>Select Occupation</option>
  <option value="Lawyer">Lawyer</option>
  <option value="Nurse">Nurse</option>
  <option value="Teacher">Teacher</option>
  <option value="Programmer">Programmer</option>
  <option value="Accountant">Accountant</option>
  <option value="Other">Other</option>
</select>

<input type="text" name="other" id="occupation" class="form-control" placeholder="please specify your occupation..." style='display:none;' />
guest271314
  • 1
  • 15
  • 104
  • 177