views:

236

answers:

1

Here's what I want to achieve, I have a select box that loads in a few rows, when the select is changed (onchange) the key is AJAXed to a PHP function that returns an array in the JSON format, my code then clears the select box options and applies the values I have just returned from my PHP function, great.

The problem is when the new values are set it fires the onchange event again, this sets the values and we're at the beginning of a never ending loop. Firebug catches the error and spits it out.

My question is is there a way to disable the onchange event so it only fires once? I'm coding in a framework based around prototype, posting a code example would be a bit heavy, however I can attempt to fill in any missing data upon request.

Cheers! ILMV

+1  A: 

Using a flag variable. Don't worry about stopping onchange from taking place, instead wrap its logic in a conditional based upon a flag variable.

Basically you'll just create a variable canChange, and set it to false when you're programmatically updating the input. Then, within the onchange logic, check the value of that variable. If it's false, don't do anything, but toggle it back to true for the next onchange event. If it's true, do your typical ajax request for new data.

Jonathan Sampson
Thanks for your reply, I figured I might have to go down that route, I will go and have a play and report back with my findings :-)
ILMV
@ILMV: Best of luck!
Jonathan Sampson
Worked great, many thanks Jonathan!
ILMV