Shimmy Weitzhandler
Shimmy Weitzhandler

Reputation: 104741

jQuery .remove(Selector) doesn't work

Look at this snippet:

var div = $('#createDrugForm');

div.remove('input[type=hidden]');
//the hidden field is still there

div.find('input[type=hidden]').remove();
//the hidden was removed

Why does the first method of removal not work?

Upvotes: 1

Views: 190

Answers (1)

dfsq
dfsq

Reputation: 193261

When you provide a selector to $.fn.remove method, this selector is used to filter already selected collection (see $.fn.filter), but not to find new child elements (see $.fn.find).

For example, if you have this HTML structure:

<div class="div a">a</div>
<div class="div b">b</div>
<div class="div c">c</div>

you can remove .a div with this code

$('.div').remove('.a');

In your case you need to use find method and then remove found inputs.

Upvotes: 3

Related Questions