Search code examples
javascriptjqueryhtmldefaultbutton

Defaultbutton div


I am just curious; does any of you have any good javascriptresources for setting defaultbutton for all elements inside a div? I have a lot of divs that executes various javascriptfunctions and I would like all of them to have their buttons script executed when enter is pressed.

Thanks in advance!


Solution

  • The way I would do it is give the elements that you want to be fired a class, for example 'acceptEnter'. Then create a handler for the containing DIVs which looks for that class on keypress. Hope this helps :)

    $('div').keypress( function(e) {
       if(e.keyCode == '13') {
         $(this).find('.acceptEnter').click();
       }
    });