On chrome, the "search" event is fired on search inputs when user clicks the clear button.
Is there a way to capture the same event in javascript on Internet Explorer 10?
The only solution I finally found:
// There are 2 events fired on input element when clicking on the clear button:
// mousedown and mouseup.
$("input").bind("mouseup", function(e){
var $input = $(this),
oldValue = $input.val();
if (oldValue == "") return;
// When this event is fired after clicking on the clear button
// the value is not cleared yet. We have to wait for it.
setTimeout(function(){
var newValue = $input.val();
if (newValue == ""){
// Gotcha
$input.trigger("cleared");
}
}, 1);
});
The oninput event fires with this.value set to an empty string. This solved the problem for me, since I want to execute the same action whether they clear the search box with the X or by backspacing. This works in IE 10 only.
Use input instead. It works with the same behaviour under all the browsers.
$(some-input).on("input", function() {
// update panel
});
Why not
$("input").bind('input propertychange', function() {
if (this.value == ""){
$input.trigger("cleared");
}
});
I realize this question has been answered, but the accepted answer did not work in our situation. IE10 did not recognize/fire the $input.trigger("cleared"); statement.
Our final solution replaced that statement with a keydown event on the ENTER key (code 13). For posterity, this is what worked in our case:
$('input[type="text"]').bind("mouseup", function(event) {
var $input = $(this);
var oldValue = $input.val();
if (oldValue == "") {
return;
}
setTimeout(function() {
var newValue = $input.val();
if (newValue == "") {
var enterEvent = $.Event("keydown");
enterEvent.which = 13;
$input.trigger(enterEvent);
}
}, 1);
});
In addition, we wanted to apply this binding only to the "search" inputs, not every input on the page. Naturally, IE made this difficult as well... although we had coded <input type="search"...>, IE rendered them as type="text". That's why the jQuery selector references the type="text".
Cheers!
We can just listen to the input event. Please see the reference for details. This is how I fixed an issue with clear button in Sencha ExtJS on IE:
Ext.define('Override.Ext.form.field.ComboBox', {
override: 'Ext.form.field.ComboBox',
onRender: function () {
this.callParent();
var me = this;
this.inputEl.dom.addEventListener('input', function () {
// do things here
});
}
});
An out of the box solution is to just get rid of the X entirely with CSS:
::-ms-clear { display: none; } /* see https://stackoverflow.com/questions/14007655 */
This has the following benefits:
Much simpler solution - fits on one line
Applies to all inputs so you don't have to have a handler for each input
No risk of breaking javascript with bug in logic (less QA necessary)
Standardizes behavior across browsers - makes IE behave same as chrome in that chrome does not have the X
for my asp.net server control
<asp:TextBox ID="tbSearchName" runat="server" oninput="jsfun_tbSearchName_onchange();"></asp:TextBox>
js
function jsfun_tbSearchName_onchange() {
if (objTbNameSearch.value.trim() == '')
objBTSubmitSearch.setAttribute('disabled', true);
else
objBTSubmitSearch.removeAttribute('disabled');
return false;
}
ref
MSDN onchange event
- tested in IE10.
... or to hide with CSS :
input[type=text]::-ms-clear { display: none; }
The above code was not working in my case and I have changed one line and introduced $input.typeahead('val', ''); which works in my case..
// There are 2 events fired on input element when clicking on the clear button:// mousedown and mouseup.
$("input").on('mouseup', function(e){
var $input = $(this),
oldValue = $input.val();
if (oldValue === ''){
return;
}
// When this event is fired after clicking on the clear button // the value is not cleared yet. We have to wait for it.
setTimeout(function(){
var newValue = $input.val();
if (newValue === ''){
$input.typeahead('val', '');
e.preventDefault();
}
}, 1);
});
Related
I'm able to replace/remove a certain value of an input when a label is clicked, here is my code:
$("#labelid").on("click",function() {
if($("#inputid").val('sometexthere'));
{
$("#inputid").val('');
}
});
The code deletes the value sometexthere each time the label is clicked. I want to limit this to only once, so if the label is clicked for the first time it deletes the value and if it clicked for the second time, it does nothing (leave value as it is).
How can I do this?
Answers would be greatly appreciated.
Use .one() method.
Attach a handler to an event for the elements. The handler is executed at most once per element per event type.
$("#labelid").one("click",function() {
if($("#inputid").val() === 'sometexthere') // remove the ;
{
$("#inputid").val('');
}
});
The val('...')in if($("#inputid").val('sometexthere')) is assigning the value, and not comparing - $("#inputid").val() === '...' .
Note : As j08691 suggests here if its just related to initial text/placeholder use <input placeholder="sometexthere"/>
var click = false;
$("#labelid").on("click",function() {
if(!click){
if($("#inputid").val('sometexthere'));
{
$("#inputid").val('');
}
click = true;
}
});
this will work :)
In your code $("#inputid").val('sometexthere') is always true because what is happening there is setting a value to the field. The val method returns jQuery object so it is basically truthy.
One of the solutions indeed is the .one method which assigns the event listener only once. However, you may need to do other things on the click event. So I'll go with a flag:
var flag = true;
$("#labelid").on("click",function() {
var input = $("#inputid");
var current = input.val();
if(current === 'sometexthere' && flag) {
flag = false;
input.val('');
}
});
And by the way, consider the usage of the placeholder attribute.
In case you've been persuaded by the argument to use a placeholder instead here's an example implementation that includes a fallback for browsers that don't natively support HTML5 Placeholders (ahem <=IE9)...
$(function() {
//test if placeholder is natively supported:
function hasPlaceholder() {
var test = document.createElement('input');
return ('placeholder' in test);
}
//if placeholder is not natively supported initialise this method to replicate the behaviour
if(!hasPlaceholder){
$('[placeholder]').focus(function() {
var $this = $(this);
if ($this.val() == $this.attr('placeholder')) {
$this.val('')
.removeClass('placeholder');
}
}).blur(function() {
var $this = $(this);
if ($this.val() == '' || $this.val() == $this.attr('placeholder')) {
$this.addClass('placeholder')
.val($this.attr('placeholder'));
}
}).blur();
//on submit, make sure we remove any fo placeholder values
$('[placeholder]').parents('form').submit(function() {
$(this).find('[placeholder]').each(function() {
var $this = $(this);
if ($this.val() == $this.attr('placeholder')) {
$this.val('');
}
})
});
}
});
/* You can style placeholders... */
input.placeholder {color:#66aaee;}
::-webkit-input-placeholder {color: #66aaee;}
:-moz-placeholder {color: #66aaee;}
::-moz-placeholder {color: #66aaee;}
:-ms-input-placeholder {color: #66aaee;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Label: <input name="inputname" id="inputid" placeholder="this is the placeholder" /></label>
You could use jQuery.off() after the link is clicked the first time. I think the placeholder is a nice idea, but I don't know if it accomplishes what you want, and if you need backwards compatibility you would need a shim to handle the placeholder property. I agree with the accepted answer if the detachment of the event handler is unconditional.
If detachment of the event handler is conditional, I think this is cleaner:
$("#labelid").on("click",function() {
var $input = $("#inputid");
if($input.val().length > 0) {
$input.val('');
$(this).off("click");
}
});
Basic Problem
input event --calls--> update() --calls--> geoInput.receive() --triggers--> input event
Explanation
Okay I've run into a strange problem which is causing my code to loop infinitely. I have the following bit of jQuery:
var geoInput = $('#Geo').on('input', function() {
_this._controller.update({
geo: this.value
});
}).get(0);
As you can see it's just a basic event listener and it's calling an update function in my controller. At the end of the update function is a method which broadcasts for the geoInput field to update. This is handled by the following:
geoInput.receive = function(formState) {
this.value = formState.geo;
this.placeholder = _this._placeholders.geo;
}
For some reason
this.placeholder = _this._placeholders.geo;
is triggering the input event on that field. You can see how this can be problematic as this creates an infinite loop. I'm sure this is what's happening because when I return before that line the loop doesn't occur. Also, if I change to say, a keyup event, the loop also doesn't occur.
Question
Why is this happening and how can I fix it?
I've tried!
I've looked at this for hours and done quite a few searches to no avail. This code works as expected in Chrome and FF.
The funny thing is that the INPUT event occurs if you just add an item to the INPUT attributes placheholder or value other than the Latin alphabet.
It's problem IE10 and IE11
var $input = $('<input type="text" placeholder="Бубу"/>');
//or
var $input = $('<input type="text" value="世界へようこそ"/>');
$input.on('input',function(){
alert('input even occur');
});
Without seeing your fiddle, I think you can fix the issue with this code:
var previousGeoInput = '';
var geoInput = $('#Geo').on('input', function() {
if (previousGeoInput === this.value) {
return;
}
previousGeoInput = this.value;
_this._controller.update({
geo: this.value
});
}).get(0);
Check the event keycode/charcode if it's 0 then it's IE being stupid and you can just cancel the event.
Anyone know of a good tutorial/method of using Javascript to, onSubmit, change the background color of all empty fields with class="required" ?
Something like this should do the trick, but it's difficult to know exactly what you're looking for without you posting more details:
document.getElementById("myForm").onsubmit = function() {
var fields = this.getElementsByClassName("required"),
sendForm = true;
for(var i = 0; i < fields.length; i++) {
if(!fields[i].value) {
fields[i].style.backgroundColor = "#ff0000";
sendForm = false;
}
else {
//Else block added due to comments about returning colour to normal
fields[i].style.backgroundColor = "#fff";
}
}
if(!sendForm) {
return false;
}
}
This attaches a listener to the onsubmit event of the form with id "myForm". It then gets all elements within that form with a class of "required" (note that getElementsByClassName is not supported in older versions of IE, so you may want to look into alternatives there), loops through that collection, checks the value of each, and changes the background colour if it finds any empty ones. If there are any empty ones, it prevents the form from being submitted.
Here's a working example.
Perhaps something like this:
$(document).ready(function () {
$('form').submit(function () {
$('input, textarea, select', this).foreach(function () {
if ($(this).val() == '') {
$(this).addClass('required');
}
});
});
});
I quickly became a fan of jQuery. The documentation is amazing.
http://docs.jquery.com/Downloading_jQuery
if You decide to give the library a try, then here is your code:
//on DOM ready event
$(document).ready(
// register a 'submit' event for your form
$("#formId").submit(function(event){
// clear the required fields if this is the second time the user is submitting the form
$('.required', this).removeClass("required");
// snag every field of type 'input'.
// filter them, keeping inputs with a '' value
// add the class 'required' to the blank inputs.
$('input', this).filter( function( index ){
var keepMe = false;
if(this.val() == ''){
keepMe = true;
}
return keepMe;
}).addClass("required");
if($(".required", this).length > 0){
event.preventDefault();
}
});
);
I want to do something when a keypress changes the input of a textbox. I figure the keypress event would be best for this, but how do I know if it caused a change? I need to filter out things like pressing the arrow keys, or modifiers... I don't think hardcoding all the values is the best approach.
So how should I do it?
In most browsers, you can use the HTML5 input event for text-type <input> elements:
$("#testbox").on("input", function() {
alert("Value changed!");
});
This doesn't work in IE < 9, but there is a workaround: the propertychange event.
$("#testbox").on("propertychange", function(e) {
if (e.originalEvent.propertyName == "value") {
alert("Value changed!");
}
});
IE 9 supports both, so in that browser it's better to prefer the standards-based input event. This conveniently fires first, so we can remove the handler for propertychange the first time input fires.
Putting it all together (jsFiddle):
var propertyChangeUnbound = false;
$("#testbox").on("propertychange", function(e) {
if (e.originalEvent.propertyName == "value") {
alert("Value changed!");
}
});
$("#testbox").on("input", function() {
if (!propertyChangeUnbound) {
$("#testbox").unbind("propertychange");
propertyChangeUnbound = true;
}
alert("Value changed!");
});
.change() is what you're after
$("#testbox").keyup(function() {
$(this).blur();
$(this).focus();
$(this).val($(this).val()); // fix for IE putting cursor at beginning of input on focus
}).change(function() {
alert("change fired");
});
This is how I would do it: http://jsfiddle.net/JesseAldridge/Pggpt/1/
$('#input1').keyup(function(){
if($('#input1').val() != $('#input1').attr('prev_val'))
$('#input2').val('change')
else
$('#input2').val('no change')
$('#input1').attr('prev_val', $('#input1').val())
})
I came up with this for autosaving a textarea. It uses a combination of the .keyUp() jQuery method to see if the content has changed. And then I update every 5 seconds because I don't want the form getting submitted every time it's changed!!!!
var savePost = false;
jQuery(document).ready(function() {
setInterval('autoSave()', 5000)
$('input, textarea').keyup(function(){
if (!savePost) {
savePost = true;
}
})
})
function autoSave() {
if (savePost) {
savePost = false;
$('#post_submit, #task_submit').click();
}
}
I know it will fire even if the content hasn't changed but it was easier that hardcoding which keys I didn't want it to work for.
I am using jquery to keep the focus on a text box when you click on a specific div. It works well in Internet Explorer but not in Firefox. Any suggestions?
var clickedDiv = false;
$('input').blur(function() { if (clickedDiv) { $('input').focus(); } });
$('div').mousedown(function() { clickedDiv = true; })
.mouseup(function() { clickedDiv = false });
Point to note: the focus() method on a jquery object does not actually focus it: it just cases the focus handler to be invoked! to actually focus the item, you should do this:
var clickedDiv = false;
$('input').blur( function() {
if(clickeddiv) {
$('input').each(function(){this[0].focus()});
}
}
$('div').mousedown(function() { clickedDiv = true; })
.mouseup(function() { clickedDiv = false });
Note that I've used the focus() method on native DOM objects, not jquery objects.
This is a direct (brute force) change to your exact code. However, if I understand what you are trying to do correctly, you are trying to focus an input box when a particular div is clicked when that input is in focus.
Here's my take on how you would do it:
var inFocus = false;
$('#myinput').focus(function() { inFocus = true; })
.blur(function() { inFocus = false; });
$('#mydiv').mousedown(function() {
if( inFocus )
setTimeout( function(){ $('#myinput')[0].focus(); }, 100 );
}
Point to note: I've given a timeout to focussing the input in question, so that the input can actually go out of focus in the mean time. Otherwise we would be giving it focus just before it is about to lose it. As for the decision of 100 ms, its really a fluke here.
Cheers,
jrh
EDIT in response to #Jim's comment
The first method probably did not work because it was the wrong approach to start with.
As for the second question, we should use .focus() on the native DOM object and not on the jQuery wrapper around it because the native .focus() method causes the object to actually grab focus, while the jquery method just calls the event handler associated with the focus event.
So while the jquery method calls the focus event handler, the native method actually grants focus, hence causing the handler to be invoked. It is just unfortunate nomenclature that the name of this method overlaps.
I resolved it by simply replace on blur event by document.onclick and check clicked element if not input or div
var $con = null; //the input object
var $inp = null; // the div object
function bodyClick(eleId){
if (eleId == null || ($inp!= null && $con != null && eleId != $inp.attr('id') &&
eleId != $con.attr('id'))){
$con.hide();
}
}
function hideCon() {
if(clickedDiv){
$con.hide();
}
}
function getEl(){
var ev = arguments[0] || window.event,
origEl = ev.target || ev.srcElement;
eleId = origEl.id;
bodyClick(eleId);
}
document.onclick = getEl;
hope u find it useful