I have the following snippet in my js file:
if ($("#LengthenCheckBox").is(":checked")) {
$("#Length").data(kendoTextBox).enable(true);
//If Lengthening field is blank, default to zero
if ($("#Length").data(kendoTextBox) == "") {
$("#Length").data(kendoTextBox).value("0");
};
document.getElementById("Lengthen").value = true;
else {
if ($("#Length").data(kendoTextBox) == "") {
$("#Length").data(kendoTextBox).value("0");
}
else {
$("#Length").data(kendoTextBox).value();
}
$("#Length").data(kendoTextBox).enable(false);
However, I just want the value of the textbox to equal 0 if no value is entered or the textbox is blank. How can I adjust the following above?
EDIT:
How can I set the value of textbox to 0 if blank or empty?
That is I have a simple textbox, if the user enters no value inside the textbox or the user leaves the textbox blank and goes somewhere else, I want it to automatically default back to 0 and not blank! I never want the textbox to be blank but contain some value whether it's 0 or any other integer value but never blank.
Hope this helps!
I have the following snippet in my js file:
if ($("#LengthenCheckBox").is(":checked")) {
$("#Length").data(kendoTextBox).enable(true);
//If Lengthening field is blank, default to zero
if ($("#Length").data(kendoTextBox) == "") {
$("#Length").data(kendoTextBox).value("0");
};
document.getElementById("Lengthen").value = true;
else {
if ($("#Length").data(kendoTextBox) == "") {
$("#Length").data(kendoTextBox).value("0");
}
else {
$("#Length").data(kendoTextBox).value();
}
$("#Length").data(kendoTextBox).enable(false);
However, I just want the value of the textbox to equal 0 if no value is entered or the textbox is blank. How can I adjust the following above?
EDIT:
How can I set the value of textbox to 0 if blank or empty?
That is I have a simple textbox, if the user enters no value inside the textbox or the user leaves the textbox blank and goes somewhere else, I want it to automatically default back to 0 and not blank! I never want the textbox to be blank but contain some value whether it's 0 or any other integer value but never blank.
Hope this helps!
Share Improve this question edited Dec 4, 2014 at 17:31 Euridice01 asked Dec 4, 2014 at 17:16 Euridice01Euridice01 2,55812 gold badges48 silver badges80 bronze badges 12-
Re-running a jQuery selector (even a fast ID one) is a bad idea. Use a local var to store it e.g.
var $length = $("#Length");
– iCollect.it Ltd Commented Dec 4, 2014 at 17:20 -
2
@God is good: Don't lengthen a jQuery example with raw JS :) Did you pull
textbox
out of thin air as that is not in the original? – iCollect.it Ltd Commented Dec 4, 2014 at 17:21 - Yes I did, @TrueBlueAussie. – Ian Hazzard Commented Dec 4, 2014 at 17:24
-
First off, why are you checking the data attribute kendoTextBox's value when you say you want to check the field's value? Second, in jQuery it's
.val("0")
, not.value("0")
. – j08691 Commented Dec 4, 2014 at 17:25 - 1 would be nice to see the corresponding html. – low_rents Commented Dec 4, 2014 at 17:26
3 Answers
Reset to default 4update 3: if you consider spaces inside the textbox as "blank" as well:
$("#Length").on("blur", function () {
if ($(this).val().trim().length == 0) {
$(this).val("0");
}
});
//trigger blur once for the initial setup:
$("#Length").trigger("blur");
JSFiddle: http://jsfiddle/northkildonan/n1dd2opx/3/
update 2: if you want the textbox to be 0
when it's left blank at all times:
$("#Length").on("blur", function () {
if ($(this).val().length == 0) {
$(this).val("0");
}
});
//trigger blur once for the initial setup:
$("#Length").trigger("blur");
JSFiddle: http://jsfiddle/northkildonan/n1dd2opx/1/
update: if you want to check for the length of a textbox with the id "Length", then it's:
if ($("#Length").val().length == 0) {
$("#Length").val("0");
};
JSFiddle: http://jsfiddle/northkildonan/n1dd2opx/
Just five small lines of code:
$("#buttonid").on('click', function() {
if ($("#textboxid").val() == "") {
$("#textboxid").val("0");
}
});
$(document).ready(function(){ $("#amount").on("blur", function () {
if ($(this).val().trim().length == 0) {
$(this).val("0");
}});});