Jennifer Anthony
Jennifer Anthony

Reputation: 2277

Find tag <a> and put a word in an element with jQuery

What I want:

if inside tag <li> there is no tag <a>, then - insert word "Empty" into <div class="tooltip"> </div>.

How can do i do it with jQuery?

<ol>
    <li><input name="hotel_id[]" value="14" style="display:none;">
    <div class="tooltip" style="top: 439px; left: 717px; display: none; ">
    </div>
    <a href="" class="tool_tip" title="ok">Hello</a></li>

    <li><input name="hotel_id[]" value="5" style="display:none;">
    <div class="tooltip" style="opacity: 0.9; top: 439px; left: 717px; display: none; ">
    </div>
    <a href="" class="tool_tip" title="">How</a></li>

    <li><input name="hotel_id[]" value="4" style="display:none;">
    <div class="tooltip" style="top: 439px; left: 717px; display: none; opacity: 0.9; ">
    </div>
    <a href="" class="tool_tip" title="">Are</a></li>

    <li><input name="hotel_id[]" value="3" style="display:none;">
    <div class="tooltip" style="opacity: 0.9; top: 439px; left: 717px; display: none; ">
    </div>
    <a href="" class="tool_tip" title="">Help</a></li>

    <li><input name="hotel_id[]" value="2" style="display:none;">
    <div class="tooltip" style="top: 439px; left: 717px; display: none; ">
         // I want append here word "Empty" that there is not tag a in li
    </div></li>

    <li><input name="hotel_id[]" value="1" style="display:none;">
    <div class="tooltip" style="top: 439px; left: 717px; display: none; ">
         // I want appen here word "Empty" that there is not tag a in li
    </div></li>
</ol>

Upvotes: 1

Views: 85

Answers (2)

voigtan
voigtan

Reputation: 9031

look after :not and :has:

$("li:not(:has(a))").find(".tooltip").html("Empty").removeAttr("style");

I remove the style attribute on .tooltip, if you dont need that just change it to show() instead to show the .tooltip:

$("li:not(:has(a))").find(".tooltip").html("Empty").show();

Demo: http://jsbin.com/uyuwoy/edit

Upvotes: 1

Kokos
Kokos

Reputation: 9121

This would work:

$('ol li').not(':has(a)').find('.tooltip').html('Empty');

Upvotes: 0

Related Questions