[Javascript] Which submit caused the event?

Mike Dougherty mdougherty at pbp.com
Tue Jan 1 21:48:49 CST 2013


On Tue, Jan 1, 2013 at 4:22 PM, Hassan Schroeder <hassan at webtuitive.com>wrote:

> <head>
> <script type="text/javascript">
> document.onclick=function(e){
>   var ev = window.event || e;
>   alert(ev.target.name);
> }
> </script>
>

no addEventListener/attachEvent ?

I understand if you feel its overkill for a simple example.  However, if
you feel "real world" code should be using addEventListener you could skip
it for the example but leave a /* reminder comment */


> </head>
> <body>
>
> <form action="" method="post" name="myform" onsubmit="javascript:return
> false;" >


Do we really need "javascript:" prefix?  I'm asking your opinion about best
practice.  I read once that since javascript is the default and the
alternatives never really caught on, that it's unnecessary.

Should this event registration even be done in markup?  If the script tag
with the document.onclick registration failed for some reason, this form
would be completely broken.  Granted, the action="" is not ideal - but many
browsers would treat that as a post to the current url.  Since Tedd
mentioned AJAX, I'm guessing he has no interest in progressive enhancement
or making this form fallback/work as a normal submit.


>
>    <input type="submit" name="edit" value="Edit">
>    <input type="submit" name="save" value="Save">
>    <input type="submit" name="cancel" value="Cancel">
> </form>
>

Tedd, in your testing did you consider:

   <input type="submit" name="dowhat" value="Edit">
   <input type="submit" name="dowhat" value="Save">
   <input type="submit" name="dowhat" value="Cancel">

I've mostly handled forms on the server (no ajax) and I've found it easier
to have one submitting control with a value of the "what" to do.  Since
there's only ever a single submit control "active" there isn't duplicated
values.  A problem with this is what 'value' do you consider the default
when a user presses the enter key inside an input[type=text]? Also from a
UI perspective, what is the difference between a "save" and an "edit" ?


More information about the Javascript mailing list