Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use jQuery to check if an element was created dynamically?

Tags:

jquery

I'm using jQuery to append new <option> tags to a <select> field, and in some cases I need to remove those options without knowing the values of them.
Is it possible to remove all the options that were created by jQuery whilst leaving the original options intact?

The only way I can think of doing it is by checking they're not the values I want to keep. Hopefully is there an easier, quicker, way?

like image 269
Richard Hedges Avatar asked Nov 19 '25 06:11

Richard Hedges


2 Answers

Once elements are inserted into the DOM they are all equal; there is no magic "dynamically created" flag that you can check.

Of course you can always create your own methods to do so. For example, you can add a "data-dynamic" HTML attribute or a dynamic CSS class to the dynamically created options and then filter based on that.

like image 106
Jon Avatar answered Nov 21 '25 09:11

Jon


You can add a property to the tags and filter on that. For example you can make them part of a class appended and delete them that way. See for an example this fiddle.

like image 30
Calavoow Avatar answered Nov 21 '25 08:11

Calavoow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!