Search code examples
jqueryjquery-selectorsparent

Parent() isn't Working... Don't Know How to Select That Element!


There are a lot of div.post. I need to select one of them... that one that have $(.comment_this').attr('rel'). And then add new tag in <!-- Here! --> place. Any idea?

FireBug says that parent() isn't a function... jQuery 1.6.1.

<div class="post">
    <div class="post_head"><div>&nbsp;</div></div>
    <div class="post_body">
        <!-- ... -->
        <div class="options">
                <a class="gray_button comment_this" href="#" rel="123">Comments</a>
                <span class="gray_txt">0 comments</span>
        </div>
        <!-- Here! -->
    </div>
    <div class="post_bottom"><div>&nbsp;</div></div>
</div>

This is what I have...

$('.post .comment_this').attr('rel').parent().html('foo');

Solution

  • To select an element that has an attribute, use the has-attribute selector [name]. attr returns the value of the attribute, rather than filtering by it. Obviously strings (such as "123") don't have a parent method!

    You probably also need after, rather than html, so that you don't overwrite wanted content.

    $('.comment_this[rel]').parent().after('your html');