1

this is my current html:

<div>
    <a href="http://example.com">example.com</a>
</div>

I need to add a new div as a last element of this structure. This is what I want to achieve:

<div>
    <a href="http://example.com">example.com</a>
</div>

<div>
    added element
</div>

I need to target it by href attribute. This is what I've tried but it's not working:

jQuery('a[href$="http://example.com"]').parent().append('<div>added element</div>');

1 Answer 1

2

You were close, but that will append the div within the div that has the anchor in it.

You want to add it next to that div using after rather than append:

jQuery('a[href$="http://example.com"]').parent().after('<div>added element</div>');
// ----------------------------------------------^

Live Example:

jQuery('a[href$="http://example.com"]').parent().after('<div>added element</div>');
<div>
    <a href="http://example.com">example.com</a>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.