0

I have a script that is supposed in an element with a default phrase if it is empty for a period of time. It works if i make a seperate if statement for every element but doesn't work when I try to combine it in one statement with $(this).

My Fiddle

HTML

<div class="data-block">
    <ul>
        <li><h2>One</h2></li>
        <li><h2></h2></li>
    </ul>
</div>
<div class="data-block">
    <ul>
        <li><h2>Two</h2></li>
        <li><h2></h2></li>
    </ul>
</div>
<div class="data-block">
    <ul>
        <li><h2>Three</h2></li>
        <li><h2></h2></li>
    </ul>
</div>

Javascript

setTimeout(function() {
    if ($.trim($(".datablock li:last-child h2").html()) == '') {
        $(this).text('Not Applicable');
    };
}, 1500);
empiric
  • 7,825
  • 7
  • 37
  • 48
Mr.Smithyyy
  • 2,157
  • 12
  • 49
  • 95

2 Answers2

2

In setTimeout context this always refers to browser object and you had typo .datablock

setTimeout(function() {
    $ele = $(".data-block li:last-child h2");
    if ($.trim($ele.html()) == '') {
        $ele.text('Not Applicable');
    };
}, 1500);
vinayakj
  • 5,591
  • 3
  • 28
  • 48
0
setTimeout(function() {
 if ($.trim($(".data-block li:last-child h2").html()) == '') {
    $(".data-block li:last-child h2").text('Not Applicable');
  };
}, 1500);
ahmad valipour
  • 293
  • 2
  • 11