Rich-text Reply

JS: Changing variable within if/else statements. Works on my local environment, not on Optimizely.

mkstvnsn 10-23-16

JS: Changing variable within if/else statements. Works on my local environment, not on Optimizely.

Hey guys. Im trying to change the value of a variable within if/else statements but it's not working with Optimizely. It works fine on my local machine. Below is some code. Any reason this would work fine anywhere but in the Optimizely editor?

 

	mike = false;
		jQuery('input').validate(function(result) {
			if (result.type.name == 'name1'){
				mike = false;
			}
			else if (result.type.name == 'name2'){
				mike = true;
			}
			else if (result.type.name == 'name3'){
				mike = true;
			}
		});
Level 2

robertchan 10-24-16
 

Re: JS: Changing variable within if/else statements. Works on my local environment, not on Optimizel

Off the top of my head, I'd say you could test scoping out certain elements to window, for example, window.result.etc. Also not sure if what you're sharing is just a snippet of a full script, but you should declare variables with var, for example, var mike as opposed to mike (just good practice).

Finally, one other issue I see is that you have a race condition so elements are not yet available when you're trying to access them. You can first test to see if this is the case by setting a time out on your code and seeing if everything works. If it does, then you could revise to use a set interval to account for slower internet connections and/or use condition activation (you can refer to the Optimizely article here: https://help.optimizely.com/Build_Campaigns_and_Experiments/Activation_Mode%3A_Activating_an_experim...).
Robert Chan

Experimentation Hero