Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove just all elements beginning with "id_"

For example, I have 2 elements like this:

<div id="id_1">ELement 1</div>
<div id="id_2">ELement 2</div>
<div id="not_id">different id</div>

how can I remove just all elements beginning with "id_",.?

thanks,.

like image 561
Praditha Avatar asked Dec 03 '22 00:12

Praditha


1 Answers

You can use an attribute starts with selector:

$("[id^='id_']").remove();

Edit (see comments)

Your question says "all elements", which is why the selector in my example is not as specific as it potentially could be. If you care about performance (in the real world, making this selector more specific is not going to make a noticable difference), then you should make your selector as specific as possible. If it is only div elements you care about, apply the "starts-with" selector to div elements only:

$("div[id^='id_']").remove();
like image 170
James Allardice Avatar answered Jan 11 '23 08:01

James Allardice