JavaScript Unable to get closest element with certain class jQuery

I have a function that catches whether the input has changed or not then if the value is exceeding the max it sets the value to the max. So you can not enter a value over the max. But if you try to do this I want to display an error message but I am struggling to get the closest error message. Here is my code;

$(document).on('keydown keyup', '.quantity input', function(e) {
  var max = Number($(this).attr('max'));

  if ($(this).val() > max) {
    e.preventDefault();
    $(this).val(max);
    $(this).closest('.quant_warn').show();
  } else if ($(this).val() < 1) {
    e.preventDefault();
    $(this).val(1);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="quantity">
  <label>Quantity: </label>

  <div class="increment">
    <span></span><span></span>
  </div>

  <input type="text" value="1" min="1" max="10">

  <div class="decrement">
    <span></span>
  </div>

  <div class="quant_warn" style="display: none;">only 10 in stock</div>
</div>
Answer:1

Since the .quant_warn div isn't a parent of the input but a sibling, you need to use siblings() instead :

$(this).siblings('.quant_warn').show();

NOTE : It will be more efficient to use input event instead of keydown keyup when you track the use inputs.

$(document).on('input', '.quantity input', function(e) {
  var max = Number($(this).attr('max'));

  if ($(this).val() > max) {
    e.preventDefault();
    $(this).val(max);
    $(this).siblings('.quant_warn').show();
  } else if ($(this).val() < 1) {
    e.preventDefault();
    $(this).val(1);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="quantity">
  <label>Quantity: </label>

  <div class="increment">
    <span></span><span></span>
  </div>

  <input type="text" value="1" min="1" max="10">

  <div class="decrement">
    <span></span>
  </div>

  <div class="quant_warn" style="display: none;">only 10 in stock</div>
</div>
Answer:2

I have Set() object with three key value var myset =new Set(); myset.add('first','This is first value'); myset.add('second','This is second value'); myset.add('third','This is third value'); Using ...

I have Set() object with three key value var myset =new Set(); myset.add('first','This is first value'); myset.add('second','This is second value'); myset.add('third','This is third value'); Using ...

  1. individual value at risk
  2. individual value of privacy
  3. individual value in excel
  4. individual value of friendship
  5. individual value of human life
  6. individual value of
  7. individual value in english

How can I display this code &#8203; for the user to copy without adding spaces I tried to add code tags <code>&#8203;</code> but didn't work how can I do it ?

How can I display this code &#8203; for the user to copy without adding spaces I tried to add code tags <code>&#8203;</code> but didn't work how can I do it ?

  1. html display blank image
  2. html display blank space
  3. display blank html

I'mm trying to design a specific type of navbar in javascript/jquery. I cannot get mouseenter() and mouseleave() to work correctly when the mouse passes between the li objects. Here is my code. Any ...

I'mm trying to design a specific type of navbar in javascript/jquery. I cannot get mouseenter() and mouseleave() to work correctly when the mouse passes between the li objects. Here is my code. Any ...

  1. mouseenter mouseleave not working
  2. jquery mouseenter mouseleave not working
  3. mouseenter mouseleave

From an input field I am sending the value as an argument to the function that sets state. I have multiple input fields so would like to use their name (which is equal to their state key) to then use ...

From an input field I am sending the value as an argument to the function that sets state. I have multiple input fields so would like to use their name (which is equal to their state key) to then use ...