1

there is a button in my code. Apart from using mouse and clicking it, I'd like to fire (pressing ENTER) it after selecting it with TAB.

<div id="CloseButton" tabindex="0" onkeypress="return submitOnEnter(blah,event)">Close</div>

if it helps:

function submitOnEnter(blah,e) {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13) {
        //at this point I need to fire a button w/o using its id
        return false;
    } else {
        return true;
    }
}

If someone knows how to do it?

Is it possible to make 'blah' refer to <div>, such that I could do: $(blah).click();

6axter82
  • 569
  • 2
  • 8
  • 19

2 Answers2

0

this could work:

jQuery(':focus').click()
Ritzelprimpf
  • 176
  • 1
  • 11
0

here is my solution:

part of HTML, close button is a div element and is a part of a modal window:

...
<div class="closeButton">
      <span id="closeSpan" tabindex="0">Close</span>
</div>
...

JS, within the ModalWindow class:

$(modWin).find("#closeButton").one("click",            
                   this.close).keypress(this.doNothing);

this.close  = function () {
    $("#modalWindow").css("display","none").fadeOut(200).html("");
    $("#modalWindow #closeButton").hide();
    $(window.self.document.body).find("#modalWindow").remove();
}

this.doNothing = function (e) {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13) {
        innerthis.close();
        return false;
    } else {
        return true;
    }
}

PS: innerthis is defined within the ModalWindow class as:

var innerthis = this;
6axter82
  • 569
  • 2
  • 8
  • 19