Vivek
Vivek

Reputation: 11028

how to add and remove an image based on class attribute in jquery

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!!!

Upvotes: 0

Views: 994

Answers (2)

Mutation Person
Mutation Person

Reputation: 30520

.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.

Upvotes: 1

amosrivera
amosrivera

Reputation: 26544

jQuery('selected').removeClass('selected').find("img").remove();

Upvotes: 2

Related Questions