0

I have a javascript there is looking like this:

jQuery("ul[data-attribute_name=attribute_pa_afhentning-og-levering] li").click(function(){
    var selectedlevering = jQuery("ul[data-attribute_name=attribute_pa_afhentning-og-levering]").find("li.selected").attr("data-value");
    if (selectedlevering == "afhentning"){
        jQuery("ul[data-attribute_name=attribute_pa_packages] li[data-value=afhentning] span").click();
        jQuery("ul[data-attribute_name=attribute_pa_packages] li[data-value=afhentning]").hide(); // hide the "afhentning" option in "Packages" if you like
    }
});

I need to call that script from my functions.php in Wordpress. But how am I doing that?

Best regards

Mv27
  • 550
  • 6
  • 17
  • You can't directly call client-side javascript from PHP. – Jonnix May 30 '19 at 09:40
  • Send an AJAX request to a file that calls the functions you need. http://api.jquery.com/jquery.ajax/ – Mav May 30 '19 at 09:40
  • I hope you don't mean you *really* want to [call JavaScript functions from PHP](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming)... – CD001 May 30 '19 at 09:41
  • Thank you for the answers. It was just a guy who said that the javascript would not run, unless I called the script in the functions.php. I was thinking of just putting the script in style.js in my child theme. But I can see that it is not called: https://dev.onlinerelation.dk/produkt/stack/ – Mv27 May 30 '19 at 09:46

1 Answers1

1

You can choose to add the custom script in the head section by adding the wp_head as the required action.

//----------------------------------------------------------------------
// Custom script in head section, add these lines in your functions.php
//----------------------------------------------------------------------

function custom_script_name(){
?>
<script>
// Your script here
</script>
<?php
}
add_action('wp_head', 'custom_script_name');