Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Javascript 

Javascript DOM [object MouseEvent]undefined

i got this problem when i click the button
[object MouseEvent]undefined

PHP Code

<?php 
var op = document.getElementById("op");
var 
one = document.getElementById("one");
var 
equa = document.getElementById("equ");
op.onclick =function verif (){
var 
opvalue = document.getElementById("op").value;
console.log(opvalue);
}
one.onclick = function verif (){
var 
onevalue = document.getElementById("one").value;
console.log(onevalue);
}
equ.onclick =  function ret (onevalue,opvalue){
 
console.log(onevalue+opvalue);
} 
Writeln('Give me a Target >');
[Image: giphy.gif]
(Nov 27, 2019 09:14 PM)SIM-1517830 Wrote:  
Code
equ.onclick =  function ret (onevalue,opvalue){
console.log(onevalue+opvalue);
}

I'd expect the output of this to be  "[object MouseEvent]undefined". Here's why:
- The onclick function expects only one argument, the MouseEvent object.
- Since the onclick function only expects one argument, any extra arguments you pass in will be undefined.
- Therefore your first argument (oneValue) will be the MouseEvent object, and your extra argument (opvalue) will be undefined. When you add these two parameters together, it coerces it to a string. So you're console logging the concatenation of two strings "[object MouseEvent]" + "undefined"

To correct you, onclick requires a function name or function expression to be passed and it doesn't matter how many arguments you put in said function. You can even create a function that takes in no arguments.. However, onclick does pass on the MouseEvent to said function. You're right about the output though.
https://developer.mozilla.org/en-US/docs...rs/onclick

Also, OP the following variable is undefined as well in your code snippet, unless you're not posting everything:
Code
equ

What's more, OP, it's 2019 and you should declare your variables with scoping in mind (e.g. let and const).
[Image: vp6iypF.gif]
(Nov 28, 2019 07:19 PM)SIM-1517830 Wrote:  Can you give an example of what you're referring to? The link you provided there outlines my understanding of it:



Unless you default additional arguments, I don't see how you'd get use out of them. And of course you could reference a function with no arguments, you just wouldn't be able to utilize the Event.

OP posted an example for you above that confirms this and obviously you wouldn't be able to use any extra parameters in said case, hence they are undefined as you stated out. But none-the-less it is not impossible to do so. As referenced by the JavaScript specs.

Regardless, you can very well share a function with other methods other than the onclick handler of an element, hence the usage of passing a method name and by sharing some functionality inside said method, which is Object-Oriented programming related.
[Image: vp6iypF.gif]


Possibly Related Threads...
Thread / Author Replies Views Last Post
by OmniscientMar 29, 2026 03:16 PM 0 95 Mar 29, 2026 03:16 PM
Last Post: Omniscient
[Javascript]  by flexing_99Apr 29, 2023 12:02 AM 3 269 Apr 29, 2023 12:02 AM
Last Post: Zgh600
[Javascript]  by FacewipeSep 7, 2021 03:44 AM 1 177 Sep 7, 2021 03:44 AM
Last Post: kitkatas