'How to prevent page reload after alert box on key down event?
I want to prevent page reload after clicking ok on alert box. click search button is ok. But now I meet problem in enter key down event. Someone help me please.
This is my html
<div id="email">
<input type="text" id="txtSearchEmail" name="email" onkeydown="Javascript: if (event.keyCode==13) {JS_Search(); event.preventDefault();}" style="width: 161px; height: 16px; padding: 2px; border: 1px solid #a8a8a8; font-size: 13px; color: #434343" />
</div>
<div id="search">
<a class="gai-button" style="width: 96px; height: 25px;" onclick="JS_Search();"><img src="images/findamessage_search.jpg" alt="" /></a>
</div>
This is my javascript
if() {
//some coding
}
else {
alert ("Not found!");
return false;
}
Solution 1:[1]
Remove "Javascript: " from your onkeydown handler, text in this attribute already Javascript. If this button in <form> tag - use "onsubmit" attribute/event in <form> tag.
Solution 2:[2]
Maybe this is what you want, maybe not, in this example i use jQuery:
$(function() {
$("#search a.gai-button").click(function(ev) {
if() {
//some coding
}
else {
alert ("Not found!");
//This prevents the a tag from
ev.preventDefault();
return false;
}
})
};
})
Solution 3:[3]
Add break; at the end of an event and modify your ifthenelse block to reload window when true.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ivan Solntsev |
| Solution 2 | BjarkeCK |
| Solution 3 | Arch |
