skip to Main Content

Change gender when you enter SA ID number and change select value option in javascript

How to get Gender select option value automatically when you enter ID number input field javascript

2

Answers


  1. Did you mean something like this?

    const setSelectGender = event => {
      const id = document.querySelector("#inputNumber").value
      document.querySelectorAll("#gendersSelect option").forEach(opt => { opt.selected = opt.value == getSexByID(id) ? true : false })
    }
    
    document.querySelector("#inputNumber")
    .addEventListener("change", setSelectGender)
    
    window.addEventListener("keyup", setSelectGender)
    
    const getSexByID = id => {
      strlenth = (id.length-1);
      for (var i=0;i<strlenth;i++){
          if (i == 6){
            return (id[i] > 4) ? 'male' : 'female'
          }
      }
    }
    <form id="form">
      <input type="number" value="5" id="inputNumber">
      <select id="gendersSelect">
        <option value="male" >Male</option>
        <option value="female">Female</option>
      </select>
    </form>
    Login or Signup to reply.
  2. <body>
        <form id="form">
            <input type="number" value="5" id="inputNumber">
            <select id="gendersSelect">
              <option value="male" >Male</option>
              <option value="female">Female</option>
            </select>
          </form>
    
          <script>
            function setGender(event){
                let gen=this.value>5?'male':'female';
                document.getElementById("gendersSelect").value=gen;
            }
            document.getElementById("inputNumber").addEventListener('keyup',setGender);       
        </script>
    </body>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search