ELement 1
ELement 2

Delete only all elements starting with "id_"

For example, I have two elements:

<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 only all elements starting with "id _",.?

thanks,.

+4
source share
2 answers

You can use an attribute with a selector:

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

Edit (see comments)

Your question says "all elements", so the selector in my example is not as specific as it could be. If you care about performance (in the real world, what makes this selector more specific will not make any noticeable difference), then you should make your selector as specific as possible. If you need only div elements, apply only the "start-with" to div elements:

 $("div[id^='id_']").remove(); 
+11
source

Use attribute starts with selector

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

JSFiddle example

+5
source

Source: https://habr.com/ru/post/1384290/


All Articles