Can't get JavaScript to check for null objects - javascript

I really don't know what the issue is here. As far as I can see, the code is simple and should work fine.
var Prices="";
for (var PriceCount = 1; PriceCount <= 120; PriceCount++) {
var CurrentPrice = "Price" + PriceCount;
if (prevDoc.getElementById(CurrentPrice).value != null) {
if (Prices == "") {
Prices = prevDoc.getElementById(CurrentPrice).value;
} else {
Prices += "," + prevDoc.getElementById(CurrentPrice).value;
}
} else {
break;
}
}
There could be up to 120 hidden inputs on the form. The moment we check for an input that doesn't exist the loop should break. My test page has two input elements that get pulled. On the third (the null) I get this error in firebug:
prevDoc.getElementById(CurrentPrice) is null
if (prevDoc.getElementById(CurrentPrice).value != null) {
Yes it is null...that's what the check is for ಠ_ಠ
Does any one know what I'm doing wrong? This seems like it should be really straight forward.
EDIT:
for clarity's sake, prevDoc=window.opener.document

if (prevDoc.getElementById(CurrentPrice).value != null) {
can be expanded to:
var element = prevDoc.getElementById(CurrentPrice);
var value = element.value; /* element is null, but you're accessing .value */
if (value != null) {

value is never null.
If it is not filled in, the value would be "" or a length of zero.
If the element does not exist, you would check for the existence of the element.
var CurrentPrice = "Price" + PriceCount;
var elem = prevDoc.getElementById(CurrentPrice);
if (elem && elem.value != null) {

I think it should be:
var Prices="";
for (var PriceCount = 1; PriceCount <= 120; PriceCount++) {
var CurrentPriceId = "Price" + PriceCount,
CurrentPrice = prevDoc.getElementById(CurrentPriceId);
if (CurrentPrice != null) {
Prices = (Prices == "") ? CurrentPrice.value : (Prices + "," + CurrentPrice.value);
}
else break;
}

Try
if (prevDoc.getElementById(CurrentPrice) !== null)

Related

Variable empty with if statement

$(".my-input").each(function () {
var theSelectBoxContainer = $(this).parent().next();
var theSelectBoxValue = theSelectBoxContainer.dxSelectBox('instance').option('value');
var txtvalue = $(this).val();
if (txtvalue != "" && txtvalue!=" ") {
if (i)
field += " and ";
field = field + "'" + $(this).val() + "'";
i++;
}
});
Above my jQuery code. With this code, I overwrite the values entered in the TextBoxes. But I do not want textboxes to be written when null characters are entered. But it is written. I check with if, but it is not. Where is the error?
You can use $.trim(txtvalue) which will validate empty, null and undefined.
Or you can also use:
if (txtvalue === undefined || txtvalue === null) {
// show error messages..
} else {
//execute this code..
}

Check and alert for the null value

I need to check for the null values and alert them if there are any before getting saved. I have used this code but I am not able to alert the null values instead it is getting saved . .
function fn_publish() {
var SessionNames = getParameterByName('SessionName');
var MenuType = getParameterByName('MenuType');
var Date = getParameterByName('ForDate');
var publish = "Y";
Dates = Date.split("-");
Date = Dates[1] + "/" + Dates[2] + "/" + Dates[0];
var rows = [];
cols = document.getElementById('product_table').rows[1].cells.length - 1;
table = document.getElementById('product_table');
for (var i = 1; i <= cols; i++) {
for (var j = 0, row; row = table.rows[j]; j++) {
if (j == 0) {
cust = row.cells[i].innerText;
alert(cust);
} else if (j == 1) {
catlg = row.cells[i].innerText;
alert(catlg);
} else {
if (typeof (row.cells[i]) != "undefined") {
if (row.cells[i].innerText != "") {
//alert(SessionNames+"::"+MenuType+"::"+Date+"::"+catlg+"::"+row.cells[0].innerText+"::"+row.cells[i].innerText+"::"+cust+"::"+publish);
fn_insert(SessionNames, MenuType, Date, catlg, row.cells[0].innerText, row.cells[i].innerText, cust, publish);
} else {
jAlert("Please select a product", "ok");
return false;
}
}
}
}
}
jAlert("Menu Published", "ok");
}
if (row.cells[i].innerText != "") {
May be the cells are containing empty space in that. Better trim ad then compare.
if (row.cells[i].innerText.trim() !== "") {
Also instead of innerText use textContent which is common in most modern browsers.
if (row.cells[i].textContent.trim() !== "") {

Better way to add an element to a serialized list without duplicates

the following code works, but is there a better way to do this, because I don't like the way and I'm not a pro in JS.
jQuery 1.11.0 is available.
value = "newvalue";
serializeList = "oldvalue1;oldvalue2;oldvalue3";
tmp = serializeList.split(';');
if ($.inArray(value, tmp) == -1) {
tmp.push(value);
}
serializeList = tmp.join(";");
value = 'newvalue';
if (serializeList.indexOf(value + ';') === -1 && serializeList.indexOf(';' + value) === -1) {
serializeList += ';' + value;
}

How to fire an event after a kendo comboBox has finished a filter

I need to fire an event after my combo box finishes its filter
I have extended the widget and have tried to chuck a .done on the end of the call.
search: function(word) {
word = typeof word === "string" ? word : this.text();
var that = this,
length = word.length,
options = that.options,
ignoreCase = options.ignoreCase,
filter = options.filter,
field = options.dataTextField;
clearTimeout(that._typing);
if (length >= options.minLength) {
that._state = STATE_FILTER;
if (filter === "none") {
that._filter(word);
} else {
that._open = true;
that._filterSource({
value: ignoreCase ? word.toLowerCase() : word,
field: field,
operator: filter,
ignoreCase: ignoreCase
}); // .done here does not work :(
}
}
},
I need to know when a value is returned so that I can do some other stuff on my page once i know that the input matches a value server side.
Can anybody think of a way to achieve this? :)
Is there a way to fire an event once the datasource has changed?
I solved the issue by using the _busy state on the widget.
The following code is in the select function on the widget so whenever someone losses focus this fires.
If the widget is not busy it will have finished the ajax call.
Hope this is usefull to someone :)
if (data && data.length != 0)
{
for (var i = 0; i < data.length; i++)
{
li = $(that.ul).find('li:nth-child(' + (i + 1) + ')');
if (li.length != 0 && data[i].ProductCode == that.input.val())
{
that._select(li);
return;
}
}
}
if (that._busy && that._busy != null)
{
that.busyCheck = setInterval(function () {
if (!that._busy || that._busy == null) {
clearInterval(that.busyCheck);
if (data && data.length != 0){
for (var i = 0; i < data.length; i++){
li = $(that.ul).find('li:nth-child(' + (i + 1) + ')');
if (li.length != 0 && data[i].ProductCode == that.input.val()){
that._select(li);
return;
}
}
}
}
}, 50);
}
that._AddcodeIsServerValid(false);

Input removing leading 0

I have a form input field.
<input style="text-align:right;" type="text" name="DiversionCalc_Diversion_Rate" id="calc_dr" value="0.25%" />
I am attempting to format it based on focusout using jQuery 1.7.2
$('#calc_dr').focusout(function () {
var value = $.trim($(this).val()).toString();
if (value.indexOf("0.") === -1) {
var $t = ("0" + value).toString();
alert($t);
$(this).val($t);
}
if (value != '' && value.indexOf("%") === -1) {
$(this).val(value + '%');
}
});
While this mostly is working, the alert pops up the correct 0.25 when I enter .25 in the field, however, the $(this).val only ever shows .25
How can I get it to show what it's showing me in the alert???
Make $t a global variable (pull it out of the if loop) and assign it instead of value.
$('#calc_dr').focusout(function () {
var value = $.trim($(this).val()).toString();
var $t = value;
if (value.indexOf("0.") === -1) {
$t = ("0" + value).toString();
alert($t);
$(this).val($t);
}
if ($t != '' && $t.indexOf("%") === -1) {
$(this).val($t + '%');
}
});
The basic idea is to grab the value, manipulate the value, then update the UI. The key being there is only one update at the end.
// Get the new value (strip everything but numbers and period)
var v= parseFloat($(this).val().toString().replace(/[^0-9\.]+/g, ""));
// Basic data type validation
if (isNaN(v)) v= 0;
if (v< 0) v= 0;
if (v> 100) v= 100;
// other validation updates v as needed...
doCheckDiversionRate(v);
// update UI (btw toFixed() will add a leading zero)
$(this).val(v.toFixed(2) + '%');

Categories

Resources