user1464139
user1464139

Reputation:

How can I add a click event to a button and get it to call a function?

I used the following to create a modal window:

function openAdminModal(dObj, content) {
    var modal = $.modal({
        title: dObj.title,
        closeButton: true,
        content: content,
        width: false,
        resizeOnLoad: true,
        buttons: {
            'Submit': function (win) {
                submitHandler(dObj.$link, $('#main-form'), false);
            },
            'Close': function (win) {
                win.closeModal();
            }
        }
    });
}

I am not sure this helps with the question but I am adding it just in case.

The modal it creates has the following:

<div id="modal" style="display: block;">
....
    <button type="button">Submit</button>

The button is not inside of a form and the code around the button can't be moved. What I would like to do is to use jQuery to add an event to this button so that when it is clicked the following function is fired.

submitHandler(dObj.$link, $('#main-form'), false);

Can someone explain to me how I can use jQuery to add the calling of the function to the click event of the button. One thing I am not sure of is HOW can I select that button as it has no ID. The only thing it has is text of "Submit". Can I use the value of the variable modal that's on line 2 of the first code when doing my search?

Upvotes: 0

Views: 90

Answers (1)

Adil
Adil

Reputation: 148178

Try this,

Live Demo

$('button:contains("Submit")').click(function(){
  submitHandler(dObj.$link, $('#main-form'), false);
});​

Upvotes: 2

Related Questions