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

154
Views
Show Div/Hide if one of the select options from optgroup is selected Javascript

I'm trying to modify and example from this same page but its not working as I want.

I have two groups in a select, and I want chose any option from one of the second group "Unique" and if you select any option from that group show Div, else show nothing.

the html:

<div class="form-floating mb-3 mt-3">
<table>
  <tr>
   <td><input class="form-control input-lg email" type="email" 
name="mail[]" required /> 
</td>
  <td>
    <select class="form-select" name="skills[]" required>
      <optgroup Class="unique" label="Common">
        <option value="Extraction">Extraction</option>
        <option value="Hunting">Hunting</option>
        <option value="Farming">Farming</option>
      </optgroup>
      <optgroup class="unique" label="Unique">
        <option value="Diplomacy">Diplomacy</option>
        <option value="Survival">Survival</option>
      </optgroup>
    </select>
  </td>
<td>
  </tr>
</table>  
</div>  

<div class="mt-5 extras" id="mydiv">
<h4 class="text-dark">Check</h4>
Increased through making items, sewing and cooking.<br>
<input class="form-check-input" type="checkbox" name="checkpoint" 
checked required/>  
</div>

the Js:

function showDiv(divId, element)
{
document.getElementById(divId).style.display = element.value == 
'Diplomacy'  || 'Survival' ? 'block' : 'none';
}

css :

.extras{
display: none;
}
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You can access the optgroup by finding the selected option, using closest() to find the enclosing optgroup tag, then accessing it's label attribute

function doSomething(element) {
  // get optgroup
  let optgroup = element.options[element.options.selectedIndex].closest('optgroup').getAttribute('label')
  console.log(optgroup)
  if (optgroup == 'Unique') {
    console.log("do something ...")
  }
}
.extras {
  display: none;
}
<div class="form-floating mb-3 mt-3">
  <table>
    <tr>
      <td><input class="form-control input-lg email" type="email" name="mail[]" required />
      </td>
      <td>
        <select class="form-select" name="skills[]" onchange='doSomething(this)' required>
          <optgroup Class="unique" label="Common">
            <option value="Extraction">Extraction</option>
            <option value="Hunting">Hunting</option>
            <option value="Farming">Farming</option>
          </optgroup>
          <optgroup class="unique" label="Unique">
            <option value="Diplomacy">Diplomacy</option>
            <option value="Survival">Survival</option>
          </optgroup>
        </select>
      </td>
      <td>
    </tr>
  </table>
</div>

<div class="mt-5 extras" id="mydiv">
  <h4 class="text-dark">Check</h4>
  Increased through making items, sewing and cooking.<br>
  <input class="form-check-input" type="checkbox" name="checkpoint" checked required/>
</div>

about 4 years ago · Santiago Gelvez 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!