.empty() jQuery method and undefined value - javascript

HTML:
<div class="form-group">
<div id="errorsum"></div>
</div>
JavaScript:
var errornbr = 0;
var mess;
$("#addclient input.form-control").each(function (index) {
if ($(this).val() == "") {
if ($(this).attr("data-val") != undefined) {
if ($(this).attr("data-val-required") != undefined) {
errornbr = errornbr + 1;
mess = mess + "<br/>" + $(this).attr("data-val-required");
}
}
}
});
$("#errorsum").empty();
if (errornbr > 0) {
$("#errorsum").append("<label class='col-xs-3 control-label text-danger' >" + mess + "</label>");
}
First line of the label text is always undefined. the mess didn't contains such value.
So I need to know:
Does the empty() method is the reason of this?
How can I fix this issue?

change var mess; to var mess = '';
Make the assume text value making it not return undefined

Please use ( typeof VALUE != 'undefined' ) instead of ( VALUE != undefined )
Please write condition like this
if( typeof $(this).attr("data-val") != 'undefined' ){
if( typeof $(this).attr("data-val-required") != 'undefined' ){
}
}

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..
}

Boolean in array with looping

i am simulate some graph and there is some problem on give some boolean condition for some array in looping, and this is the code
var check = false;
for (var k in GexfJS.graph.edgeList) {
var _edge = GexfJS.graph.edgeList[k]
if ( (_edge.source == _curre) && ( _edge.target != _nodeIndex) ) {
var _node = GexfJS.graph.nodeList[_edge.target];
_str += '<li><div class="smallpill" style="background: ' + _node.color.base +'"></div>' + _node.label + '' + ( GexfJS.params.showEdgeWeight && _edge.weight ? ' [' + _edge.weight + ']' : '') + '</li>';
}
}
My goal is, when the edge have been selected by some node it wont to select node it again because when the edge select the node it will have some action , in my graph there is a lot of edges from the nodes. so how code i insert to that algorithm above in use boolean condition for give condition to the node that have been selected from the edges?
Dont use boolean condition, just try object for it, like check={};
then put the condition
if ( (_edg.target == _curra) && (_edg.source != _nodeIndex)) {
var _nod = GexfJS.graph.nodeList[_edg.source];
if(check[_nod.label] != true){
if(_n != _nod){
_str += '';
}
}
check[_nod.label] = true;
}

Can't get JavaScript to check for null objects

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)

If-statement condition checking on != "undefined" fails

I'm trying to generate some HTML content for a google maps infowindow. I have 7 values which is supposed to be displayed if they do not equal null, undefined or "" (empty string).
But apparently my if(e.Property != null || e.Property != "undefined" || e.Property == "") doesn't work when a Property is undefined. Mostly the case is that e.Email is undefined. So instead of skipping that part, my code still inserts the html + "<br /> part. And when I alert() the e.Email it returns undefined which it's supposed to catch and skip if that was the case.
I have tried writting if(typeof e.Property != null || typeof e.Property != "undefined" || typeof e.Property == ""), but that made no difference.
// 'e ' is JSON object
var generateHTML = {
init: function(e) {
if (e != null || e != "undefined"){
generateHTML.check(e);
}
},
check: function (e) {
if(e.Title != null || e.Title != "undefined" || e.Title == ""){
html = html + "<b>"+e.Title+"</b>";
}
if(e.Address != null || e.Address != "undefined" || e.Address == ""){
html = html +"<br />"+ e.Address;
}
if(e.Zipcode != null || e.Zipcode != "undefined" || e.Zipcode == ""){
html = html +"<br />"+ e.Zipcode+", ";
}
if(e.City != null || e.City != "undefined" || e.City == ""){
html = html + e.City;
}
if(e.Phone != null || e.Phone != "undefined" || e.Phone == ""){
html = html +"<br />"+ e.Phone;
}
if(e.Email != null || e.Email != "undefined" || e.Email == ""){
html = html +"<br />"+ e.Email;
}
if(e.WebAddress != null || e.WebAddress != "undefined" || e.WebAddress == ""){
html = html +"<br />"+ e.WebAddress;
}
return html;
}
};
You want to check for !== undefined
e.g.
if(myvar !== undefined) {
//DO SOMETHING
}
If you want a more shorthand version you can just use:
if (e.Title) {
// add to HTML
}
if (e.Address) {
// add to HTML
}
You may want to consider building your HTML as an Array and then joining at the end to avoid creating many strings, e.g.
var html = [];
html.push("FirstName");
html.push("<br />");
html.push("LastName");
html.push("<br />");
html.push("Number");
var output = html.join(""); // "FirstName<br />LastName<br />Number"
if(e) //this would be shorter
if(e != undefined)
//
if(typeof(e) != 'undefined')
undefined is a variable name, not a string.
You don't need the quotes around it.
You are checking it as if its value is string "undefined"
remove the ""
better check something via e.length cause variables type are not acurate in JavaScript
I would also use the length function, if the array or object is empty the Logged length will be 0.0, i.e.
if(e.length == 0){
//then do something or nothing
}
else {
//Do somthing
}

jQuery check if two values is blank

I have three values:
var tbb = $("#total1 span").text();
var tmb = $("#total2 span").text();
var tab = $("#total3 span").text();
Each of them could be blank.
What is the better way in javascript/jquery check if two of these values is blank?
UPDATE
here is what i mean, my a bit ugly and a "lot of lines" solution
var i = 0;
if (tab != "") {
i++;
}
if (tmb != "") {
i++;
}
if (tbb != "") {
i++;
}
if (i >= 2) {
//do something
}
Any better thoughts?
if(tbb == null || tbb == ''){...}
And the same for the rest.
var twoAreEmpty = $("#total1, #total2, #total3").find("span:empty").length === 2;
var twoblank =
$("#tota11 span, #total2 span, #total3 span")
.filter(function(index){
return !$(this).text().replace(/^\s+|\s+$/g, '');
})
.length === 2;
Also, you probably mean #total1, not #tota11 - just sayin.
if (tab == "") should be enough shouldn't it?
Does .text() ever return blank?
var filtered = [tmb, tbb, tab].filter(function( str ) {
return !str.length;
});
if( filtered.length === 2 ) {
console.log('yay, two empty values: ', filtered);
}
Just try it.
var i = 0;
if(tbb.length == 0 )
{
i++
}
if(tmb.length == 0 )
{
i++
}
if(tab.length == 0 )
{
i++
}
if(i >=2)
{
//perform operation
}
It's been a while, but this is going to be a single-line solution for what you want.
if (((tab == '' ? 1 : 0) + (tmb == '' ? 1 : 0) + (tbb == '' ? 1 : 0)) > 1){
// Do Something
}
All valid answers, but forgetting about undefined :)
if(tbb === '' || tbb === null || tbb === undefined){ // do stuff };
I'd recommend making it a function which could return true/false :)

Categories

Resources