3

I am trying to use EE conditional statement within Javascript. The Javascript is called through a EE snippet. Part of the problem is I am using js conditionals right below my EE conditional. Is this even possible with a EE if statement? Do I need to escape my if statement some how? Any suggestions would be greatly appreciated. Here is a simplification of my snippet:

<script type="text/javascript"> 
...

{if add_option_on=="On"}
if (thisForm.os2.value == "") {
 alert('Please choose a {exp:channel:entries channel="products" disable="member_data|pagination" limit="1"}{customize_option_label}{/exp:channel:entries}');
 return false;
}
{/if}

...

</script>
4
  • What is add_option_on? Is that a global variable? Commented Dec 1, 2012 at 3:42
  • No it's a channel field {add_option_on} Commented Dec 1, 2012 at 3:45
  • Of the products channel? Commented Dec 1, 2012 at 3:46
  • yes {add_option_on} is a checkbox type channel field of the products channel. Thanks @Siebird for helping me clarify my question. Commented Dec 1, 2012 at 3:51

1 Answer 1

9

Try turning off javascript protection to allow the conditionals to parse: $config['protect_javascript'] = 'n';

Also, I would wrap the channel entries tag around the conditional like so:

<script type="text/javascript"> 
...
{exp:channel:entries channel="products" disable="member_data|pagination" limit="1"}
{if add_option_on=="On"}
if (thisForm.os2.value == "") {
 alert('Please choose a {customize_option_label}');
 return false;
}
{/if}
{/exp:channel:entries}
...

</script>
5
  • Thanks @Siebird that did the trick! Looks like I overlooked having my EE conditional nested within a channel entries tag. Thanks again. Commented Dec 1, 2012 at 4:02
  • Yep no problem! Commented Dec 1, 2012 at 4:04
  • So what is $config['protect_javascript'] = 'n'; doing? Commented Dec 1, 2012 at 4:06
  • You may even be able to get rid of that as it's not a advanced conditional. The protect_javascript config variable prevents the advanced conditionals (ie. if:else) parser from processing anything inbetween the tags. I believe the issue was where you had your channel tags ;) Commented Dec 1, 2012 at 4:11
  • I tried protect_javascript to 'y' and it did not work. I looked this hidden configuration variable up on the EE docs for more explanation. If anyone else is wondering how this is used, you can read more about it here -->. Commented Dec 1, 2012 at 4:23

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.