• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 481
  • Last Modified:

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

0
Solutionabc
Asked:
Solutionabc
1 Solution
 
edemcsCommented:
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.
0
 
silemoneCommented:
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.
0
 
SolutionabcAuthor Commented:
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.

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
leakim971PluritechnicianCommented:
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

0
 
leakim971PluritechnicianCommented:
So it seems you have a selector which return more than one object and you run the code for each of this object
0
 
leakim971PluritechnicianCommented:
Try this too :

http://api.jquery.com/event.stopImmediatePropagation/
//somewhere 
$("...... you selector(s) .....").change(function(ev) {
//
if($(this).val() > balanceQ && balanceQ < 0) {
       
       
        $(this).parents("td").removeClass("selected");
		alert("Product out of stock");
		$(this).val(0);
     ev.stopImmediatePropagation();		
}

Open in new window

0
 
SolutionabcAuthor Commented:
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

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now