How to check that period character (.) Is removed from a word in a text box in jQuery?

I want to know if an event can be hit if the period sign . removed from text box. for example, I have a #Quantity text box that accepts numeric values ​​as input with a period sign

enter image description here

and I have a #unitValue dropdown control which has three options

I successfully hit the event with a period sign . , in the following way

 $("#Quantity").keypress(function(e) { if(e.which == 46) { //successfully disabled Gram in Unit } }); 

Now I want to enable the "Gram" option in the drop-down list if the period sign . removed from text box. I have an idea, but not if it is correct. Idea: - At any key press, add each letter to the array. Then press the backspace key to check if the letter in the array index is a period icon . or not if that . then turn on the "gram"

Any help would be appreciated

thanks

+6
source share
3 answers

JS Fiddle - Updated - using .prop() instead

 // attach the functionality on multi events $('#inpt').on('keypress input change', function() { var Gram = $('#slct').children('[value="1"]'); // if the indexOf "." is > -1, then there a dot if ($(this).val().indexOf('.') > -1) { // disable the Gram option Gram.prop('disabled', true); // ====================== this part was added by @SarathChandra // reset the drop down if Gram option was selected if ($('#slct').val() === "1" || $('#slct').val() === null) { $('#slct').val("0"); } // ====================== @SarathChandra Thank you for the improvement } else { // if it the dot was removed, we set the disabled prop to false Gram.prop('disabled', false); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <input id="inpt" type="text"> <select id="slct"> <option value="0">- select -</option> <option value="1">Gram</option> <option value="2">Kilo</option> <option value="3">Quntal</option> </select> 

EDIT: Thanks @SarathChandra added these lines:

 if($('#slct').val() == "1" || $('#slct').val() == null){ $('#slct').val("0"); } 

To fix this when the entered value does not contain . , and Gram was selected. THEN period has been added, it resets the drop down menu.

+5
source

Jsfiddle demo

You can use this.

 $(document).on('keyup paste', '#Quantity', function() { if ($(this).val().indexOf('.') > -1) { // disable the Gram option $('#Unit').children('[value="1"]').prop('disabled', true); } else{ $('#Unit').children('[value="1"]').prop('disabled', false); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <input id="Quantity" type="text"> <select id="Unit"> <option value="0">- select -</option> <option value="1">Gram</option> <option value="2">Kilo</option> <option value="3">Quntal</option> </select> 
+1
source

I would suggest saving the previous text entered and comparing it with the actual text in Input:

 ... var prevText = ""; $("#Quantity").keypress(function(e) { var actualText = $(e.target).text(); if (this.prevText.indexOf(".") !== -1 && actualText.indexOf(".") === -1) { // deleted } this.prevText = actualText; }); ... 
0
source

All Articles