1

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)">
Community
  • 1
  • 1
Malasorte
  • 1,153
  • 7
  • 21
  • 45
  • How do you call this function? `event` is "alive" only within an eventhandler function (in IE only). If this is an evenhandler, `form` argument probably is the missing `event` in FF... – Teemu Apr 11 '14 at 11:17
  • ...and your function var is `form` but youre creating a new var named `form` inside it. – Batu.Khan Apr 11 '14 at 11:18
  • @BatuZet To be exact, `form` is re-assigned only. Arguments are declared before the body of the function is executed, hence the "double" `var` is omitted. – Teemu Apr 11 '14 at 11:23

1 Answers1

1

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.

Teemu
  • 22,918
  • 7
  • 53
  • 106
  • You mean function inlocuire_diacritice (event, form) { or function inlocuire_diacritice (ev, form) { ? – Malasorte Apr 11 '14 at 11:38
  • Literal `event` must be passed, the argument name in the function declaration doesn't matter, use what you wish (except keywords ofcourse). – Teemu Apr 11 '14 at 11:39