简体   繁体   中英

Issue with Firefox: event.target

I use the script below to replace some diacritical marks from legacy to standard form. Works fine in all browsers, except Firefox; where it does not work at all. I assume the problem is from event.target, but I am just beginning with javascript and lack the know how for fixing this.

Found this (posible?) solution to my problem, but don't know how to adapt it for my script: event.target not working on Firefox

Any help would be greatly appreciated!

function inlocuire_diacritice (form) {
var form = event.target;
var i, l;
for (i = 0, l = form.elements.length; i < l; i += 1) {
if (form.elements[i].type === 'text') {
  form.elements[i].value = form.elements[i].value.replace(/Ş/g, 'Ș');
  form.elements[i].value = form.elements[i].value.replace(/ş/g, 'ș');
  form.elements[i].value = form.elements[i].value.replace(/Ţ/g, 'Ț');
  form.elements[i].value = form.elements[i].value.replace(/ţ/g, 'ț');
}
}
return true; 
}

EDIT: I call the function from inside another function:

function validate(form) {
//form validation script
inlocuire_diacritice (form);
}

And this is called on form submit:

<form action="whatever" name="whatever" method="post" onsubmit="return validate(this)">

Try this:

<form action="whatever" name="whatever" method="post" onsubmit="return validate(event, this)">...</form>

And the function :

function inlocuire_diacritice (ev, form) {
    // form is a reference to the form element already
    // if you need event.target, use ev.target
    // this refers to the element to which this event is attached
                :
}

Actually in FF (or in any modern browser) you don't need to pass arguments when invoking the function, in the function declaration you need to include one argument, which represents event . this in eventhandler refers to the element, to which the event is attached.

Please read about addEventListener to see, how the listeners should actually be attached to elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM