Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
Can't able to perform two actions in a javascript on select box

Can't able to perform two actions (i.e,I need to disable and need to unselect the selected option from the select)

  1. I have two select boxes i.e, select_one box is ('1','2','3') & select_two box is ('please select', 'test1', 'test2','test3'...etc)
  2. and i have written onchange on the select_one box
  3. when i selected option 1 or 3 from select_box dropdown then i need to unselect the current value from select_two box and need to display the 'please select' option and also i need to disable the select_two box.

const checkFun = ( value ) => {
                            if( value == '1' || value == '3' )
                            {
                                document.getElementById("select_two").disabled = true;
                                document.getElementById("select_two").options[0].selected = "selected";
                            } else
                            {
                                document.getElementById("select_two").disabled= false
                            }    
                        }
 Select One: <select name="testOne" id="select_one" onchange="checkFun( this.value )"><option value="1">1</option><option value="2">2</option><option value="3">3</option></select>

 Select Two: <select name="testTwo" id="select_two"><option value=""> - please select - </option><option value="test1">test1</option><option value="test2">test2</option><option value="test3">test3</option></select>      

Thanks, KK

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

add value attribute to - please select - option tag like value="0"

  <select id="select_one">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
  </select>

 <select id="select_two">
    <option value="0"> - please select - </option>
    <option value="test1">test1</option>
    <option value="test2">test2</option>
    <option value="test3">test3</option>
 </select> 

<script>
    const boxTwo = document.getElementById('select_two');   
    document.getElementById('select_one').addEventListener('change',function(){
            boxTwo.value = 0;
            if (this.value == 2) {
                boxTwo.disabled = false;
            }else{
                boxTwo.disabled = true;
            }
    },{passive:true});
</script>
about 4 years ago · Juan Pablo Isaza Report

0

it is working as expected!

const checkFun = (value) => {
  if (value == '1' || value == '3') {
    document.getElementById("select_two").disabled = true;
    document.getElementById("select_two").options[0].selected = true;

  } else {
    document.getElementById("select_two").disabled = false
  }
}
<select name="test1" id="select_two">
  <option value=""> - please select -</option>
  <option value="test1">test1</option>
  <option value="test2">test2</option>
  <option value="test3">test3</option>
</select>

<select name="test" id="select_one" onchange="checkFun( this.value )">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

about 4 years ago · Juan Pablo Isaza Report

0

function checkFun(value) {
  const s2 = document.getElementById("select_two");

  s2.value = "";
  s2.disabled = (value == '1' || value == '3');
}
<select name="test" id="select_one" onchange="checkFun( this.value )">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

<select name="test1" id="select_two" disabled>
  <option value=""> - please select -</option>
  <option value="test1">test1</option>
  <option value="test2">test2</option>
  <option value="test3">test3</option>
</select>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!