Multiple buttons to trigger single jQuery function - javascript

I want to use multiple images to open the same jQuery dialog box. I have noticed that this only works for the first image, is there any way to make the second one work as well without giving it a unique id? There will be 100+ of these images so I don't want to create a new function for each image
$(function() {
$("#dialog").dialog({
autoOpen: false},{ buttons: [ { text: "Ok", click: function() { $( this ).dialog( "close" ); } } ]});
$("#IMG").on("click", function() {
$("#dialog").dialog("open");
});
});
<div id="dialog" title="Dialog box">Test</div>
<input type="image" id="IMG" src="img/image.gif">
<input type="image" id="IMG" src="img/image.gif">

Specify a class for each image, say "img". Then declare a handler like this:
$(".img").on("click", function() {
$("#dialog").dialog("open");
});

IDs have to be unqiue. Use a class instead:
$(function() {
$("#dialog").dialog({
autoOpen: false},{ buttons: [ { text: "Ok", click: function() { $( this ).dialog( "close" ); } } ]});
$(".open-img").on("click", function() {
$("#dialog").dialog("open");
});
});
HTML:
<div id="dialog" title="Dialog box">Test</div>
<input type="image" class="open-img" src="img/image.gif">
<input type="image" class="open-img" src="img/image.gif">

Related

Sending the next element of the clicked button into jQuery UI dialog box

After trying to animate an element, when clicking on a button using this script:
$(document).ready(function()
{
$("#oone, #otwo, #othree, #ofour, #ofive, #osix, #oseven, #oeight, #onine, #oten, #otwelve, #otwenty, #othirteen, #ofourteen, #ofifteen, #osixteen, #loone, #lotwo, #lothree, #lofour, #lofive, #losix, #loseven, #loeight, #lonine, #laaten, #lotwelve, #lotwenty, #lothirteen, #lofourteen, #lofifteen, #losixteen").click(function()
{
// $(this).css('border', "solid 2px red");
// var divs = $("#div_one, #div_two, #div_three, #div_four, #div_five, #div_six, #div_seven, #div_eight, #div_nine, #dive_ten, #div_eleven, #div_twelve, #div_thirteen, #div_fourteen, #div_fifteen, #div_sixteen, #div_lone, #div_ltwo, #div_lthree, #div_lfour, #div_lfive, #div_lsix, #div_lseven, #div_leight, #div_lnine, #dive_lten, #div_leleven, #div_ltwelve, #div_lthirteen, #div_lfourteen, #div_lfifteen, #div_lsixteen");
// $(divs).siblings().slideToggle();
$(this).next().slideToggle();
$(this).siblings().slideToggle();
});
});
I've got some animation unwanted result. So I decided, instead of animating the next element of the clicked button, why I don't send the result into dialog bix using jQuery UI plugin. So I tried the following:
<script src="../include/jquery-1.12.1.min.js"></script>
<script src="../include/jquery-ui.min.js"></script>
<script src="../include/bootstrap.min.js" type="text/javascript"></script>
$(document).ready(function() {
$("#oone, #otwo, #othree, #ofour, #ofive, #osix, #oseven, #oeight, #onine, #oten, #otwelve, #otwenty, #othirteen, #ofourteen, #ofifteen, #osixteen, #loone, #lotwo, #lothree, #lofour, #lofive, #losix, #loseven, #loeight, #lonine, #laaten, #lotwelve, #lotwenty, #lothirteen, #lofourteen, #lofifteen, #losixteen").click(function() {
$(this).next().dialog({
autoOpen: false,
hide: "puff",
show: "slide",
width: 800,
modal: true
});
//$(this).dialog("open");
});
});
And here is the html code for only the first 2 buttons, because the cod is too long:
<div class="form-group col-md-12">
<input class="img1" type="image" style="width:60px;height:60px" src="../images/molar_left_t.png" id="oone" name="one" alt="button" />
<div id="div_one" class="collapse">3rd Molar:
<?php echo $resTeeth['one'] ?>
</div>
<input class="img1" type="image" style="width:60px;height:60px" src="../images/molar_left_t.png" id="otwo" name="two" alt="button" />
<div id="div_two" class="collapse">
<?php echo $resTeeth['two'] ?>
</div>
So I had this error:
jquery-1.12.1.min.js:2 Uncaught Error: cannot call methods on dialog prior to initialization; attempted to call method 'open'
How to fix this error, and is it possible to send the next() div element into dialog box using jQuery UI, or should I specify id for each div, and create a dialog script for each one of them ?
Your problem is, you're creating the dialog from the next div, but you're trying to open the dialog on the CURRENT div.
You can fix this pretty easy:
$(document).ready(function() {
$("#oone, #otwo, #othree, #ofour, #ofive, #osix, #oseven, #oeight, #onine, #oten, #otwelve, #otwenty, #othirteen, #ofourteen, #ofifteen, #osixteen, #loone, #lotwo, #lothree, #lofour, #lofive, #losix, #loseven, #loeight, #lonine, #laaten, #lotwelve, #lotwenty, #lothirteen, #lofourteen, #lofifteen, #losixteen").click(function() {
var dialog = $(this).next();
dialog.dialog({
autoOpen: false,
hide: "puff",
show: "slide",
width: 800,
modal: true
});
dialog.dialog("open");
});
});
I made the assumption you want the dialog to contain the contents of the next (following) div.
This would do that:
$(document).ready(function() {
var dialog = '<div class="mydialog" title="Basic dialog"><p class="dialogcontent">me</p></div>';
var newDiv = $(dialog);
newDiv.dialog({
autoOpen: false,
hide: "puff",
show: "slide",
modal: true,
buttons: {
Ok: function() {
$(this).dialog("close");
}
}
});
$(".form-group").on('click', ".img1", function() {
var me = $(this);
newDiv.find('.dialogcontent').html(me.next('.collapse').html())
newDiv.dialog("open");
});
});
Example in action: https://jsfiddle.net/89pyhsuj/

Close button X not removed

I'm using jqueryui dialog, I want to hide the X button there at the top.
I tried the code below, but it didn't work.
$( '.ui-dialog-titlebar-close').remove();
My JS:
$(function () {
$("#dialog").dialog({
closeOnEscape: false,
...
});
HTML:
<div id="dialog" title="Confirmation Message" class="dialogboxstart">
<fieldset>
<div class="dialogRow">
<span class="dialogHeading" id="confirmMsg"></span>
</div>
</fieldset>
</div>
Try doing that in the open function of dialog.
JS:
$(function () {
$("#dialog").dialog({
open: function () {
$('.ui-dialog-titlebar-close').remove(); //Remove here.
}
});
});
Demo: http://jsfiddle.net/lotusgodkk/GCu2D/459/
You probably need this
$(".ui-dialog-titlebar-close").hide();
Use the remove in document.ready.
$(document).ready(function(){
$( '.ui-dialog-titlebar-close').remove();
});
Here is the fiddle.

Why does my jquery function not work on div in page loaded by ajax

I'm just starting out with jquery. Already learned some things and like it, but I have been struggling with the following issue for a few days.
I copied the "dialog-confirm"-function from https://jqueryui.com/. I placed this script between the tags on my index.php page.
<script type = "text/javascript">
$(document).ready(function(){
$(function() {
$( "#dialog-confirm" ).dialog({
resizable: false,
height:140,
modal: true,
buttons: {
"Delete all items": function() {
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});
$(window).resize(function() {
$('#scrollpage').height($(window).height() - 250);
});
$(window).trigger('resize');
$('.container').on('click', '.mainmenu', function(event){
event.preventDefault();
var url = $(this).attr('href');
$.get(url, function(data) {
//alert(data);
$("#div1").load(url);
});
$( this ).parent().addClass('current_page_item');
$( this ).parent().siblings().removeClass('current_page_item');
});
$('.container').on('click', '.rapport', function(event){
event.preventDefault();
//$(".dialog-confirm").dialog( "open" );
var url = $(this).attr('href');
$.get(url, function(data) {
//alert(data);
$("#div1").load(url);
});
});
});
</script>
If i place the matching div in the same index.php page. It works fine, the div pops up.
<div id="dialog-confirm" title="Empty the recycle bin?">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>Blablabla</p>
</div>
However when i place the div in a page which is loaded by ajax in the div1, then I cant get it to work.
<div class="scrollpage" id="scrollpage">
<div class="container" class="page" id="div1">
</div>
</div>
Can anyone explain to me why this is, and how I can fix this?
What's happening is that your $(document).ready() function executes as soon as the DOM is loaded. This DOM contains just what it is in the html file. At that time, there's no div with an id equal to 'dialog-confirm'. Loading pieces of HTML with ajax doesn't trigger a DOMReady event. What you've got to do is to call the .dialog() jQuery function AFTER you've loaded the div with Ajax:
$("#div1").load(url, function() {
$( "#dialog-confirm" ).dialog({
resizable: false,
height:140,
modal: true,
buttons: {
"Delete all items": function() {
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});
Here is what happens in your program(loading div with ajax):
First, your script initiates dialog window container by looking an element with id dialog-confirm.
Since, you don't have an element with that id yet, dialog container cannot be prepared.
There are two ways you can make it work:
Call dialog() after ajax requests,
Place div statically on page and change content with ajax request.
Solutions:
1- Use the code below instead of $("#div1").load(url);
$("#div1").load(url, function(){
$( "#dialog-confirm" ).dialog({
resizable: false,
height:140,
modal: true,
buttons: {
"Delete all items": function() {
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});
2- Place divs statically on your page:
<div class="scrollpage" id="scrollpage">
<div class="container" class="page" id="div1">
<div id="dialog-confirm" title="Empty the recycle bin?">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>Blablabla</p>
</div>
</div>
</div>
Then load just <p>... with $("#dialog-confirm").load(data); instead of $("#div1").load(url);.
Your jquery is executed only once after your page is loaded. Then is initiating everything needed for the tool!
This means every .container is being 'transformed'.
If you later add a new div.container it is not 'transformed' yet. You have to execute the jquery again after your div is appended!

How to Window.Open inside a jQuery Modal

I have my Modal Div as:
<div id="dialog-modal" title="Open File">
<img alt="progress" src="images/ajax-loader.gif"/>
</div>
On Click of Button I am able to see Modal and then see the progress icon.
<script type="text/javascript">
$(function () {
$('#button1').click(function () {
$('#dialog-modal').dialog("open");
});
$("#dialog-modal").dialog({
autoOpen: false,
height: 140,
modal: true,
});
});
</script>
But How can I do a operation or function on Load of Modal Window??
Let's say , I want to download a file from server , when the modal window appears (..showing the progress icon)
You can hook into the dialogopen function:
$( "#dialog-modal" ).on( "dialogopen", function( event, ui ) {
console.log('Wayhay!!');
window.open("http://www.google.com");
} );
See jsFiddle:
http://jsfiddle.net/3Y63f/1/
Further info here:
http://api.jqueryui.com/dialog/#event-open

jquery ui popup doesn't work more then once

I am working on mvc project where jquery is heavily used. In one of the views there us accordion control with multiple (three) views inside.
The jquery popup works fine in the first panel, but once I close that panel, the popup doesn't want to work again.
I have no idea what can be, although I used http://blog.nemikor.com/2009/04/08/basic-usage-of-the-jquery-ui-dialog/ and http://jsfiddle.net/DSNt5 as guides.
Please find the code below.
Markup:
<div>
#Html.Hidden("Id", Model.Report.Id)
<div id="accordion">
#foreach (var item in Model.Parameters)
{
<h3>#Html.LabelFor(m => item.Name, item.Prompt)</h3>
<div>
<div class="editor-label">
Search #*Html.TextBox("Search")*#
<input id="#("Search" + item.Name)" type="text" name="q" data-autocomplete="#Url.Action("QuickSearch/" + item.Name, "Report")" />
</div>
<div class="editor-field">
<select multiple id="#("Select" +item.Name)" name="#("Select" +item.Name)"></select>
</div>
<div class="removed" style="clear:both; float:left; margin-left:440px;">
Remove selection
<button id="opener">Open Dialog</button>
<h2 class="demoHeaders">Dialog</h2>
<p><span class="ui-icon ui-icon-newwin"></span>Open Dialog</p>
</div>
</div>
}
</div>
<p style="text-align: right">
<input type="submit" value="Generate Report" />
</p>
</div>
JS:
<script type="text/javascript">
$(document).ready(function () {
var $dialog = $('<div></div>')
.html('This dialog will show every time!')
.dialog({
autoOpen: false,
title: 'Basic Dialog'
});
$('#opener').click(function () {
$dialog.dialog('open');
// prevent the default action, e.g., following a link
return false;
});
});
</script>
<script type="text/javascript">
$(function() {
$( "#dialog2" ).dialog({
autoOpen: false,
show: "blind",
hide: "explode"
});
$('#opener').click(openDialog);
})
var openDialog = function(){
$('#dialog2').dialog('option', 'buttons',{
"Cancel":function(){
$('#dialog2').dialog('close');
}
});
$('#dialog2').dialog('open');
</script>
I have the buttons from both samples there, and both of them are doing the same thing.
Every advice will be greatly appreciated.
Thanks in advance, Laziale
UPDATE:
<script type="text/javascript">
$(document).ready(function () {
{
$("#dialog2").dialog({
autoOpen: false,
show: "blind",
hide: "explode"
});
$('#opener').click(openDialog);
}
});
</script>
<script type="text/javascript">
var openDialog = function(){
$('#dialog2').dialog('open');
$('#dialog2').dialog('option', 'buttons',{
"Cancel":function(){
$('#dialog2').dialog('close');
}
});
$('#dialog2').dialog('open');
</script>
You should only initialize your dialog once. You are reinitializing it every time you click.
Initialize it on document ready and then in your click handler just call
$('#dialog2').dialog('open');
EDIT:
You still have dialog initialization happening in your openDialog function. Try this:
<script type="text/javascript">
$(document).ready(function () {
{
$("#dialog2").dialog({
autoOpen: false,
show: "blind",
hide: "explode",
buttons: {"Cancel": function(){
$('#dialog2').dialog('close');
}
});
$('#opener').click(function(){
$('#dialog2').dialog('open');
});
}
});
</script>

Categories