Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing onchange events from getting into an infinite loop

I have two text fields, one represents a percentage and the second represents a corresponding amount. Now what I want is, when the percentage changes the amount should change and when the amount changes the percentage should change accordingly. I am using javascript.

In this case, if any of them changes an infinite loop will start.

What I would like is that when the amount changes, it should change the percentage accordingly and then the loop should stop. And when the percentage changes, it should change the amount accordingly and then the loop should stop. Is there any way for this? Maybe something like stopPropagation ?

Dummy JS Code:

Here value is one text box and percentage is another text box

function valueChng(){
    percentage.value = "" + someInteger;
}
function percentageChng(){
    value.value = "" + someInteger;
}

Thanks!!

like image 271
kanishk Avatar asked Feb 21 '23 20:02

kanishk


1 Answers

I still don't see a problem here, as a change handler is not executed when the content is changed programmatically. I just implemented a simple fiddle to show how it is working without running into in infinite loop (using jquery for simplicity): http://jsfiddle.net/aWuks/

like image 147
Simon Avatar answered Apr 07 '23 02:04

Simon