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=".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:
/
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/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/n7zd1hw8/
Share Improve this question asked Feb 14, 2016 at 18:10 Uriahs VictorUriahs Victor 3854 silver badges15 bronze badges 2- 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 Commented Feb 14, 2016 at 18:18
- As soon as I click the button console tells me that test is not a function pastebin/VTU9eDMZ – Uriahs Victor Commented Feb 14, 2016 at 21:32
1 Answer
Reset to default 1Added 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/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>