1

I'm trying to get an element which is added by an external application. The only way I can get this specific element is by the data-i18n-key attribute which I thought I can grab like any data attribute so something like this.

The code:

const buttons = document.querySelectorAll('[data-i18n-key="sdk.seamless_product_reward.paid_items_required"]');
console.log(buttons.length);
<span class="lion-reward-item__redeem-button-text lion-loyalty-page-reward-item__redeem-button-text" data-i18n-key="sdk.seamless_product_reward.paid_items_required">Paid items required</span>

However, this doesn't return anything. Any ideas how to do this?

Barmar
  • 741,623
  • 53
  • 500
  • 612
MariaL
  • 1,112
  • 2
  • 16
  • 41
  • 1.) what does "it doesn't return anything" mean 2.) are you sure the element exists in the DOM when querying for it? – Marco Feb 13 '23 at 16:30
  • Your code works in the snippet. The problem is probably answered in https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element – Barmar Feb 13 '23 at 16:31

1 Answers1

1

Of course, Barmer is absolutely right. Your code works. The problem will be that your JS is initialised before the DOM has finished loading. Pack your JS above the closing body tag.

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79