Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2005
    Posts
    78
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Javascript onchange event handler help

    Hi, I'm building a site for a friend, and he wants to include a price calculator that updates any time a text input (input type=text, not textarea), checkbox, or select is changed. I could add the HTML onChange attribute to every input tag, but that would be tedious, not to mention a waste of space.

    I tried using document.getElementsByTagName('input').onchange=function() but that didn't work . Any ideas?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,441
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    Code:
    var flds = document.getElementsByTagName("input");
    for ( f = 0; f < flds.length; ++f )
    {
        var fld = flds[f];
        if ( fld.type == "text" ) // I think this is right type, but check it with an alert maybe?
        {
             fld.onchange = function....
        }
    }

  • Users who have thanked Old Pedant for this post:

    ikilledsanta (03-16-2009)

  • #3
    New Coder
    Join Date
    May 2005
    Posts
    78
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Code:
    var flds = document.getElementsByTagName("input");
    for ( f = 0; f < flds.length; ++f )
    {
        var fld = flds[f];
        if ( fld.type == "text" ) // I think this is right type, but check it with an alert maybe?
        {
             fld.onchange = function....
        }
    }
    This worked perfectly, thanks.


  •  

    Posting Permissions

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