1

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?

Petra Žagar
  • 1
  • 1
  • 8
  • 3
    `$(function(){ $("#testni").val("petra"); })`, Use `.val()` to set value and correct DOM ready handler – Satpal Apr 17 '18 at 11:37

2 Answers2

1

What you want is this

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

Read about DOM ready
Read about setting an input value

Binar Web
  • 867
  • 1
  • 11
  • 26
0

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.

Vidya
  • 126
  • 9