javascript - Change CSS if input value has specfic text ( Using plus minus buttons) -
i have input box plus , minus buttons
want show div pete (display: block)
if input box reaches value 50.
tried using keyup works if manually put 50 value , not using plus minus buttons (in case, have disabled input).
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <p id="pete" style="display:none;">pete</p> <div class="sp-quantity"> <div class="sp-minus fff"> <a class="ddd" href="#">-</a> </div> <div class="sp-input"> <input type="text" class="quntity-input" value="52" disabled /> </div> <div class="sp-plus fff"> <a class="ddd" href="#">+</a> </div> </div>
jquery:
$(".ddd").on("click", function() { var $button = $(this); var oldvalue = $button.closest('.sp-quantity').find("input.quntity-input").val(); if ($button.text() == "+") { var newval = parsefloat(oldvalue) + 1; } else { // don't allow decrementing below 0 if (oldvalue > 0) { var newval = parsefloat(oldvalue) - 1; } else { newval = 0; } } $button.closest('.sp-quantity').find("input.quntity-input").val(newval); }); $(".quntity-input").keyup(function() { if (this.value == "50") { $("#pete").css("display", "block"); } else { $("#pete").css("display", "none"); } });
css:
.sp-quantity { width: 124px; height: 42px; font-family: "proximanova bold", helvetica, arial; } .sp-minus { width: 40px; height: 40px; border: 1px solid #e1e1e1; float: left; text-align: center; } .sp-input { width: 40px; height: 40px; border: 1px solid #e1e1e1; border-left: 0px solid black; float: left; } .sp-plus { width: 40px; height: 40px; border: 1px solid #e1e1e1; border-left: 0px solid #e1e1e1; float: left; text-align: center; } .sp-input input { width: 30px; height: 34px; text-align: center; font-family: "proximanova bold", helvetica, arial; border: none; } .sp-input input:focus { border: 1px solid #e1e1e1; border: none; } .sp-minus a, .sp-plus { display: block; width: 100%; height: 100%; padding-top: 5px; }
a few tweaks , you're there. think best achieved via custom event on input. see fiddle.
var $button = $(this); var inputcontrol = $button.closest('.sp-quantity').find("input.quntity-input"); var oldvalue = inputcontrol.val(); if ($button.text() == "+") { var newval = parsefloat(oldvalue) + 1; } else { // don't allow decrementing below 0 if (oldvalue > 0) { var newval = parsefloat(oldvalue) - 1; } else { newval = 0; } } if (newval != oldvalue) { inputcontrol.val(newval); inputcontrol.trigger( 'value-updated'); } }); $(".ddd").closest('.sp-quantity').find("input.quntity-input").on( 'value-updated', function() { if ($(this).val() == "50" ) { $('p#pete').show(); } else { $('p#pete').hide(); } }); $(".quntity-input").keyup(function() { if (this.value == "50") { $("#pete").css("display", "block"); } else { $("#pete").css("display", "none"); } });
Comments
Post a Comment