SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    Toronto, ON, Canada
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question addListener to many elements

    I have a page with many input text fields and need to have onClick, onBlur and onChange trigger functions for evey one. Is there a way to bind an event listener to all the input elements such that when an event occurs it fires a function that can then determine the name or id of the source input tag (event.srcElement) and process the rest of the function? I don't want to include "onBlur, onClick and onChange" in each tag as there are hundreds to thousands of input fields.

    Thanks Rob.

    PS this is the code now:

    <input type=text name=XData123 value="26,121.00" onchange="javascript:XData_onchange('XData123', 2);" onfocus="javascript:XData_focus();" onblur="javascript:XData_onblur();">

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aaron's excellent article is just what you need

    Well-Behaved DHTML: A Case Study

  3. #3
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use these two functions myself, which aren't too dissimilar from Aaron's code
    Code:
    function _addEvent( obj, evt, fn, useCapture )
    {
    	if ( typeof obj.attachEvent != 'undefined' )
    	{
    		obj.attachEvent( "on" + evt, fn );
    	}
    	else if ( typeof obj.attachEventListener != 'undefined' )
    	{
    		obj.addEventListener( evt, fn, Boolean( useCapture ) );
    	}
    }
    function _addEvents( obj, evts, fn, useCapture )
    {
    	var i = 0, evt;
    	while( evt = evts[i++] )
    	{
    		_addEvent( obj, evt, fn, Boolean( useCapture ) );
    	}
    }
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •