HTML Text Input: Avoid submit when enter is pressed - html

I have an HTML input which is a textfield.
When I am pressing the enter, this will call the submit, which is normal.
Now, I would like to do something else when Enter is clicked on that textBox.
I am trying something like that:
<input type="text"
id="first_page"
onPaste=""
onkeydown="if (event.keyCode == 13) alert('enter')" />
The alert works well but the submit is still done. My page is reloading after that.
Could you help me please.

write return false; and check it

Using jQuery:
<script>
$('#first_page').keypress(function(e) {
if (e.keyCode == '13') {
e.preventDefault();
//your code here
}
});​
</script>
using javascript
<input type="text" id="first_page" onPaste="" onkeydown="if (event.keyCode == 13) { alert('enter');return false;}" />

Other option would be using onkeypress
<input type="text" id="first_page" onPaste="" onkeypress="if (event.keyCode == 13) {alert('enter'); return false;}" />

You could try the below script :
<script>
function checkEnter(event)
{
if (event.keyCode == 13)
{
alert('enter')
return false;
}
}
</script>
And the Html:
<input type="text" id="first_page" onPaste="" onkeydown="return checkEnter(event);" />

You should be returning false in the keydown event handler function in order to prevent further processing of the event.
Also, you might find the following question useful: Prevent Users from submitting form by hitting enter.

write the following code after alert('enter')
return false;

Use this, it worked in mine.
Replace on the keydown:
onkeydown="if (event.keyCode == 13 || event.which == 13) event.preventDefault();"

$(function () {$('#form').validationEngine(); });
It will work in my mvc problem

using jquery you can avoid all submiting or some element
$("#form").submit(function(e) {
e.preventDefault();
});

Related

How to trigger click event when pressing enter key? Text area

