[Javascript] attach actions to all objects in a form?

Chris T christ at saeweb.com
Wed May 26 13:37:30 CDT 2004


I've never personally used addEventListener, but would something like this
work?

======================

var objForm = document.forms["formName"]
var objEl = null
for(var x = 0; x < objForm.elements.length; x++){
    objEl = objForm.elements[x]
    objEl.onclick = captureAllClicks
    // which should also be able to be written as:
    // objEl.onclick = function(){
    //     Some code in here
    //}
}

function captureAllClicks(){
    // I'm confused on why you're passing in textbox in
    // your original code so I don't know how you would
    // write this function so that it works properly
}

======================


I really should test this code before sending, but...

:)

Chris Tifer


----- Original Message ----- 
From: "Roger Roelofs" <rer at datacompusa.com>
To: "[JavaScript List]" <javascript at LaTech.edu>
Sent: Wednesday, May 26, 2004 2:12 PM
Subject: Re: [Javascript] attach actions to all objects in a form?


> Shawn,
>
> On May 26, 2004, at 1:52 PM, Shawn Milo wrote:
>
> > Is there a way to write an onclick function, or something
> > similar, so that it applies to all objects on a form?

> > <script type="text/javascript">
> >    function captureAllClicks(textbox){
> >       //this happens on the
> >       //onclick event of any
> >       //text box on the form
> >
> >       textbox.value = 'you just clicked this box';
> >    }
> > </script>

> Yes,
>
> ///  setup
> var el = document.getElementById("theContainer");
> if (el) {
> if( el.addEventListener ) {
> el.addEventListener("click", clickableTableRowClick, false);
> } else {
> if(el.attachEvent) {
> el.attachEvent("onclick", clickableTableRowClick);
> } else {
> el.onclick = clickableTableRowClick;
> }
> }
> }
>
> // callback routine
> function clickableTableRowClick(evt) {
> var targ, oldRow;
>      if (!evt) evt = window.event;
>      if (evt.target) targ = evt.target;
>      else if (evt.srcElement) targ = evt.srcElement;
> ...
> do what you want with the target element




More information about the Javascript mailing list