event trigger: explicitOriginalTarget


A co-worker had a javascript issue regarding how to determine what button was pressed to submit a form from within that submit function. The idea was to have a single form with two buttons. Button 1 would have one action and button 2 would have another. Rather than tying click events to both buttons it was hoped a single submit even on the form could sort out what was pressed.

At first it seemed odd that there wasn’t an obvious solution (aside from the separate click events). But thinking more about how that form would function without javascript, it made sense. To complete the task with a full submit request, either there would have to be a checkbox or radio button to select the desired action follow by a single button or there would need to be 2 forms, each with its own button. There wouldn’t be a (cross browser) server solution otherwise as it wouldn’t be able to tell what was clicked.

The click event is deciding the fate of what happens in submit. Not the submit determining its own path based on what already happened. I was convinced it was fine that the browser wouldn’t tell what object triggered the event.

Beyond standards

Some more research turned up a Mozilla only property event.explicitOriginalTarget. It holds the object that triggered an event. The use case even matches my co-workers exact situation.

For example, assuming jQuery is on the page and it’s run in Firefox

<form action="something.hmtl" class="form">
		<button type="submit" value="foo" class="foo">Foo</button>
		<button type="submit" value="bar" class="bar">Bar</button>

<script type="text/javascript">
		console.log(e.originalEvent.explicitOriginalTarget.value); // = foo or bar, as intended
		return false;

So just after convincing myself event.explicitOriginalTarget wasn’t needed, doubt has returned in that some one has considered it enough to bother implementing it in a browser. I can still see argument for why it would be more reasonable to just attach to the click event of each object rather than forking in the submit, but can’t argue this wouldn’t be useful if all browsers had it.

Skateboards and bikes are better at nights

Skateboards and bikes are better at nights. 0 comments.

Search WayTooCrowded
The Header Should Always Point Home