pyccki
pyccki

Reputation: 693

jQuery replacement for Default Button or Link

As everyone knows, that default button doesn't work in FF, only IE. I've tried to put in the tag or in the and it's not working. I've found a js script to fix this issue, but for some reason it's not working for me. This script for a submit button, i need to use it for a LinkButton, which is should be the same.

Link:

  <div id="pnl">
    <a id="ctl00_cphMain_lbLogin" title="Click Here to LogIn" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$cphMain$lbLogin&quot;, &quot;&quot;, true, &quot;Login1&quot;, &quot;&quot;, false, true))">Log In</a>
    <input name="ctl00$cphMain$UserName" type="text" id="ctl00_cphMain_UserName" />
    <input name="ctl00$cphMain$UserName" type="text" id="ctl00_cphMain_UserName1" />
  </div>

<script>

 $(document).ready(function() {
    $("#pnl").keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $("a[id$='_lbLogin']").click();
            return true;
        }
    });
});

I know that i can override original function "WebForm_FireDefaultButton" in This post, but i really wanted to get this one to work.

Thanx in advance!!!

Upvotes: 2

Views: 3370

Answers (3)

Oliver
Oliver

Reputation: 9488

Inspired by Larry Flewwelling's answer or rather Michael Cindric's, I came up with a modified and updated version of that javascript snippet. I've documented more detailed information on why and how in this blog post, but let me summarize the important changes:

  1. Works with dynamically loaded content
  2. Works with any number of "forms" on the same page

And here it is:

$('body')
  .off('keypress.enter')
  .on('keypress.enter', function (e) {
    if (e.which == 13 && e.target.type != 'textarea') {
      var $btn = $(e.target).closest('.jq-form').find('.jq-form-submit');
      if ($btn.length > 0) {
        if ($btn[0].type == 'submit') {
          $btn[0].click();
        }
        else {
          eval($btn[0].href);
        }
        return false;
      }
    }
  });

Upvotes: 1

Larry Flewwelling
Larry Flewwelling

Reputation: 144

I found the answer to fixing linkbuttons here: http://www.sentia.com.au/2009/03/fixing-the-enter-key-in-aspnet-with-jquery/

$(document).ready(function(){
  var $btn = $('.form_submit');
  var $form = $btn.parents('.form');

  $form.keypress(function(e){
      if (e.which == 13 && e.target.type != 'textarea') {
          if ($btn[0].type == 'submit')
              $btn[0].click();
          else
              eval($btn[0].href);
          return false;
      }
  });
});

Upvotes: 2

Chris Brandsma
Chris Brandsma

Reputation: 11736

You could expand your selector.

$("#pnl > input")

Which will then catch the input click for all the controls in the panel.

Upvotes: 0

Related Questions