Prevent enter from writing new line in content edititable div [duplicate] - javascript

This question already has answers here:
Prevent line/paragraph breaks in contentEditable
(11 answers)
Closed 2 years ago.
How can I prevent the user from writing a new line with the enter key in a contenteditable div?
So far I have tried (the id of my editable div is editable:
$('#editable').on('keyup', function(e) {
if (e.which == 13) {
event.preventDefault();
}
});
However this does not work.
Is this possible using JS and jQuery?
Edit
I have been informed that pressing enter does not insert a new line so I have put a picture showing when happens when I press enter inside the editable div.
I would like code to remain on one line basically, even if the user presses enter.

Instead of keyup, use keydown.
const ediatble = document.querySelector('#editable');
ediatble.addEventListener('keydown', e => {
if (e.key === 'Enter') {
e.preventDefault();
}
});
See example on JSFiddle

Related

Calling an AJAX function in jQuery using the Enter Key [duplicate]

This question already has answers here:
How can I detect pressing Enter on the keyboard using jQuery?
(19 answers)
Closed 6 years ago.
I have a textbox in HTML:
<input id="myInfo">
I would like to run an AJAX function as soon as the user enters in some value into the textbox using jQuery. I want the function to be called as soon as the user presses the "Enter"
Can someone please provide a very simple illustration as to how this would be accomplished?
Thank you
You could do something like
document.getElementById('myInfo').addEventListener('keypress', function(event) {
// you could also do keyCode === 13
if (event.key === 'Enter') {
console.log('do ajax request here');
}
})
Demo here: http://codepen.io/3stacks/pen/jyVpQP?editors=1111
$('#myInfo').on('keydown', function(e){
if(e.keyCode == 13)
{
//your code
}
});

Simulate enter key in jquery or javascript [duplicate]

This question already has answers here:
How can I automatically trigger an enter key press event after a certain action is executed?
(4 answers)
Closed 7 years ago.
I am trying to let jquery press the enter for me when the page loads. I already tried a few things, but they did not work. I would rather not use any plugins. So what i want is that jquery puts the focus on my input field and presses enter. This is my current code.
$(document).ready(function() {
$('#test').focus().trigger({ type : 'keypress', which : 13 });
});
You can use jQuery.Event constructor
$('body').on('keypress', function(e) {
console.log(e.which);
});
// Create a new jQuery.Event object with specified event properties.
var e = jQuery.Event("keypress", {
which: 13
});
// trigger an artificial keypress event with which 13
jQuery("body").trigger(e);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
in your on load function/ready function
var e = jQuery.Event("keydown");
e.which = 13;
$('#test').focus();
$("#test").trigger(e);

Jquery - detect the click outside of input [duplicate]

This question already has answers here:
How do I detect a click outside an element?
(91 answers)
Closed 8 years ago.
<input id="ok" />
$('.hor-minimalist-a').on('blur','#ok',function(){
});
with this code above, i can handle events which fire If i leave the input field.
How can I detect if someone clicks outside of inputfield without coming to input. I mean the user never came to this input field before, so there was no focus at all.
$('.hor-minimalist-a').on('?','#ok',function(){
?
});
$(document).on('click', function(e) {
if ( e.target.id != 'ok' ) {
// you clicked something else
}
});
That would capture any click except on the input, but why would you need it ?
To capture only clicks on .hor-minimalist-a and not the entire document, just replace document with that selector in the code above.
How about:
$('.hor-minimalist-a').on('click',':not(#ok)',function(){
That'll register click events within .hor-minimalist-a but outside the input #ok
There is a jquery plugin for that:
http://benalman.com/code/projects/jquery-outside-events/examples/clickoutside/

Enter key reloads Chrome Browser [duplicate]

This question already has answers here:
'Enter key' wont submit form in Firefox, but will in Chrome, why?
(2 answers)
Closed 9 years ago.
I have a inputfield and an eventlistener keypress, in Firefox erverything is fine. If I run it in Chrome and hit the enter key the browser refreshes and the page is loaded again.
$('#iputField').keypress(function(e) {
if (e.which == 13) {
var message = $('#iputField').val();
}
});
Probably the form is submiting, you have to stop the default event propagation to avoid that.
$('#iputField').keypress(function(e) {
if(e.which == 13) {
e.preventDefault();
var message = $('#iputField').val();
}
});
Try saying e.preventDefault(); after the function ends.
If you're using a form, you probably need to call e.preventDefault().
Handling the "Enter" / "Return" key in Chrome
Try preventing the default behaviour:
$('#iputField').keypress(function(e) {
if(e.which == 13) {
e.preventDefault().
var message = $('#iputField').val();
}
});

How do I use the Enter key as an event handler (javascript)? [duplicate]

This question already has answers here:
How can I execute a function on pressing the enter key in an <input> field?
(12 answers)
Closed 4 years ago.
im trying to make my own chat... so i have an input text field, the submit button, isn't even submit, its just a button.... so when the enter key is pressed, i need the value of the input field to appear in my textarea (which is readonly)...
well look.. make long story short, i just want a basic enter key event handler, i know it works perfectly with submit buttons cus you don't need to program anything at all, its default. but my button is type="button" .... so when you press enter nothing happens... how do i trigger my button by pressing enter?
You could make the button type submit, or you can use the onkeyup event handler and check for keycode 13.
Here's a list of key codes: Javascript Char codes/Key codes). You'll have to know how to get the keycode from the event.
edit: an example
HTML:
<input onkeyup="inputKeyUp(event)" ...>
Plain javascript:
function inputKeyUp(e) {
e.which = e.which || e.keyCode;
if(e.which == 13) {
// submit
}
}
Here is a working code snippet for listening for the enter key
$(document).ready(function(){
$(document).bind('keypress',pressed);
});
function pressed(e)
{
if(e.keyCode === 13)
{
alert('enter pressed');
//put button.click() here
}
}
Here is a version of the currently accepted answer (from #entonio) with key instead of keyCode:
HTML:
<input onkeyup="inputKeyUp(event)" ...>
Plain javascript:
function inputKeyUp(e) {
if (e.key === 'Enter') {
// submit
}
}

Categories