0

I want to execute a script when some data attribute are set. Attributes can be set AFTER the all page is loaded.

In that case the script doesn't execute.

<div data-id='' data-position=''></div>

But in that one, i want to execute it

<div data-id='3' data-position='67'></div>

Is it even possible ?

Adrien Castagliola
  • 911
  • 2
  • 11
  • 30
  • 2
    If your script is assigning the value/attribute, execute required things after assignment.. – Rayon Nov 09 '16 at 09:51
  • 1
    Possible duplicate of [Detecting attribute change of value of an attribute I made](http://stackoverflow.com/questions/16781778/detecting-attribute-change-of-value-of-an-attribute-i-made) – viral Nov 09 '16 at 09:56

2 Answers2

1

You can use MutationObserver to listen attribute change and act upon.

Thaadikkaaran
  • 5,088
  • 7
  • 37
  • 59
0

Setting the attributes of html means that it is done programmatically via jQuery as in:

$("div").attr("data-id", "someId");

So if it is your script performing this action then simply do whatever you want right after.

If you want to check if the attribute has been set you can do:

if ( $("div").attr("data-id") != "" ) { do the stuff you want }

If you want to listen to changes then go for @Jaganathan's suggestion

DropkickSteve
  • 123
  • 1
  • 7