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?

1
  • 3
    $(function(){ $("#testni").val("petra"); }), Use .val() to set value and correct DOM ready handler Commented Apr 17, 2018 at 11:37

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

6 Comments

value in field is still la, it has to be petra
Then you have another error somewhere else. Also, make sure you have only 1 id="testni".
I tried on new clean website and it still does not work
Did you loaded jQuery above this code? Do you have any error in the console?
<script src="code.jquery.com/jquery-3.1.1.js"></script> <script type="text/javascript"> $(function(){ $("#testni").val("petra"); }); </script>
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.