1

I have a button. As soon as i click the button i get an event object. with the event object i can say event.target.

With event.target i get following string:

<button class="mdi mdi-barley mdi-input-icon" id="vegan" date="7"></button>

i get the id with event.target.id

i get the class with event.target.class

if i say event.target.date i get null

i set the date attribute with the method element.setattribute();

How can i get thevalue of the prop date?

ddave
  • 19
  • 3
  • 2
    So you *set* the attribute with `setAttribute` but you're not sure what you *get* the attribute with? – Heretic Monkey Oct 02 '20 at 21:31
  • 1
    Does this answer your question? [javascript dom, how to handle "special properties" as versus attributes?](https://stackoverflow.com/questions/7006253/javascript-dom-how-to-handle-special-properties-as-versus-attributes) – Heretic Monkey Oct 02 '20 at 21:33

1 Answers1

5

It looks like date is a reserved property. You can access it here by using getAttribute instead:

console.dir(document.querySelector('button').getAttribute('date'));
<button class="mdi mdi-barley mdi-input-icon" id="vegan" date="7"></button>

But it would be more appropriate to use a data attribute instead of using a non-standard attribute:

console.dir(document.querySelector('button').dataset.date);
<button class="mdi mdi-barley mdi-input-icon" id="vegan" data-date="7"></button>
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320