0

lets say i have fallowing html...

<li ><a href="#" id="UsersGuides" ">Users Guides</a></li>
<li ><a href="#" id="Application" ">Application</a></li>

i am adding and removing class by fallowing jquery function...

jQuery('selected').removeClass('selected');
if(!jQuery("#"+selectedAsset).parents('li').hasClass('selected')){
    jQuery("#"+selectedAsset).parents('li').addClass('selected');
}

but at the same time i want to add an image at the end of anchor tag, and it should get removed at the same time when i am removing the selected class.i know i can use append method but how to remove it when class get removed, i couldn't not find out. out put should look like in this way...when i clicked on this link

<li class="selected"><a href="#" id="UsersGuides" ">Users Guides</a><img width="10" height="18" src="/search/images/refineSearch-selectedRight.gif" style="position: relative; left: 9px;"></li>
<li ><a href="#" id="Application" ">Application</a></li>

and when i clicked on second link it should like...

 <li ><a href="#" id="UsersGuides" ">Users Guides</a></li>
<li class="selected"><a href="#" id="Application" ">Application</a><img width="10" height="18" src="/search/images/refineSearch-selectedRight.gif" style="position: relative; left: 9px;"></li>

how to do this in jquery? Thanks in advance!!!

2 Answers 2

2
jQuery('selected').removeClass('selected').find("img").remove();
Sign up to request clarification or add additional context in comments.

Comments

1

.append() will insert inside your selected tag. If you could guarantee that the last element was your anchor then it would work. if there was anything else after the anchor then it would fail. If you selected the anchor, then you would expect to use the .after() method.

To remove an element, use the .remove() method upon the selected image.

1 Comment

No worries, although I think we're being told that an upvote is at least as good as a thanks...blog.stackoverflow.com/2011/01/how-to-say-thanks-in-an-answer

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.