bootstrap datetimepicker disabling other fields - javascript

I was wondering what the Bootstrap DateTimePicker used for storing the date? I want to know how I can use that object/text/whatever it may be to disable other textbox fields that I have on my web page.
I have one page setup for querying against multiple GridViews, so I don't want the user to enter information into multiple fields otherwise more than one GridView will be returned. I have gotten the other textbox fields to become disabled, including the DateTimePicker textbox fields, using the below javascript (jquery):
$("#tasknameText").keyup(function (e) {
if ($(this).val() != '') {
$("#textDate").attr('disabled', 'disabled');
$("#beginDate").attr('disabled', 'disabled'); //DateTimePicker Field
$("#endDate").attr('disabled', 'disabled'); //DateTimePicker Field2
$("#beginDate2").attr('disabled', 'disabled'); //DateTimePicker Field3
$("#endDate2").attr('disabled', 'disabled'); //DateTimePicker Field4
} else {
$("#tasknameText").removeAttr('disabled');
$("#textDate").removeAttr('disabled');
$("#beginDate").removeAttr('disabled');
$("#endDate").removeAttr('disabled');
$("#beginDate2").removeAttr('disabled');
$("#endDate2").removeAttr('disabled');
}
});
The above code represents four DateTimePicker fields, or two pairs of Start Date and End Date fields. It works to disable all the other textboxs on the page when I use keyup on a regular textbox. However, keyup only works when the user manually enters a date into the DateTimePicker fields - I need it to work when the user clicks the glyphicon icon and the date is automatically populated as well.

