I saw from W3C example something like this. An action happens on form submit(an alert). However, when I tried with my own function here, my action doesn't happen (show hidden div). Goal is to show hidden div on form submit using 'GET' request. I am new to javascript/jQuery/ajax, but isn't jQuery supposed make call to server without refreshing page?
not working javascript:
$(document).ready(function(){
$("form").submit(function showDiv(){
document.getElementById("showMe").style.display = "block";
});
});
not working html:
<body>
<form action="" method="GET">
<input type="text" name="LastName" value="Mouse"><br>
<input type="submit" value="Submit">
</form>
<div id="showMe" style="display: none;">
<p>hey this should be hidden</p>
</div>
</body>
Try this:
You need to cancel the form submit before executing your code:
Actually the
jfiddle
you posted is not blocking theform
, it shows you an alert that block all js execution (browser behavior), if you select ok in thealert
, theform
goes throughthe
event.preventDefault()
statement means: don't process anything outside this functionYou need at least one form element (button or input), that has type "submit". Then jQuery .submit() or .on('submit',..) will definitely work.