Event keyword is deprecated

PyCharm is flagging event as deprecated.

<select
onchange="document.querySelector('#user-email').value =
this[event.target.selectedIndex].getAttribute('data-email');
>
<option
value="{{ user.id }}"
data-email="{{ user.email }}"
data-mobile="{{ user.user_mobileno }}"
>{{ user.first_name }}</option>
</select>

What do I replace event.target.selectedIndex with ?

Could you just reference the selected option directly?

<select onchange="document.querySelector('#user-email').value = this.options[this.selectedIndex].getAttribute('data-email');">
  <option value="{{ user.id }}" data-email="{{ user.email }}" data-mobile="{{ user.user_mobileno }}">{{ user.first_name }}</option>
</select>

1 Like

Yes, this works, thanks, but I don’t see how come this is not widely documented because search always gave references to the event object.

PyCharm is trying to say that the event property of the window is deprecated. Which… technically it is, but it’s been Legacy’d into standarization for the window object. This may cause problems when the object is not in the window (Shadow elements or non-browser contexts), so its not recommended for use.
What you’re referring to with event is actually window.event; if you need to catch the event object, it’s better to invoke a function, to which an Event object will be passed. Inline javascript is generally not recommended either, but thats beyond the scope of the question.

2 Likes

Inline javascript is generally not recommended either, but thats beyond the scope of the question.

I’m using inline JavaScript for AlpineJS only.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.