-1

To the good people who downvoted because it's been asked before: my question was not about the implicit event parameter. I had no idea. The event parameter was the answer, not the question.


function myEventHandler() {
  var eventSrcType=(event.srcElement) ? event.srcElement.type : 'type undefined';
  alert(eventSrcType);
}

The function gets called when the user clicks an image ("onclick"), e.g

<a href="javascript:void(0)" onclick="myEventHandler();">

Apparently there's something wrong with the assignment, because the alert is never executed. What's wrong here?

(I tried window.event, but Dreamweaver doesn't hint event as a member of window. Anyway, it doesn't work either)

edit
All answers hint at the implicitly passed event. So I added that as the function's argument (calling without argument),

function myEventHandler(event) {
  var eventSrcType=(event.srcElement) ? event.srcElement.type : 'type undefined';
  alert(eventSrcType);
}

But I still don't get an alert.

Passing this as argument shows "type undefined", but I expect the anchor type here.

stevenvh
  • 2,971
  • 9
  • 41
  • 54
  • @downvoter: please explain why this is not a good question. – stevenvh Nov 26 '13 at 13:33
  • I didn't DV, but probably because it has been asked multiple times before – Moritz Roessler Nov 26 '13 at 13:35
  • Do you get any (error-)messages in the javascript console? – Hans Kesting Nov 26 '13 at 13:41
  • It's because of `onclick="showFS()"` you're directly invoking the function without passing a parameter, hence `event` is `undefined` – Moritz Roessler Nov 26 '13 at 13:42
  • @C5H8: That would be the explanation in hindsight, but if OP has no idea what the problem is, he won't link it to the missing argument, hence not to other questions either. (+1 to cancel the downvote.) – Johan.A Nov 26 '13 at 13:55
  • 2
    Ehh... stop using inline event handlers (switch to `addEventListener` ) and that stupid `javascript:void(0)` abomination. – kapa Nov 26 '13 at 14:34
  • 1
    @bažmegakapa: since you call `javascript:void(0)` both stupid and an abomination, could you tell us what is the right way? A link is OK. – Geert Goeteyn Nov 26 '13 at 15:36
  • 1
    @GeertGoeteyn Glad you asked :). When you need an element, that does nothing but invokes a Javascript click event handler, use a ` – kapa Nov 26 '13 at 16:57

6 Answers6

6

You are missing the event parameter in function, so you have probably got a null reference exception.

function myEventHandler(event) {
  var eventSrcType=(event.srcElement) ? event.srcElement.type : 'type undefined';
  alert(eventSrcType);
}
Miroslav Popovic
  • 12,100
  • 2
  • 35
  • 47
  • What do I pass in the onclick event? Also, shouldn't I get "type undefined" displayed? – stevenvh Nov 26 '13 at 13:08
  • 3
    @stevenh You pass nothing. Every event handler will automatically get the event object as its first argument. Your code fails with an error because `event` is undefined so it cannot have an `srcElement` property - or any other property. Learn to check your browser console please, you will see errors that way. – kapa Nov 26 '13 at 13:09
  • @bažmegakapa Not if someones calling it without parameters,by assigning `"myEventHandler()"`, to their elements onclick attribute – Moritz Roessler Nov 26 '13 at 14:03
1

The event variable is not defined anywhere. It is usually passed to event callbacks as the first parameter, but it is not included in your function signature. Try this function signature:

function myEventHandler(event) {
Tim Heap
  • 1,671
  • 12
  • 11
1

All the answers above are correct. What you failed to understand is that you must pass that event object to the function in which you use it. I checked your code and your click handler is function onclick(event) { showFS();} but it should be function onclick(event) { showFS(event);}

Nikola Dimitroff
  • 6,127
  • 2
  • 25
  • 31
  • I understand what you mean, but my code is different from what you say. I'm not **calling** showFS in my event handler. I'm **assigning** showFS as **the** event handler. As such I expect it to cover the same list of arguments. – stevenvh Nov 26 '13 at 14:34
0

There's an implicit event parameter in onclick

function myfunction(event)
{
    console.log("srcElement " + event.srcElement);
}

http://javascript.info/tutorial/obtaining-event-object

IE pre-IE9 doesn't pass it so you need to check window .event.

TygerKrash
  • 1,362
  • 2
  • 20
  • 38
0

Your problem, besides, not specifying the event parameter is the way you're attaching the eventlistener.

You're attaching your click-event handler by setting the onclick attribute of your elements to "myEventHandler()" which is, well, kind of bad, as

  • Your string has to be evaluated, for your function call, to happen
  • You're directly invoking your function, passing zero parameters

If you would leave out the parens, only setting onclick="myEventHandler", you wouldn't explicitly call you function without parameters anymore. But it's still a string value, which has to be evaluated in order to obtain a reference to your actual function.

What you probably want to do is attaching your eventlistener via EventTarget.prototype.addEventListener, e.g.

var images = document.getElementsByTagName ("img");

[].forEach.call (images, function (img) {
  img.addEventListener ("click", myEventHandler);
})

Your function could then look like this

function myEventHandler(event) {
  var eventSrc = event.srcElement || event.target || {},
      eventSrcType = eventSrc.type || "type undefined";

      alert(eventSrcType);
}
Moritz Roessler
  • 8,542
  • 26
  • 51
-2

The HTML Code must look like this

<img onclick="myEventHandler(this)" />

JS:

function myEventHandler(event) {
    var eventSrcType=(event.srcElement) ? event.srcElement.type : 'type undefined';
    alert(eventSrcType);
}

And you should see "type undefined" displayed.

You get different results depending on whether you omit this and/or event. Try each combination until you get your desired result.

Roberto
  • 526
  • 2
  • 10