Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery show or hide elements based on input value?

Hey I would like to know how to show a div and hide another at the same time. And that it only does that when I give an input of 5 or higher:

<div class="InputField InputMeters">
   <input type="tel" name="iFenceMeters" id="FenceMeters" class="AutosubmitCalculator" 
          data-minimum-length="1" tabindex="1" placeholder="00" maxlength="3" value="">
   <div class="FormExclamation Tipped Hidden" id="FormCalculatorExclamationFence">0</div>
</div>

this needs to show: <div id="testje" class="CalculatorRight" style="display:none">

This needs to hide: <div id="SummaryHTML" class="CalculatorRight">

The code I use now that only focuses on input and not on input's value:

$('#FenceMeters').on('input', function() {
      $('#testje').show();
      $('#SummaryHTML').hide();
});
like image 339
Joopie Avatar asked Dec 31 '25 14:12

Joopie


2 Answers

You can use the current value and check using Number.parseInt

$('#FenceMeters').on('input', function() {
  const value = $(this).val();
  if (Number.parseInt(value) >= 5) {
    $('#testje').show();
    $('#SummaryHTML').hide()
  } else {
    $('#testje').hide();
    $('#SummaryHTML').show()
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="InputField InputMeters">
  <input type="tel" name="iFenceMeters" id="FenceMeters" class="AutosubmitCalculator" data-minimum-length="1" tabindex="1" placeholder="00" maxlength="3" value="">
  <div class="FormExclamation Tipped Hidden" id="FormCalculatorExclamationFence">0</div>
</div>

<div id="testje" class="CalculatorRight" style="display:none">hidden</div>

<div id="SummaryHTML" class="CalculatorRight">shown</div>
like image 55
Just code Avatar answered Jan 03 '26 02:01

Just code


try below solution.

$('#FenceMeters').on("change paste keyup", function() {
  var val = $(this).val();
  if (val >= 5) {
    $('#testje').show();
    $('#SummaryHTML').hide();
  } else {
    $('#testje').hide();
    $('#SummaryHTML').show();
  }
});
.hidden {
  display: none;
}

.CalculatorRight {
  border: 1px solid red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="InputField InputMeters">
  <input type="tel" name="iFenceMeters" id="FenceMeters" class="AutosubmitCalculator" data-minimum-length="1" tabindex="1" placeholder="00" maxlength="3" value="">
  <div class="FormExclamation Tipped Hidden" id="FormCalculatorExclamationFence">0</div>
</div>
<div id="testje" class="CalculatorRight hidden"> testje </div>
<br/>
<div id="SummaryHTML" class="CalculatorRight">SummaryHTML </div>
like image 33
Shiv Kumar Baghel Avatar answered Jan 03 '26 03:01

Shiv Kumar Baghel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!