Home › Forums › General Issues › True/False: false output is empty not zero?
Hi,
I’m using a True/False field to setup some JavaScript variable. When it’s ‘on’ it enables a JavaScript. When it’s ‘off’ it should disable the JavaScript. The ‘on’ scenario works fine. The ‘off’ scenario, however, goes wrong. When the switch is ‘off’ the JavaScript doesn’t work and breaks the JavaScript altogether… It works when I manually set it to 0…
A True/False output is either ‘true’ or nothing. Is this normal? It doesn’t output 0…
In my php template I have: (the_sub_field is a true_false in a flexible content).
<script>
var omd_table_filters = <?php the_sub_field('filters'); ?>;
</script>
In my JavaScript this:
if (omd_table_filters === 1) {
my scripting goes here...
}
Hi @Exelmans Graphics
The true/false field will either output (unsurprisingly!) true or false.
If you want your conditional to work then you should perhaps change it to
if (omd_table_filters === false) {
my scripting goes here...
}
That should work – shout if not
Sorry – just realised that should have been:
if (omd_table_filters === true) {
my scripting goes here...
}
Another thing would be to maybe use == instead of === as the former checks for equality after doing any necessary type conversion.
Finally do you have a fallback in the event that the field returns false? Is there any scripting that relies on the variable you set returning true that’s causing things to break?
Thanks,
== doesn’t change a thing.
I’ve got no fallback. I want to run the script when the true/false is selected. When it’s unselected I want no scripting.
Checkbox checked + if (omd_table_filters === true) => WORKS
Checkbox unchecked + if (omd_table_filters === true) => breaks script. I just expect that the code inside the statement gets ignored. Instead it breaks.
Also, when I echo the true/false when it’s set to ‘off’ the echo is empty. Is this normal?
Okay,
I fixed it myself. I loaded my JS in the footer of the page and now everything works.
The problem was JavaScript related.
HOWEVER: When I echo the true/false when it’s set to ‘off’ the echo is empty. Is this normal?
Thanks for the help!
The topic ‘True/False: false output is empty not zero?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.