0

Looking to have it so that when the first checkbox is selected (geographic_checkbox), a further 2 checkboxes are displayed (regions,cities &towns_checkbox and ringfencing_checkbox), but for some reason my code is not working.

$('#geographic_checkbox').on('click', function() {
  if (this.checked) {
    $('#geographic_suboptions').removeClass('hidden');
  } else {
    $('#geographic_suboptions').addClass('hidden');
  }
});
.hidden {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="targeting">
  <h1>Targeting</h1>
  <ul>
    <li>
      <input type="checkbox" name="geographic" value="0.00" id="geographic_checkbox" onclick="GeographicFunction()">Geographic<br>
      <p id="geographic_text" style="display:none">+£0.08 CPC</p>
      <div id="geographic_suboptions" class="hidden">
        <ul>
          <li><input type="checkbox" name="regions,cities&towns" value="0.08" id="regions,cities&towns_checkbox" onclick="RegionsFunction()">Regions, Cities & Towns<br></li>
          <p id="regions,cities&towns_text" style="display:none">+£0.08 CPC</p>
          <li><input type="checkbox" name="ringfencing" value="0.09" id="ringfencing_checkbox" onclick="RingfencingFunction()">Ring-Fencing<br></li>
          <p id="ringfencing_text" style="display:none">+£0.08 CPC</p>
        </ul>
      </div>
    </li>
  </ul>
</div>

5
  • 2
    Your current code seems to be working fine for me: jsfiddle.net/AndrewL64/76ujpd2v Commented Jan 29, 2020 at 16:35
  • Your code is already working. Commented Jan 29, 2020 at 16:35
  • just tried and it doesn't seem to work on safari or chrome for me Commented Jan 29, 2020 at 16:37
  • Check the console in dev tools for errors. Also ensure that the snippet above matches your code exactly. Commented Jan 29, 2020 at 16:40
  • @bboooooyintheuk Can you please upvote usefull answer ? Thanks ;) Commented Jan 29, 2020 at 18:14

1 Answer 1

1

Actually for such a feature you not really need javascript since css provide the :checked selector:

.hidden {
  display: none;
}

ul input:checked ~ #geographic_suboptions {
  display: inline;
}
<div class="targeting">
  <h1>Targeting</h1>
  <ul>
    <li>
      <input type="checkbox" name="geographic" value="0.00" id="geographic_checkbox">Geographic<br>
      <p id="geographic_text" style="display:none">+£0.08 CPC</p>
      <div id="geographic_suboptions" class="hidden">
        <ul>
          <li><input type="checkbox" name="regions,cities&towns" value="0.08" id="regions,cities&towns_checkbox">Regions, Cities & Towns<br></li>
          <p id="regions,cities&towns_text" style="display:none">+£0.08 CPC</p>
          <li><input type="checkbox" name="ringfencing" value="0.09" id="ringfencing_checkbox">Ring-Fencing<br></li>
          <p id="ringfencing_text" style="display:none">+£0.08 CPC</p>
        </ul>
      </div>
    </li>
  </ul>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.