-7

i try to detect null this way

if(!$(this))
{
    alert('here is null');
}

OR

if($(this)===null)
{
    alert('here is null');
}

but still no luck.

here is partial code

$elements.each(function(){
//alert($(this).html());
    var $item = $('<li />').append($(this));
    if(!$(this))
    {
        alert('here is null');
    }
    //alert($item.text());
    $list.append($item);
});

anyone can see full code from here https://jsfiddle.net/tridip/41s1pq3a/12/

edit

i was iterate in td's content. td has some link and text. i was trying to wrap each text and link inside li. so iterate this below way. code is working but some time it is also showing null which i need to detect.

i am looking for way not consider any null or empty.

here is the code

var $elements = $('.webgrid-footer td').contents()
  .filter(function() {
    return this.nodeType === 3 || this.nodeType === 1; // 1 means elements, 3 means text node
  });

var $list = $('<ul />');

$elements.each(function(){
//alert($(this).html());
    var $item = $('<li />').append($(this));
    if(this===null)
    {
        alert('here is null');
    }
    //alert($item.text());
    $list.append($item);
});
//alert($list.html());
$('#dv').append($list);

see this line var $item = $('<li />').append($(this)); it is getting some time empty or null which i do not want tp consider. if anyone knows it how to handle this situation then share the idea. thanks

Mou
  • 15,673
  • 43
  • 156
  • 275

2 Answers2

3

$(this) will never be either null or falsey, because jQuery always returns an object reference, which is not null or falsey.

In strict mode, it's possible for this (not $(this)) to be null. In loose mode, it isn't; attempts to make this be null will cause this to be a reference to the global object.

So it may be that you want to test this, not $(this). But only in strict mode. In loose mode, bizarrely, you'd want if (this == window) to be your "null" test.

Having said that, $elements is clearly meant to be a jQuery object, and I'm not immediately thinking of a way to to create a jQuery objct with nulls in through the public API. (It's easy if you muck about with the internals...)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
3

$(null) is an empty jQuery object, not null. And all objects are truthy.

If you want to test null, use this === null. You don't need jQuery for this.

However, I don't see why do you expect this to be null sometimes. Instead, it seems you want to ignore whitespace text nodes.

var $elements = $('.webgrid-footer td').contents().filter(function() {
  return (this.nodeType === 3 && $.trim(this.nodeValue) !== '')
         || this.nodeType === 1;
});
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • i tried this code `if(this===null) { alert('here is null'); }` but did not work. it will be helpful if u see my jsfiddle code and rectify there. – Mou Dec 27 '15 at 15:05
  • 2
    @Mou: No, that's not the way it works. The **full content** of your question must be *in* your question, not just linked. – T.J. Crowder Dec 27 '15 at 15:06
  • @Mou use $(this)===null and if its null then only it will be goes under if statement What answer oriol gave it's correct. – Pankaj Gupta Dec 27 '15 at 15:07
  • @Mou Question must be complete by themselves, without needing to read an external link. And I don't see why do you expect `this` to be null sometimes. – Oriol Dec 27 '15 at 15:08
  • see the js fiddle https://jsfiddle.net/tridip/41s1pq3a/24/ i applied your modification in code and now nothing is getting append in div called dv. please see and tell me what is wrong there? – Mou Dec 27 '15 at 16:00
  • @Mou `&.trim` was a typo, I meant `$.trim`. – Oriol Dec 27 '15 at 16:07