timpone
timpone

Reputation: 19969

how to delete a hash with subhashes by name in jQuery

In my html I have:

<input name="creative[placement_creative_attributes][site_placment_id]" />
<input name="creative[placement_creative_attributes][site_id]" />
and ....

I'd like to delete this whole series of elements via something like this but this doesn't work:

  $('[name*=creative[placement_creative_attributes]]').remove();

What is the proper way to delete this entire hash via jQuery?

Upvotes: 0

Views: 22

Answers (1)

esqew
esqew

Reputation: 44714

Looks like you've mixed up a bit of your quotes in your example, should likely have quotes around the expected wildcard match for the name attribute:

$('[name*="creative[placement_creative_attributes]"]').remove();

In this particular case however, I'd reccommend using the carat (^) to select every element that has a name attribute that starts with the string creative[placement_creative_attributes], and then remove() each of them from the DOM:

$("[name^='creative[placement_creative_attributes]']").remove();

jQuery Documentation

$("button").click(function () {
    $("[name^='creative[placement_creative_attributes]']").remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="creative[placement_creative_attributes][site_placment_id]" placeholder="creative[placement_creative_attributes][site_placment_id]" /><br />

<input name="creative[placement_creative_attributes][site_id]" placeholder="creative[placement_creative_attributes][site_id]" /><br />

<input name="creative[site_id]" placeholder="creative[site_id]" /><br />

<button>Remove placement_creative_attributes</button>

Upvotes: 1

Related Questions