toniet
toniet

Reputation: 3

How can i refer to a Modal Window in Dom?

I've gotten hundreds of aids from this site. thanks. This is my first question. Which object is a modal window (alert popup) into the Dom. How can i refer it? How can i know if open or closed? Something like this: if (alertPopup is open) {..code...} My code is this (i use jQuery):

<script type="text/javascript">
$(document).ready(function(){
     var myButton = $('#mybutton')

     myButton.click(function(){
        if ($('#myinput').val() == '') {
            alert('input Empty!');
        } else {
            // More code.
        }

     });

     $(document).keyup(function(e){
        if (e.keyCode == 13) myButton.trigger('click');
     })
});
</script>

<body>
    <input id="myinput" />
    <button id="mybutton">Show alert</button>
</body>

The purpose of the code is trigger up the event 'click' on the button whith key 'enter'. It works, but when i close the popup, again with key 'enter', the popup comes again an again. I need to disable event 'click' of my button or unbind the trigger action when the popup is displayed. I would't like to make my own modal windows. thanks in advance.

Upvotes: 0

Views: 364

Answers (1)

Joe Rinehart
Joe Rinehart

Reputation: 763

You can move the handler to its own function and programmatically bind/unbind it to the event:

$(document).ready(function(){
     var myButton = $('#mybutton')

     console.log('whee');

     myButton.click(clickHandler);

     $(document).keyup(function(e){
        if (e.keyCode == 13) myButton.trigger('click');
     })
});


function clickHandler(){
    $('#mybutton').unbind('click', clickHandler)

    if ($('#myinput').val() == '') {
        alert('input Empty!');
    } else {
        // More code.
    }

}

However, it looks more like you're trying to deal with enter buttons in a form submission style. I'd recommend wrapping this whole thing in a form and dealing with it as such.

See http://jsfiddle.net/ruBY4/ for a cleaner form-based solution.

Upvotes: 1

Related Questions