Petra Žagar
Petra Žagar

Reputation: 1

How to call js jQuery from functions.php in Wordpress

I have this code on Wordpress post:

<input type=text id="testni" value="la">

and this code in functions.php:

<script type="text/javascript">
          $function(){
        $("#testni").attr("value", "petra");


          }
        </script>

This does not work. I guess i have to add some php code?

How to call JS from php?

Upvotes: 1

Views: 2532

Answers (2)

Vidya
Vidya

Reputation: 126

If you need to call js in wordpress.. You need to add action in functions.php file. Try the below method.

<?php
add_action('wp_footer','custom_script');
function custom_script(){
 echo "<script type='text/javascript'>
          $function(){
        $('#testni').attr('value', 'petra');


          }
        </script>";
}
?>

Or else simply add your script in header.php or footer.php.

Upvotes: 0

Binar Web
Binar Web

Reputation: 943

What you want is this

<script type="text/javascript">
$(function(){
    $("#testni").val("petra");
});
</script>

Read about DOM ready
Read about setting an input value

Upvotes: 1

Related Questions