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,665
  • 6
  • 39
  • 47
Mr.Smithyyy
  • 2,013
  • 9
  • 43
  • 85

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,423
  • 3
  • 27
  • 47
0
setTimeout(function() {
 if ($.trim($(".data-block li:last-child h2").html()) == '') {
    $(".data-block li:last-child h2").text('Not Applicable');
  };
}, 1500);
ahmad valipour
  • 283
  • 2
  • 11