PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 15, 2022

[FIXED] how to set add and subtract value min=0 and max=10 with jquery

 October 15, 2022     dom, javascript, jquery     No comments   

Issue

how to set add and subtract input value using custom button wiht rule min=0 and max=10, it's almost similar to <input type="number" value="0" min="0" max="10" />, here is my code examples, i created button add and subtract to change the input value with jquery, but the value not have min and max rule, what should i do to create it?

$(function(){

  var x = $('input').val();
  
  $('#plus').click(function(){
    $('input').val(++x)
  });
  
  $('#minus').click(function(){
    $('input').val(--x)
  });

});
.n{display:flex}

input{margin:0 8px 0 8px}

.b{
padding: 4px;
background: yellow;
cursor: pointer;
}

.b:hover{
background: blue;
color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Quantity</p>
<div class="n">
  <div id="minus" class="b">
    minus
  </div>
  <input type="text" value='0'>
  <div id="plus" class="b">
    plus
  </div>
</div>


Solution

$(function(){

  var x = $('input').val();
  
  $('#plus').click(function(){
    $('input').val(x<10?++x:x)
  });
  
  $('#minus').click(function(){
    $('input').val(x>0?--x:x)
  });

});
.n{display:flex}

input{margin:0 8px 0 8px}

.b{
padding: 4px;
background: yellow;
cursor: pointer;
}

.b:hover{
background: blue;
color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Quantity</p>
<div class="n">
  <div id="minus" class="b">
    minus
  </div>
  <input type="text" value='0'>
  <div id="plus" class="b">
    plus
  </div>
</div>



Answered By - Shovan
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing