Fire rule when Enter Key is pressed. Adobe DTM - forms

I have this code in my Custom code section of an event based rule in DTM. I am trying to fire the rule upon the Enter key press. The input is not within a form element. How to I get the Keycode scoped into my custom page code? Any help would be appreciated!
jQuery(this).keypress(function (e) {
if (e.keyCode == 13) {
var frmData = 'search:new:submit';
var inpData = jQuery(this).siblings('input').val().trim().toLowerCase();
_satellite.setVar('frmData', frmData);
_satellite.setVar('inpData', inpData);
return true;
}
});

I got it to fire by switching the event type to Keypress and using this simple code. -cheers
if (event.keyCode == 13){
return true;
}

Related

Yii2 - Submit Active-form only on click over submit button, not at pressing enter key

I'm working in an active form and adding some fields dynamically using a GridView, but when I have to filter data and I press the enter key (on GridView to begin filtering) the form is submitted.
How to avoid "submitting" on press Enter key?
Refer Link
$('#formid').on('keyup keypress', function(e) {
var keyCode = e.keyCode || e.which;
if (keyCode === 13) {
e.preventDefault();
return false;
}
});

bootstrap-tagsinput form submited on press enter key

in bootstrap-tagsinput on press enter key for next tag form is submitted!
what is the solution?
$("#inputID").tagsinput();
First, you'll want to map 'Enter' to your tagsinput's confirmkeys option, then you'll need to call preventDefault() on your enter key to prevent the form from being submitted. In my solution, it only prevents submission by enter key while the user is focused in the tags input field.
To add a bit of redundancy, I also re-map the enter key to a comma in the input field, just to be sure.
$('#tags-input').tagsinput({
confirmKeys: [13, 188]
});
$('#tags-input input').on('keypress', function(e){
if (e.keyCode == 13){
e.keyCode = 188;
e.preventDefault();
};
});
Just also an FYI, Enter key is mapped as 13, and comma is 188 in JS.
Try this:
$('.bootstrap-tagsinput input').keydown(function( event ) {
if ( event.which == 13 ) {
$(this).blur();
$(this).focus();
return false;
}
})
Go to tagsinput.js
Find line:
self.$container.on('keypress', 'input', $.proxy(function(event) {
Before the end of this function add following:
if (event.which == 13) {
return false; // dont submit form !!
}
Enter key is register for postback form . that's why a case has been happened that when you hit 'enter' key it trigger the 'postback' event so the form goes to submitted .
solution :
on page load reserve the enter key for tag input :
#using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { onkeydown = "return event.keyCode!=13" }))
if using asp.net mvc framework this is a simple example .
This work for me
$('.bootstrap-tagsinput input[type=text]').on('keydown', function (e) {
if ( event.which == 13 ) {
$(this).blur();
$(this).focus();
return false;
}
});
The cancelConfirmKeysOnEmpty option is set to true by default, but when set to false calls .preventDefault on the tag delimeter keydown event. Set it to false.
This solution also fixes the issue of a "carried comma".
cancelConfirmKeysOnEmpty: false,
// If the field is empty, let the event triggered fire as usual
if (self.options.cancelConfirmKeysOnEmpty === false) {
event.preventDefault();
}

Dont want to allow manual tagging in bootstrap tokenfiled

I am using bootstrap-tokenfield http://sliptree.github.io/bootstrap-tokenfield/ with jquery autocomplete. I want tagging with autocomplete content(from dropdown only). When user is inputting something and hit enter key it automatically creates tags. I dont want to allow this feature to the users. Is there any option/flag to set it? Any help would be appreciated. Thanks in advance.
Using listener tokenfield:createtoken you can validate the token before creating it.
Info: http://sliptree.github.io/bootstrap-tokenfield/#events
$('#tokenfield').on('tokenfield:createtoken', function (event) {
var exists = false;
$.each(yourSource, function(index, value) {
if (event.attrs.value === value) {
exists = true;
}
});
if(!exists) {
event.preventDefault(); //prevents creation of token
}
});

Pressing enter key creates a blank span in tinymce editor

I am using tinyMCE editor in one of my project. When I press enter key inside the equation box,I want to reload the contents of the editor.It works well now but the problem I am facing here is an empty span with class AM that is automatically created on pressing enter key.The tinymce function for reloading the contents is :
tinyMCE.execCommand("mceRepaint");
I have made a javascript functin for this which is given below :
function loadlistener() {
//console.log('load');
$("#elm1_ifr").contents().keydown(function (event) {
//console.log('key');
var code = (event.keyCode ? event.keyCode : event.which);
//console.log(code);
if (code == 13 && amedit) {
tinyMCE.execCommand("mceRepaint");
tinyMCE.activeEditor.focus();
}
});
var p_parent = $("#elm1_ifr").contents().find("p");
setInterval(function () {
if (p_parent.find('span').length && p_parent.find('span')) {
amedit = true;
} else {
amedit = false;
}
}, 200)
};
So my question is how to prevent the creation of this empty span when enter key is pressed.
You can check it live Here
Pleas inspect the html so you can understand my question more clearly.

How to fire place_changed event for Google places auto-complete on Enter key

The click seems to fire the event and set the cookies but pressing enter to submit doesn't set the cookies and instead the page redirects without the cookies.
function locationAuto() {
$('.search-location').focus(function () {
autocomplete = new google.maps.places.Autocomplete(this);
searchbox = this;
google.maps.event.addListener(autocomplete, 'place_changed', function () {
var thisplace = autocomplete.getPlace();
if (thisplace.geometry.location != null) {
$.cookie.raw = true;
$.cookie('location', searchbox.value, { expires: 1 });
$.cookie('geo', thisplace.geometry.location, { expires: 1 });
}
});
});
The .search-location is a class on multiple textboxes.
There is a submit button that takes the values from the cookies and redirects (server side)
Adapted from Jonathan Caulfield's answer:
$('.search-location').keypress(function(e) {
if (e.which == 13) {
google.maps.event.trigger(autocomplete, 'place_changed');
return false;
}
});
I've encountered this problem as well, and came up with a good solution. In my website I wanted to save the autocomplete.getPlace().formatted_address in a hidden input prior to submission. This worked as expected when clicking the form's submit button, but not when pressing the Enter key on the selection in the autocomplete's dropdown menu. My solution was as follows:
$(document).ready(function() {
// Empty the value on page load
$("#formattedAddress").val("");
// variable to indicate whether or not enter has been pressed on the input
var enterPressedInForm = false;
var input = document.getElementById("inputName");
var options = {
componentRestrictions: {country: 'uk'}
};
autocomplete = new google.maps.places.Autocomplete(input, options);
$("#formName").submit(function(e) {
// Only submit the form if information has been stored in our hidden input
return $("#formattedAddress").val().length > 0;
});
$("#inputName").bind("keypress", function(e) {
if(e.keyCode == 13) {
// Note that simply triggering the 'place_changed' event in here would not suffice, as this would just create an object with the name as typed in the input field, and no other information, as that has still not been retrieved at this point.
// We change this variable to indicate that enter has been pressed in our input field
enterPressedInForm = true;
}
});
// This event seems to fire twice when pressing enter on a search result. The first time getPlace() is undefined, and the next time it has the data. This is why the following logic has been added.
google.maps.event.addListener(autocomplete, 'place_changed', function () {
// If getPlace() is not undefined (so if it exists), store the formatted_address (or whatever data is relevant to you) in the hidden input.
if(autocomplete.getPlace() !== undefined) {
$("#formattedAddress").val(autocomplete.getPlace().formatted_address);
}
// If enter has been pressed, submit the form.
if(enterPressedInForm) {
$("#formName").submit();
}
});
});
This solution seems to work well.
Both of the above responses are good answers for the general question of firing a question when the user presses "enter." However - I ran into a more specific problem when using Google Places Autocomplete, which might have been part of the OP's problem. For the place_changed event to do anything useful, the user needs to have selected one of the autocomplete options. If you just trigger 'place_changed', the if () block is skipped and the cookie isn't set.
There's a very good answer to the second part of the question here:
https://stackoverflow.com/a/11703018/1314762
NOTE: amirnissim's answer, not the chosen answer, is the one to use for reasons you'll run into if you have more than one autocomplete input on the same page.
Maybe not the most user friendly solution but you could use JQuery to disable the enter key press.
Something like this...
$('.search-location').keypress(function(e) {
if (e.which == 13) {
return false;
}
});