1

You'd think this was easy - but I'm having the hardest time with it.

Here's what I'm trying to identify:

<span class="cw-value-one"></span>

Here's what I'm using so far:

    $('span.cw-value-one').each(function(){
        var textNode = $(this).text();
        var type = typeof textNode;
        var len = textNode.length;
        if($(this).is(':empty')){
            $(this).siblings('span.cw-value-two').css({"position": "relative", "left": "1em"});
        }
    });

Ok, so textNode = "", type = string and len = 1 - none of which is helpful in identifying an empty text node, since a has a type of string and length of 1. The jQuery .is(':empty') is not working either.

So whow do you identify an empty text node in JQuery or plain ol' Javascript?

b. e. hollenbeck
  • 6,493
  • 7
  • 32
  • 47

2 Answers2

2

You might be having white space in span use trim() to remove spaces within span around the text, if any.

Live Demo

  var textNode = $(this).text().trim();
Adil
  • 146,340
  • 25
  • 209
  • 204
0

try checking by the length of the text.

$('span.cw-value-one').each(function(){
        var textNode = $(this).text();
        var type = typeof textNode;
        var len = textNode.length;

        if(len <= 0){
            $(this).siblings('span.cw-value-two').css({"position": "relative", "left": "1em"});
        }
    });​

I would also recommend trimming the text. If you want to trim the text please also be sure to see this about ie not liking trim. .trim() in JavaScript not working in IE

Community
  • 1
  • 1
Dennis Martinez
  • 6,344
  • 11
  • 50
  • 67