I have a script that clears the contents of an input field when clicked. Currently everything works fine but when validating my script I'm getting a message of "event" is depreciated. Is there an alternative way to accomplish what I'm doing in JavaScript?
HTML:
<button class="ClearElementsBtn" onClick="ClearElementName('InputID');"></button>
JavaScript:
function ClearElementName(ElementName) {
var clickedelement = event.target
var elementsbyname = document.getElementsByName(ElementName)
var TemplateCount = elementsbyname.length;
if (TemplateCount > 0) {
for (let Group = 0; Group < TemplateCount; Group++) {
if (clickedelement.parentElement.parentElement.contains(elementsbyname[Group])) {
elementsbyname[Group].value = "";
}
}
}
}
My code is scripted for the template page it is used on this way because when filling out the template if the user needs to add a second Template to add another set of data, the second Template has the same name and IDs so this script is like this to allow addition clear buttons of the same names to work otherwise a simpler code I used only cleared the first instance of a name and not the others.
var clickedelement = event.target
This is accessing window.event
, which has been deprecated for quite a while already, https://developer.mozilla.org/en-US/docs/Web/API/Window/event
You should be able to work around this, by changing your function parameters to
function ClearElementName(ElementName, clickedelement)
removing the
var clickedelement = event.target
line, and then changing the places where you invoke this, to
onClick="ClearElementName('InputID', this);"