Link to home
Start Free TrialLog in
Avatar of Solutionabc
Solutionabc

asked on

Javascript prevent if statement from running twice

Hi,

I have an input box referenced as $(this)
I have a balance quantity number var name as balanceQ

My code is executing with the onchange property and I want to stop this specific statement from executing twice.

for example

let $(this).val = 3
let balanceQ = -4

because it executes on the onchange the if statement executes twice when the $(this) value is changed back to 0 near the end of the code block.

How could I make this code only execute once when the condition is true?
if($(this).val() > balanceQ && balanceQ < 0) {
       
       
        $(this).parents("td").removeClass("selected");
		alert("Product out of stock");
		$(this).val(0);
		
}

Open in new window

Avatar of edemcs
edemcs
Flag of United States of America image

Why not use a temporary variable that flips to true or "1", etc. So that if that value exists, you do not execute it again.
because if you refresh, the script will start anew, using a flip will not work unless you never leave the script.  From my understanding this is an event, so it will occur anew each time a change occurs....

you will probably need a hiddenfield and store value of 'true' or 'false' there.  When page first loads, value = 'true';
during that function, change it to 'false' so that each time after that, your function checks the value of that field and if its not true, then doesn't run.
Avatar of Solutionabc
Solutionabc

ASKER

I have many input boxes doing the same thing on the same page so by doing it that way it will affect all other instances of the input boxes.

I would like it to only show the alert box once on every change for every instance.

Avatar of leakim971
With the following example the statement run only one time

<html>
<head>
<script src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
<script>
	var balanceQ = -40;
	$(document).ready(function() {
		$("input").change(function() {
			alert("fired!")
			if($(this).val() > balanceQ && balanceQ < 0) {
				$(this).parents("td").removeClass("selected");
				alert("Product out of stock");
				$(this).val(0);
			}
		});
	});
</script>
</head>
<body>
<table><tr><td>
<input type="text">  
</td></tr></table>
</body>
</html>

Open in new window

So it seems you have a selector which return more than one object and you run the code for each of this object
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That looks like what I need but for some reason it is not stopping it from running twice. Here is my complete code.
$(".orderQty span INPUT").change(function(ev){
  
  var s1 = $(this).parent().parent().parent().children('.orderQty');
         var balanceQ = $(s1).text();
        
         
                  balanceQ = parseInt(balanceQ);
         
         if($(this).val() <= balanceQ) {
               
         
          $(this).parents("td").addClass("selected");
           
        } 
        if($(this).val() > balanceQ && balanceQ > 0) {
            $(this).parents("td").removeClass("selected");
           
            $(this).val(balanceQ);
            alert("Not enough product in stock");
        }
        
        
if($(this).val() > balanceQ && balanceQ < 0) {
       
       
        $(this).parents("td").removeClass("selected");
		alert("Product out of stock");
		$(this).val(0);
		
}
	
	if($(this).val() == 0){
	$(this).parents("td").removeClass("selected");
	}
ev.stopImmediatePropagation();
});

Open in new window

I also tried it inside of the if statement that is causing the problem but still did not change the result.
no live link?
sorry, it's an internal portal so I am unable to supply a link. Thanks for your help.
could your ight click on the page, choose view source and post it here?