Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda