I want to show popup box on simple HTML button click using jQuery in MVC3. What would be a sample of how to do this?
4 Answers
To handle button click events and show a div on the click you can do:
$("#button").click(function() {
$("#yourDialog").show();
});
You could also use a jQuery Dialog to show a "popup".
$(document).ready(function() {
$("#yourDialog").dialog();
});
$("#button").click(function() {
$("#yourDialog").dialog('open');
});
Comments
Here's how I display a pop up box that selects various tag values, first the html:
<div style="display: none">
<div id="tagDialogBox" title="Add Tags">
@* other html here *@
</div>
</div>
Here's the jQuery, note that you need to add .parent().appendTo($("formname")) in order to allow the dialog fields to be submitted as part of the form:
function showTagDialogBox() {
$('#tagDialogBox').dialog({
autoOpen: true,
title: "Select Tags",
modal: true,
height: 530,
width: 800,
buttons: {
"Ok": function () {
$(this).dialog("close");
}
}
}).parent().appendTo($("form:first"));
return false;
}
Comments
This is extremely simple.
<input type="button" onclick="$('dialogbox').dialog();"/>
Also, examples on the jquery ui page. This one is very simple (though it also includes animation, you can pull that out as an example)