Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
How to call a JavaScript method based on the Advanced Custom Field's value in WordPress?

I have a custom field in the form of a checkbox in my WordPress website's admin panel. I want to load different JavaScript methods based on the selection of this checkbox. I have already tried the following approach:

<script>
    function setFirstStyle() {
        $('#faq-div').addClass('style-one');
    }

    function setSecondStyle() {
        $('#faq-div').addClass('style-two');
    }

    <?php if(!empty(get_field('my_custom_field')) && get_field('my_custom_field') == "Yes"):?>
            setFirstStyle();
    <?php else: ?>
            setSecondStyle();
    <?php endif; ?>
</script>

I also have put the relevant CSS styles for each style in my .cssfile, but the script is not working properly and I always get the default style (which is style-two) although I change the custom fields value in wp-admin page.

When I add a checkbox to my html code like below and I write a function to listen to that checkbox the issue is solved but I don't want to do this. I want to solve the issue with custom fields and without using any other html element.

<input type="checkbox" id="change-style"/>
<script>
    $('#change-style').on('change', function () {
        if (this.checked) {
            setSecondStyle();
        }
        else {
            setFirstStyle();
        }
    });
</script>

How can I fix this issue in WordPress?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!