Rousto
Rousto

Reputation: 15

Call JS function in Wordpress, noob question?

I have simple JavaScript function

function hide()
{
    document.getElementById('div_sc_1').style.display = "none";
}

in my themefolder/js file I put

function add_scripts(){
    wp_enqueue_script( 'quest', 
        get_stylesheet_directory_uri().'/js/quest.js', 
        array('jquery'), 
        '1.0.0', 
        false
    );
}
add_action( 'wp_enqueue_scripts', 'add_scripts' );

in my function.php.

I can call my function from an <input type="submit" onClick="hide();">

BUT I want make something like that in my function.php

<?php
if(isset($_GET[xxx]))
{
<script>
hide()
</script>
}
?>

I don't understand why it doesn't work.

Upvotes: 0

Views: 60

Answers (1)

Wasim Sayyed
Wasim Sayyed

Reputation: 611

Since you are adding it directly in the dom, so It may run before the target element even exists, Javascript requires an element to available first in order to do its job.

You have to add it once the document is fully loaded so that it can work. Maybe try this jQuery(document).ready(function($) { hide(); });

If you add it after the document is ready then Javascript will be able to find your element and execute related functions.

Upvotes: 1

Related Questions