I am new in Firefox OS. I have a search box.
After pressing the submit button it shows results perfectly. But I want to add keyboardevent like press enter button it will show result.
Try this:
$("#SearchText").keydown(function (e) {
var keyCode = e.keyCode || e.which;
if (keyCode == 13) {
//stop CR & do submit here instead
document.forms["myform"].submit();
return false;
}
});
It intercepts every key press in the search text field and checks for Return.
Related
I have four modals that are each a step for filling out and submitting form. each modal has a "Next" button that closes the current open modal and opens the next in the process. I am trying to get the enter key to act as pressing the "next" buttons, and the "submit" button on the last step/modal.
This is my current code for a single modal(looks the same for all modals)
$("#user-lookup-modal").on('shown.bs.modal', function () {
$('#add_assistant_submit').prop('disabled', true);
$(document).keypress(function (e) {
if (e.keycode == 13 || e.which == 13) {
$('#add_assistant_next').trigger("click");
return true;
}
}) ;
});
However, this causes weird behavior where pressing enter in one modal counts as pressing enter in all previous modals as well. I tested that out with console output.
EDIT:
I tried this, which seemed to work but is it a poor solution since it will listen for keypresses at all times?
$(document).keypress(function (e) {
if ( e.keycode == 13 || e.which == 13) {
if ($("#myModal").data('bs.modal') || {isShown: false}).isShown) {
//trigger button
}
//... repeat for other modals
}
});
return false;
I am creating a script that will enable me to submit a form by pressing the Enter key but i do not want it to submit the form when i press Shift+Enter. Rather when i press Shift + Enter i would like the input to expand just like on Facebook comment.
So far I was able to get the form to submit by pressing enter with this
<script>
$(document).ready(function() {
$('#comment').keyup(function(e)
{
if(e.keyCode == 13)
{
var comment = $('#comment').val()
var sid = $('#sid').val()
if(comment == "")
{
/*alert("Please write something in comment.");*/
}
else
{
$("#commentbox").append("<div class='commentarea'>"+comment+"</div>");
$.post("index.php", {sid:sid,comment:comment},function(data)
{
})
$('#comment').val("");
}
}
});
});
</script>
Problem and Question
With this code, when i press Shift and Enter, apparently Jquery recognize it as Enter instead of Shift and Enter, therefore it validates the form .
How to make jquery differentiate between Shift + Enter from Enter , How to make jquery submit the form only when Enter alone is pressed ?
You can catch the shift key with event.shiftKey
$('#comment').keyup(function(e) {
if(e.keyCode === 13 && e.shiftKey) {
// do stuff
}
...
I am creating a webapp for android device using html5,javascript and css3. I have a screen which has a textarea. User can enter his comments in the textarea. On clicking the android enter/return key of keyboard, the cursor should move to next line in the textarea.
Any help on this feature ?
I tried many ways. like
$('#reply').bind('keyup', function(e) {
var data = $('#reply').val();
logger.debug("data = " + data);
$('#reply').text(data.replace(/\n/g,"<br />"));
});
#reply is the id of my textarea.
No luck....
Return key can be checked using this key code 13 and just append newline character to force user to write on newline:
$("#reply").on('keydown', function(e) {
var code = e.keyCode || e.which;
if(code == 13) { //Enter keycode
event.preventDefault();
var s = $(this).val();
$(this).val(s+"\n");
}
});
I have a web application where on one specific screen I have to make sure the user clicked the button using the mouse as opposed to just pressing enter or space.
I have written this code:
$('button').keydown(function (e) {
if (e.which === 10 || e.which === 13 || e.which === 32) {
return false;
}
});
However, this only works for enter. The form can still be submitted by pressing space on a button. I am just wondering what caused this inconsistency and how to get around it?
Edit:
Example fiddle: http://jsfiddle.net/billccn/3JmtY/1/. Check the second check box and pressing enter while the focus is on the button will have no effect. If I further disable the input and expand the keydown trapping to the whole form, then enter cannot be used to submit the form.
Edit 2:
I do have a backup plan which is replacing the button with a link or even a plain div and use the click event to submit the form programmatically. However, extra work is required to make it look like a button so I'd rather use a button is possible.
Just found out: handling space (32) on keyup will prevent the click event.
Updated fiddle: http://jsfiddle.net/3JmtY/2/
Missed the Point of your Question. After some googleing if found the following trick:
Bind the keypress event to your from and listen to it's keycode. If the keycode is 13
(enter), prevent all default actions (event.preventDefaul()) and prevent further event bubbeling ( return false; ).
Her is a fiddler code example:
HTML:
<form id="target" action="destination.html">
<input type="text" value="Hello there" />
<input type="submit" value="Go" />
</form>
<div id="other">Trigger the handler</div>
JavaScript:
$('#target').keypress(function (event) {
var code = event.keyCode || event.which;
if (code == 13) {
event.preventDefault();
return false;
}
});
$('#target').submit(function (event, data2) {
debugger;
alert('test');
return false;
});
Fiddler: http://jsfiddle.net/ggTDs/
Note that the form is not submited when enter is clicked!
Use below code. 13 for Enter key and 32 for Spacebar.
$("#form_id").on('keydown keyup keypress', function( e ) {
if ( e.keyCode == 13 || e.which == 13 || e.which == 32 ) {
e.preventDefault();
return false;
}
});
I want to have a chat box (textarea) where if user press Enter then it should submit the chat, and if user press Shift+Enter then it should enter in a new line.
I tried something but not able to figure out the exact keyup or keydown thing. The code I'm using at the moment is:
$("textarea").keydown(function(e){
if (e.keyCode == 13 && !e.shiftKey)
{
e.preventDefault();
}
});
jsFiddle
Also I want to get the \n in place when Enter+Shift key is pressed.
EDIT
Issue with my code is:-
When i am check the content on client using using alert then it shows the next line. But when i am posting it my rails back end. Then its just a simple string. No new line thing is there.
This is how i am sending chats to rails server.
$.post("/incomingchat", { body:$("#chat_" + group_id).val() },
function(data){
// do something..
});
I've answered this before. It's a little tricky if the caret is not at the end of the textarea:
How do I detect "shift+enter" and generate a new line in Textarea?
$("textarea").keydown(function(e)
{
if (e.keyCode == 13)
{
if (e.shiftKey) {
$(this).val( $(this).val() + "\n" );
}
else {
submitTheChat();
}
}
});
Consider using the keypress event instead. If you run the code in jsfiddle, you'll see that a new line is not added when you press enter.
$("textarea").keypress(function(e){
if (e.keyCode == 13 && !e.shiftKey)
{
e.preventDefault();
//now call the code to submit your form
alert("just enter was pressed");
return;
}
if (e.keyCode == 13 && e.shiftKey)
{
//this is the shift+enter right now it does go to a new line
alert("shift+enter was pressed");
}
});