SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2008
    Location
    Italy
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question add event for each input with a function.

    Hi,

    please I would to know If I have some input text with the same classname,
    can I add an event, for example onclick, for each of them without adding onclick="function();" in every input text in the code?

    for example I have:
    <input type="text" class="inputbox" name="text1">
    <input type="text" class="inputbox" name="text2">
    <input type="text" class="inputbox" name="text3">

    Without adding this for each input:
    <input type="text" class="inputbox" name="text3" onclick="function();">
    is it possible to set that onclick event with a js function for every input text with that same classname?

    many thanks.

  2. #2
    SitePoint Member
    Join Date
    Apr 2010
    Location
    Charlotte, NC
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using jQuery it's extremely easy:

    Code:
    $("input.class_name").bind("click", function() { 
       // ...function body...
    });
    If you'll have input elements added dynamically, you'll want to use live() or delegate() though instead of bind().
    LinkedIn : VernonK
    Web Development Resources : ChooseDaily.com


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
  •