Nick Faraday
Nick Faraday

Reputation: 568

jQuery remove the prev occurance of a class?

I have a class that I want to dynamically remove based on a link. How do I select the parent "added" class with jQuery. There can be multibpule instances of the class I only want to remove the closest parent.

Example:

<div class="added">
    <div class="cl">&nbsp;</div>
        <label>Add Item<br />
            <span>What to Add</span>
        </label>
        <div class="rightbox">
            <% fields_for "items[related_items][]", add do |item_form|%>
                <%= item_form.text_field :add %>
            <%= link_to_function "remove", **"('div.added').empty()"** %>
            <% end %>       
        </div>
    <div class="cl">&nbsp;</div>
</div>

<div class="added">
    <div class="cl">&nbsp;</div>
        <label>Add Item<br />
            <span>What to Add</span>
        </label>
        <div class="rightbox">
            <% fields_for "items[related_items][]", add do |item_form|%>
                <%= item_form.text_field :add %>
            <%= link_to_function "remove", **"('div.added').empty()"** %>
            <% end %>       
        </div>
    <div class="cl">&nbsp;</div>
</div>

<div class="added">
    <div class="cl">&nbsp;</div>
        <label>Add Item<br />
            <span>What to Add</span>
        </label>
        <div class="rightbox">
            <% fields_for "items[related_items][]", add do |item_form|%>
                <%= item_form.text_field :add %>
            <%= link_to_function "remove", **"('div.added').empty()"** %>
            <% end %>       
        </div>
    <div class="cl">&nbsp;</div>
</div>

Upvotes: 0

Views: 157

Answers (2)

Nick Faraday
Nick Faraday

Reputation: 568

Never mind just figured it out.... see below for anyone viewing this question.

$(this).closest('.added').empty()

Upvotes: 0

cletus
cletus

Reputation: 625237

If you want closest parent and the link is inside the relevant div then:

$("a.classname").click(function() {
  $(this).closest("div.added").removeClass("added");
  return false;
});

I'm not certain that's relevant to what you're doing as I'm not familiar with ASP.NET but the above is the vanilla jQuery way of doing it.

Upvotes: 1

Related Questions