I have some little code that I can't get to work. Here it is:
<input type='password' name='Confirmpwd' id='Confirmpwd'
onkeyup="if(this.value != '') myFunction('checkConfirmpwd', (Password.value == this.value) ? 1 : 0;, this.id);" />
I'm 100% sure that Password.value and this.value are correct when I place them in there. So it's not a problem with the variables.
The problem is that, myFunction isn;t executed anymore when I try to compare Password.value and this.value in the argument like above. myFunction is declared like this:
myFunction(val1, val2, val3) { ...some code... }
What my goal is that I can compare the 2 strings and send them to myFunction when I call the function.
2 Answers 2
The ; indicates an end of statement. If there are missing parameter or brackets that have not been closed when a semi-colon is encountered, it may result in an error.
This is the case in this instance.
Change the code to something like
if(this.value != '') myFunction('checkConfirmpwd', (Password.value == this.value) ? 1 : 0, this.id);
2 Comments
Put the onkeyup in an external javascript file rather than the same HTML file. It's better to do this for ease-of-editing and performance:
HTML
<input type="password" name="Confirmpwd" id="Confirmpwd" />
JS
document.getElementById("Confirmpwd").onkeyup = function () {
"use strict";
if (this.value !== "") { // !== is better than !=
myFunction("checkConfirmpwd", (Password.value === this.value) ? 1 : 0, this.id); // === is better than ==
}
};
And all we did there was change ;, to ,. It was just a typo
6 Comments
onkeyup part will hide the script, right? -> More security?(function { // code }())) to avoid other scripts overwriting functions which you've used in an on<something> attribute.onkeyup the input and the debugger will show you where the code is. It's so hackers can't overwrite your functions as easily.onkeyup property to intercept the call and do whatever they want (including tracing into the original code to see what it does). There's no real increase in security here.
myFunction. That may just be a typo in your post here, but if it's in the original code, it would cause the call to fail with a syntax error.