I have two controls in my page i.e. sap.m.Input and sap.m.CheckBox. There is a change event attached to input field which gives an error bar if input text is not matched with the regex. This change event is triggered on clicking out of focus of the input field. Clicking on checkbox will hide this input and show a third control. Now if i enter something in input field which will not match regex and click on checkbox, It is triggering changeEvent of Input and not even selecting the checkbox. I tried many things like checking in change event whether checkbox is ticked or not, attached a click event on checkbox but no solution.
Please help me so that i will be able to determine the click on checkbox if the current focus is on input field
Here is an example at jsbin. Here error will be thrown if input text contains # and change event is triggered.
var textInput = new sap.m.Text({text:"Area:"})
var inputField =new sap.m.Input("inptId",{
change:function(oEvent){
sap.ui.getCore().byId("barId").setVisible(oEvent.getParameters().value.includes("#"));
}
});
var select = new sap.m.Select("select",
{visible:false,
items:[new sap.ui.core.Item({text:"India"}),
new sap.ui.core.Item({text:"US"}),
new sap.ui.core.Item({text:"UK"})
]
})
var hBox = new sap.m.HBox({items:[textInput,inputField,select]})
var bar = new sap.m.Bar("barId",{
visible:false,
contentMiddle:new sap.m.Text({text:"Error"})
});
var chkBox = new sap.m.CheckBox("chkBxId",{
text:"Select from dropdown",
select:function(oEvent){
var selected = oEvent.getParameters().selected;
sap.ui.getCore().byId("inptId").setVisible(!selected);
sap.ui.getCore().byId("select").setVisible(selected);
sap.ui.getCore().byId("barId").setVisible(false);
}
});
var vBox = new sap.m.VBox({
items:[bar,hBox,chkBox]
});
In this example it is happening sometimes not always but in my project its happening always as there are lot of validations are happening in change event.
#Ashish for focus out try following code.
this.getView().byId("input").addEventDelegate({
onfocusout: function() {
console.log("focus lost !!");
}
}
and for enter key press use following code
.addEventDelegate({
onkeypress: function(e) {
if (e.which === 13) {
do your stuff;
}
}
})
once focus is out it will trigger function provide in onfocusout and while on focus if enter key is pressed it will trigger function provide in onkeypress event.
Also you can merge this in one.
addEventDelegate({
onkeypress: function(e) {
console.log("Enter pressed fouse out", e.which);
},
onfocusout: function() {
do your stuff!
}
})
Related
I have input element which will take input and filter the contents and the filter event will be trigger once the user gets focused out from the input element.
When the user having the focus in the input element and he clicks in one of the button, the click event is invoked first and then the focus out event, as it creates conflicts while generating the filtered content.
I tried changing the order of code and other options such as changing the way of invocation of the click event - none of the ways worked out for me
$('body').on('focusout', '.classname', functionname);
function functionname(e) {
if (typeof e == 'object') {
}
}
$('body').on('click', '.buttonclass', function (e) {});
Could someone help me to build The FocusOut event to trigger first and then the click event.
Based on the current conditions, you have to - inside the click handler - retrieve the validation result, and based on that result, decide if button submission should or should not occur.
JS Code:
$("#input").focusout(function(){
var that = this;
valid = this.value.length ? true : false;
!valid && window.setTimeout(function() {
$(that).focus();
}, 0);
});
$("#button").click(function(e) {
if ( !valid ) { return false; }
e.preventDefault();
alert('execute your filter)');
});
I'm trying to create keyboard for input extra symbols in dynamically created fields. I'm using on() function to handle blur and change events of input form. I'd like to input special chars on caret position. Is it possible to make it without using global variables?
Currently change is noticed if I add letters by keyboard and then loose focus or press enter, or if I type (letter and then special character) || (special character and then special character) without loosing blur or even pressing enter.
// checking click targets
var clicky;
$(document).mousedown(function(e) {
clicky = $(e.target);
});
// handling dbclick and enter press
$(document).on("dblclick", "input#word", function (){
$(this).parent().parent().css('background-color', setOnEditColor);
$(this).prop("readonly", false);
$(this).keypress(function(e) {
if(e.which == 13) {
$(this).focusout();
}
});
});
// handling blur on input form
$(document).on("blur", "input#word", function (e){
lastFocus = $(this);
// checking if keyboardLetter element is clicked,
// if yes I want to keep focus on current input
if(clicky.attr('class') == 'keyboardLetter'){
return false;
}
$(this).prop("readonly", true);
$(this).parent().parent().css('background-color', setDefaultColor);
// $(this).trigger("change");
});
// onChange is triggered on blur
$(document).on("change", "input#word", function (){
saveChanges($(this).closest('tr').attr('id'), $(this).val(), 1);
});
//clicking on special characters
$(document).on("click", ".keyboardLetter", function (){
pos = $(lastFocus).caret(); //getting caret position of focused input
lastFocus.val(lastFocus.val().insertAt(pos, $(this).text().trim()));
$(lastFocus).caret(pos+1);
});
Call .change() after setting its value:
lastFocus.val(lastFocus.val().insertAt(pos, $(this).text().trim()));
$(lastFocus).change(); //this line
I have a text input. It has a change and an input listener. In the input listener, I delete the value of the input, if it is "delete". The change listener simply alerts "Changed.".
Current behaviour:
There is no "Changed." alert when I type "delete" and stop editing, because the comparison base is changed as well.
Desired behaviour:
I want to see the "Changed." alert based on the value at the start of the editing, ignoring all the programatic changes made during the modification.
What is the simplest way of doing this?
Playgorund:
Click the input.
Delete the content.
Type "delete".
See how the text is deleted.
Click somewhere else.
See how the "Changed." alert is not displayed.
var input = document.querySelector('input');
input.addEventListener('change', function() {
alert('Changed.');
});
input.addEventListener('input', function() {
input.value = input.value.replace(/^delete$/, '');
});
<input value="example" />
My current solution is to add an attribute that I store the initial value in on each focus event, and compare it to the current value at the time of the blur event.
var input = document.querySelector('input');
input.addEventListener('focus', function() {
this.setAttribute('data-value-on-focus', this.value);
});
input.addEventListener('blur', function() {
if (this.getAttribute('data-value-on-focus') !== this.value) alert('Changed.');
});
input.addEventListener('input', function() {
this.value = this.value.replace(/^delete$/, '');
});
<input value="example" />
I am trying to disable the submit button if the username textbox field is empty.
$("#txtUserName").bind("input propertychange change keyup paste", setButtonState);
var setButtonState = function () {
if ($("#txtUserName").val().trim() == "") {
$("#login").attr("disabled", true);
}
else {
$("#login").removeAttr("disabled");
}
}
The above code is working fine in all scenarios except that,when the user selects username which is saved earlier(autocomplete).
I cannot set the autocomplete off option for the textbox.
How can I catch the event when user selects text in autocomplete usernames?
Just bind your input field to the additional event select, which is fired when a user selects something from his autocompletion.
This would be your code then. I just added select and rearranged your code a little bit. Also I run setButtonState() once at the domready to be sure it's disabled.
var setButtonState = function () {
if ($("#txtUserName").val().trim() == "") {
$("#login").attr("disabled", true);
}
else {
$("#login").removeAttr("disabled");
}
}
$("#txtUserName").bind("input propertychange change keyup paste select", setButtonState);
setButtonState();
I have also updated the jsfiddle to demonstrate it.
I'm working with Drupal 7, the problem is that I have an input type text for the quantity of the products with 2 arrows in span by default (widget quantity for the add cart button).
When using the js_injector module for getting the 'change()' event of the input, something weird happens and a loop started changing the value of the input for more or less depending of the clicked arrow.
jQuery(function($) {
$(document).ready(function(){
$('#edit-quantity').bind('change keydown keyup click input submit mouseenter', function (e) {alert('Type: ' + e.type); });
});
});
->This works correctly but doesn't get the arrow-span clicked options:
$('#edit-quantity').bind('keydown keyup click input submit mouseenter',...
->These options create a loop:
$('#edit-quantity').bind('change ...',
OR
$('#edit-quantity').change(function (e) {alert('Type: ' + e.type); });
My point is , Why this .change() event creates such a loop? or better how can I stop it to use the event?
Thank you,
You can use "input" instead of change
$('#textbox').on('input', function() {
// do your stuff
});
Better not to use change on text field. Change is more preferred for radio button, select field, check-box etc...
Hope it helps someone, at the end I just did search for all the possibilities of change. Not only in the input also at the 'buttons' represented with span. I did some little trick and ended up like this:
jQuery(function($) {
function value()
{
if($(this).index()!=0) //Just execute once
{
var price=parseFloat(document.getElementsByClassName('field-item')[0].innerHTML.replace(' €','')).toFixed(2);
var quantity = parseFloat($("#edit-quantity").val());
price *= quantity;
var glbvar = price.toFixed(2).replace('.',',')+' €';
//Set Price
document.getElementsByClassName('field-name-commerce-price')[0].getElementsByClassName('field-item')[0].innerHTML = glbvar ;
}
}
$(document).ready(function(){
//When refreshing the value get lost:
if( parseFloat($("#edit-quantity").val()) != 1) value();
/*For input*/ $("#edit-quantity").bind("propertychange keyup paste input",value);
/*For span*/ $('span').bind('click',value);
});
});