0

I have been trying to get a simple JS function to work for the longest while but its just not working. Console says "test is not a function.

echo '<input type="hidden" value="'.$return_survey_url.'" id="return_url">';
echo '<form action="http://www.example.com/test.php" onsubmit="return test();" method="post" target="votar">';
echo '<textarea name="tweet" rows="8" cols="10" maxlength="130"></textarea>';
echo '<input type="submit" value="test"/>';
echo '<iframe name="votar" style="display:block; width: 500px; height: 500px;"></iframe>';    
echo '</form>';

The below that I close the PHP tag and have a simple script:

 <script type="text/javascript">
      function test() {
        alert('thank you');
         var return_url = document.getElementById('return_url').value;
         window.location.href= return_url;

     }
  </script>

Whenever I click the submit button console spits out that it is not a function. I am trying to get this to work within a CPT template, this code only needs to happen on that specific template. This same code works here:

https://jsfiddle.net/n7zd1hw8/

  • your code works for me with a copy/paste into a template file, you'll have to give some more context as to where you've got this code exactly. – Milo Feb 14 '16 at 18:18
  • As soon as I click the button console tells me that test is not a function pastebin.com/VTU9eDMZ – Uriahs Victor Feb 14 '16 at 21:32
0

Added ID to form submit botton and removed onSubmit event:

echo '<input type="hidden" value="'.$return_survey_url.'" id="return_url">';
echo '<form action="http://www.example.com/test.php" method="post" target="votar">';
echo '<textarea name="tweet" rows="8" cols="10" maxlength="130"></textarea>';
echo '<input type="submit" value="test" id="test"/>';
echo '<iframe name="votar" style="display:none;"></iframe>';    
echo '</form>';

Added JS in noconflict mode and called function when submit button click:

  <script type="text/javascript">
  jQuery(function($) {
    // Code that uses jQuery's $ can follow here.
    $( "#test" ).click(function() {
      alert('some message');
      var return_url = document.getElementById('return_url').value;
      window.location.href= return_url;
    });

});
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.