I have search area which works fine as I need it to but the problem is when i hit enter it won't search for results, so you need to press button to do so. I tried different codes but nothing is working and I'm wondering if anyone has solution for it. This is code that I have:
<input
data-path=".title"
data-button="#title-search-button"
type="text"
value=""
placeholder="Search..."
data-control-type="textbox"
data-control-name="title-filter"
data-control-action="filter"
/>
I guess this could work with "onkeydown=" but not really sure what to add after it.
I would really appreciate if someone has solution for this.
Although you can use the onkeydown attribute I prefer to do these things through JavaScript event listeners & handlers. If you want to do this with the onkeydown attribute then look at Bryan's answer.
I would first add an ID/Class name to the input so we can easily target it. In my example i will add searchTextas the ID for this input.
JavaScript:
document.getElementById('searchText').addEventListener("keydown",function(e){
if(e.keyCode == 13){
//doSomething()
}
});
jQuery:
$('#searchText').on('keydown',function(e){
if(e.which == '13'){
//doSomething();
}
});
Yes, this would work with keydown. But you will need to add javascript for it to work.
<input
data-path=".title"
data-button="#title-search-button"
type="text"
value=""
placeholder="Search..."
data-control-type="textbox"
data-control-name="title-filter"
data-control-action="filter"
onkeydown="if (event.keyCode == 13) { // Your search results code here;
return false; }"
/>
You will have to add an event listener for keyup for your input field. e.keyCode will give you the special keys (which happens to be 13 for Enter)
Here is an example:
$("#myinput").on('keyup', function (e) {
if (e.keyCode == 13) {
alert("Enter clicked");
// add your code here
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input
id = "myinput"
data-path=".title"
data-button="#title-search-button"
type="text"
value=""
placeholder="Search..."
data-control-type="textbox"
data-control-name="title-filter"
data-control-action="filter"
/>
Hope this helps!
I have given a sample code below using plan javascript. This solution will work without jquery or other libraries.
On the key press event, you can either call the same method of the click event (or) trigger the button click directly as per the option 1 and option 2 mentioned below.
function triggerSearch() {
if(event.keyCode === 13) {
// Option 1: You can call the 'search' method directly
//search();
// Option 2: OR you can trigger the button `click` event
getElement('searchButton').click();
}
}
function search() {
var searchTerm = getElement('searchTextBox').value;
getElement('searchTerm').innerHTML = searchTerm;
}
function getElement(id) {
return document.getElementById(id);
}
<input
data-path=".title"
data-button="#title-search-button"
type="text"
value=""
placeholder="Search..."
data-control-type="textbox"
data-control-name="title-filter"
data-control-action="filter"
id="searchTextBox"
onkeydown="triggerSearch()"
/>
<input type="button" value="Search" id="searchButton" onclick="search()" />
<br/><br/>
Search Term: <span id="searchTerm"></span>

How to keep a form from being submitted if not filled up using javascript?

(I copied the code below from a tutorial but it dose not work out, and I wonder if I should do something specific on the page receiving the submitted data. )
<script>
function stopsubmit()
{
document.getElementById("submit").addEventListener("click",function(event)
{
if(document.getElementById("name").value="")
{
event.preventDefault();document.getElementById("erroeMessage").innerHTML="Please write your name";return false;
}
else
{
return true;
}
}
)
}
</script>
You can use onsubmit and return false to prevent submitting. A form can only have one onsubmit function though.
<form onsubmit="return validate();">
<input id="aField" type="text">
<input type="submit">
</form>
<script>
function validate() {
if(document.getElementById("aField").value.length == 0) {
alert("Hold it right there, buddy!");
return false;
}
return true;
}
</script>
Your code snippet has syntax errors, e.g.
if(document.getElementById("name").value="")
Must double equality instead one.
Your sample is worked in this fiddle (with my sample html):
Jsfiddle link

HTML and submit in input text

I have several input fields text on my page:
<input type="text">
The form is submitted when I press ENTER in some of them, which is what I don't want.
What can cause such behavior?
I triple checked and I do not have submit buttons (actually I do not have any buttons at all)
You could use a bit of Javascript placed in the head section of your HTML page to disable submission on enter clicked in an input field:
<script type="text/javascript">
function stopRKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}
document.onkeypress = stopRKey;
</script>
You are probably submitting some of your forms via ajax calls in which you prevent the default browser behavior. That is why some of your forms are not submitted when you hit the enter.
To make sure all of your forms do not submit when enter is pressed, you can bind a custom function to all of your forms which detects when enter key is pressed. In this function you can prevent the default browser behavior.
$('form').keydown(function(e){
if(e.keyCode === 13) {
e.preventDefault();
return false;
}
});
Example Fiddle here.

How do I stop a html textbox from refreshing on enter key?

I just want to ask how can I stop my html textbox from refreshing when I press enter.
<div id="topSearchBox">
<input id="Text1" type="text" value="search..."/>
<img id="topSearchBoxIcon" alt="search buttom" src="~Images/Search-32x32.png" width="18px" height="18px" />
</div>
I have a script that tells it to do some thing else, but after the script has completed it refreshes the page anyway.
$(document).keyup(function (e) {
if (e.keyCode == 13) { // enter
Search();
}
});
I've read a bit on it, but nothing seems to work.
$(document).keyup(function (e) {
if (e.keyCode == 13) { // enter
Search();
return false; //you can also say e.preventDefault();
}
});
If I well understood, maybe the page refresh because you may have a submit input (or button) after to the input text field so when you hit the enter key you also send the form itself.
try to stop the propagation of the event with
$(document).keyup(function (e) {
if (e.keyCode == 13) { // enter
e.stopPropagation();
Search();
}
})
Return false in the event to stop the default event (submit) taking place. This is assuming that the submit event is being triggered.
$(document).keyup(function (e) {
if (e.keyCode == 13) { // enter
Search();
return false; //this will stop the default event triggering
}
});
You could also put it outside of a form (if it's inside).
Note - Also, it's better if you bound the event to your input directly.
$('#Text1').keyup();
I have answered it here. https://stackoverflow.com/a/16016122/1047337
It's about having only one text box in the form.

Search form, if no value go to site

I have a search form which searchers wikipedia.com. Nothing fancy, a form with an input.
If the input is empty, currently the user is redirected here: http://en.wikipedia.org/w/index.php?title=Special%3ASearch&redirs=1&search=&fulltext=Search&ns0=1 .
Id like to redirect the users here: www.wikipedia.com instead
How can I do this?
This is my search form:
<form method="get" action="http://en.wikipedia.org/w/index.php">
<input type="hidden" value="Special:Search" name="title"/>
<input type="hidden" value="1" name="redirs"/>
<input type="text" name="search">
<input type="hidden" value="Search" name="fulltext"/>
<input type="hidden" value="1" name="ns0"/>
</form>
It's not possible, I don't think, without using JavaScript; if, as you've indicated, you're willing to use jQuery:
$('form').submit(
function(){
if ($(this).find('input:text').val() == ''){
$(this).attr('action','http://www.wikipedia.com/');
}
else {
$(this).submit();
}
});
JS Fiddle demo.
In plain JavaScript, the following seems to work (tested in Chromium 12 and Firefox 7 on Ubuntu 11.04):
var formEl = document.getElementsByTagName('form')[0];
var inputs = formEl.getElementsByTagName('input');
var textInputEl = [];
for(i in inputs){
if (inputs[i].type == 'text'){
textInputEl.push(inputs[i]);
}
}
formEl.onsubmit = function(){
if (textInputEl.value === '' || textInputEl.value === null){
this.action = 'http://www.wikipedia.com/';
}
else {
formEl.submit;
}
};
JS Fiddle demo.
I'm trying to set this up so that, with an empty text-input, the form will remove any hidden elements and then set the action, or the document.location to be http://en.wikipedia.org/wiki/Special:Random. This does not, for some reason, seem to work. Almost as if Wikipedia's finding something wrong and then redirecting to a different page. Ungh. Anyways, this is what I tried:
$('form').submit(
function(){
if ($(this).find('input:text').val() == ''){
$(this).find('input:hidden').remove();
$(this).attr('action','http://en.wikipedia.org/wiki/Special:Random');
}
else {
$(this).submit();
}
});
JS Fiddle demo;
$('form').submit(
function(){
if ($(this).find('input:text').val() == ''){
$(this).find('input:hidden').remove();
document.location.href = 'http://en.wikipedia.org/wiki/Special:Random';
/*
I also tried 'document.location','window.location'
and 'window.location.href' all of which, predictably,
failed in exactly the same way.
*/
}
else {
$(this).submit();
}
});
JS Fiddle demo;