Found a way to make it work - instead of using keyup, i just called all the events in the same line and that seemed disabled the other text fields. Would still like to know which one specifically it is changing on...
$("#beginDate").bind("blur focus focusin focusout load resize scroll unload click" + " dblclick mousedown mouseup mousemove mouseover mouseout mouseenter " + "mouseleave change select submit keydown keypress keyup error", function (e) {

Related

jQuery - how to "activate" input when I move on it with pressing "tab" key?

I have a form and when I move on an input that contains where the user is supposed to enter a date (after clicking into it, I fire datetime picker from jQuery-UI), nothing happen.
When I click into the field, the datatime picker is fired, that's fine. But when I move onto this input by pressing the tab key, the datetime picker is not activated. How do I activate it?
I tried
$(function() {
$('body').on('blur','.date_field', function(e) {
$(this).datepicker({dateFormat: 'yy-mm-dd'}).focus();
}
});
But it doesn't work. I also tried to use keypress or keydown and comparing the key-codes, but it doesn't work neither.
How to activate the datetime by moving on an input by pressing the tab key?
EDIT:
I tried also bind with no success.
Thank you.
js at Question is missing closing ) at event handler. Initialied .datepicker() before blur event. Added tabindex="1" to input element. Actually, blur event is not needed, and would appear to recursively add focus to input element.
$(function() {
$(".date_field").datepicker({dateFormat: "yy-mm-dd"});
//$("body").on("blur",".date_field", function(e) {
// $(this).focus();
//})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js"></script>
Date: <input type="text" class="date_field" tabindex="1">

Jquery keyup issue with dropdown menus?

I have this code, basically I have <input id="register_username" type="text" name="username"> and I want to check if the users already exist in the database or not. The code works perfectly when the user type a username, but sometimes some browsers (for example Firefox) gives the user drop down menu and let them choose some values they already entered in the past (such as their name). The problem is when the chose the username from the drop down menu, the keyup function does not work. How can I fix this problem ?
$("#register_username").live('keyup', function() {
$.post('scripts/register/register_check.php', {
checkusername: $('#register_username').val()
}, function(data) {
if (data == "good") {
//do something
} else {
//do the other thing
}
});
});​
I don't think all browers if any triggers an event after choosing an option from an autocomplete dropdown.
Your best bet is to use .change() which will trigger after the element loses focus. For example, when the user chooses an autocomplete option and move on to the next field.
I would use the onblur event to fire whenever the field loses focus.

Using .change() and a datepicker in jquery to submit a form

Currently I'm using JQuery UI's datepicker range, and I've added some more javascript that should submit the form on the page when different form elements change.
This works on the drop down I've added, as well as the date picker input boxes if I enter dates manually and then click away.
However, if I click an input field for the datepicker, and the date chooser pops up, the form will not submit after choosing a date..
Its as if the input field didn't know a change occured..?
http://api.jquery.com/change/
here is the code I'm using:
$(document).ready(function() {
$('#from').change(function() {
document.myform.submit();
});
});
$(document).ready(function() {
$('#to').change(function() {
document.myform.submit();
});
});
Using the datepicker UI events, I've added the following code for testing but it isn't working.. any ideas?
$(document).ready(function() {
$('#from').datepicker({
onClose: function() { alert('Yo yo!') }
});
});
The jQuery UI DatePicker has custom events, particularly the onSelect event, click the event's tab and it's at the bottom of the docs:
http://jqueryui.com/demos/datepicker/
Double check there are no JavaScript errors above the code you quoted.
If there are errors then the JavaScript code below these errors will not execute properly.
That is probably why the code you added to test did not work either.
The correct answer is here: jQuery datepicker, onSelect won't work
I hope it should be on focus . on click of date picker the focus will be in the text box where you have to show the date.
so the event should be .focus()
http://api.jquery.com/focus/
http://api.jquery.com/focusout/

Jquery : how to trigger an event when the user clear a textbox

i have a function that currently working on .keypress event when the user right something in the textbox it do some code, but i want the same event to be triggered also when the user clear the textbox .change doesn't help since it fires after the user change the focus to something else
Thanks
The keyup event will detect if the user has cleared the box as well (i.e. backspace raises the event but backspace does not raise the keypress event in IE)
$("#inputname").keyup(function() {
if (!this.value) {
alert('The box is empty');
}
});
jsFiddle
As Josh says, this gets fired for every character code that is pressed in the input. This is mostly just showing that you need to use the keyup event to trigger backspace, rather than the keypress event you are currently using.
The solution by Jonathon Bolster does not cover all cases. I adapted it to also cover modifications by cutting and pasting:
$("#inputname").on('change keyup copy paste cut', function() {
//!this.value ...
});
see http://jsfiddle.net/gonfidentschal/XxLq2/
Unfortunately it's not possible to catch the cases where the field's value is set using javascript. If you set the value yourself it's not an issue because you know when you do it... but when you're using a library such as AngularJS that updates the view when the state changes then it can be a bit more work. Or you have to use a timer to check the value.
Also see the answer for Detecting input change in jQuery? which suggests the 'input' event understood by modern browsers. So just:
$("#inputname").on('input', function() {
//!this.value ...
});
Another way that does this in a concise manner is listening for "input" event on textarea/input-type:text fields
/**
* Listens on textarea input.
* Considers: undo, cut, paste, backspc, keyboard input, etc
*/
$("#myContainer").on("input", "textarea", function() {
if (!this.value) {
}
});
You can check the value of the input field inside the on input' function() and combine it with an if/else statement and it will work very well as in the code below :
$( "#myinputid" ).on('input', function() {
if($(this).val() != "") {
//Do action here like in this example am hiding the previous table row
$(this).closest("tr").prev("tr").hide(); //hides previous row
}else{
$(this).closest("tr").prev("tr").show(); //shows previous row
}
});
Inside your .keypress or .keyup function, check to see if the value of the input is empty. For example:
$("#some-input").keyup(function(){
if($(this).val() == "") {
// input is cleared
}
});
<input type="text" id="some-input" />

Detecting autocomplete on form input with jQuery

I have a form that detects if all the text-fields are valid on each keyup() and focus(); if they're all valid, it will enable the submit button for the user to press. However, if the user fills in one of the text inputs with a browsers autocomplete feature, it prevents the submit button from being enabled.
Is there a way to detect if any of the input has changed regardless of how it's been changed, using jQuery?
You could try using on input to detect text-based changes (except keys like ctrl and shift) in <input>'s.
For example:
$(input).on('input', function() {
console.log($(this).val());
});
The jQuery change event will only fire on blur. The keyup event will fire as you type. Neither fire on clicking an auto-completion option. I am also searching for a way to detect this, but I'm currently going with
$(selector).bind("change keyup",function(){
//Do something, probably with $(this).val()
});
But it doesn't quite solve the problem...
Myself I used
$(selector).on("change keyup blur input", function() {});
which did the trick in Chrome. input is what made it work for autocomplete.
My issue was detecting auto-fill (via a plugin like lastpass or 1password) as well as the issue described above.
The solution that worked for me was:
$(function(){
function validate(){
if($('#email').val() !== '' && $('#password').val() !== '')
$('#submit').prop('disabled', false);
else
$('#submit').prop('disabled', true);
}
// Validate after user input
$('#email, #password').on('keyup change', validate);
// Validate on mouse enter of body and login form
// to catch auto-fills from roboform/1password etc...
$('body, #loginform').on('mouseenter', validate);
// Validate onload incase of autocomplete/autofill
validate();
});
See demo in JSFiddle.
You could use the jQuery .change() function.
After the page initially loads, you can validate the entire form, just to check that it is in fact not filled in. After that you can use .change() to check if things have changed on the form, and if anything has changed, validate the form again.
$(document).ready(function() {
// validate form once, just to be sure (if valid, activate submit button)
});
...
<form>
<input class="target" type="text" value="Field 1" />
<select class="target">
<option value="option1" selected="selected">Option 1</option>
<option value="option2">Option 2</option>
</select>
</form>
<script type="text/javascript">
$('.target').change(function() {
alert('Something changed');
// Try validating form again (if valid, activate submit button)
});
</script>
Plan B
Another option is to always have the submit button clickable, but use .submit() to bind it to the form validator. Then if the form IS valid, carry on. If the form IS NOT valid use .preventDefault() to stop the submission of the form..... and you'd display a warning message too, indicating the missing fields.
The answer has been given in this question. It doesn't use jQuery, but it works for Autocomplete:
Use js onpropertychange event.
I have a decent solution after having the same problem. Set keyup as normal to our form fields, then mouseover to the surrounding div. So once you click the autocomplete option, you mouse will be over the top of the div:
$("#emailaddress").bind("keyup", function() {
displayFullSubcribeForm();
});
$(".center_left_box").bind("mouseover", function() {
displayFullSubcribeForm();
});
I wanted a very good user experience on a field where it would not be invalid (turn red in my case) as long as the user was reasonably active e.g. still filling out the field.
To do this for normal input, I was able to hook up to keyup with a debounce function, while blur is connected for immediate validation. While it appears that keyup is triggered by lastpass, since I have debounced it, there was a delay in validation. Thanks to #peter-ajtai I tried to add the change event and it indeed catches last pass and leaves the other niceties alone.
Coffeescript example:
#fieldExp
.keyup($.debounce(#_onExpChange, 3000))
.blur(#_onExpChange)
.change(#_onExpChange)
This worked well and lastpass form fill triggers immediate validation.
this is the ultimate solution, guaranteed to work
$(document).bind('mouseover', function(){
liveValidate();
});

Categories

Resources