January 4, 201412 yr I'm using dialog boxes for a website I'm doing http://jqueryui.com/dialog/. I'm wanting it to pop up by clicking in two places, for every dialog window to pop up. One is the title and the other is at the read more part (see attached). Here is the code. <div> <a class="more" id="pin_1" name="pin_1" title="Text1">Text1</a> </div> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In quis orci id erat elementum congue a eu dolor. Mauris et fermentum sem. Nullam consequat ligula eu diam malesuada,<br> <a class="more" id="pin_1" name="pin_1" title="Text1">Read More »</a></p> <div id="dialog_1"> <p>Phasellus lacinia dignissim mattis. Nullam arcu elit, mollis quis pharetra vitae, egestas nec est. Nunc bibendum scelerisque dapibus. Vivamus pulvinar sapien non tincidunt vehicula. Mauris a lacus vulputate, fringilla neque congue, lacinia mi. Mauris semper dui mi, ut blandit ante euismod et. Phasellus vitae odio id nisl blandit consectetur ut nec lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Mauris sem ante, sollicitudin nec fermentum non, pellentesque id lacus.</p> </div> <div> <a class="more" id="pin_2" name="pin_2" title="Text2">Text2</a> </div> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In quis orci id erat elementum congue a eu dolor. Mauris et fermentum sem. Nullam consequat ligula eu diam malesuada,<br> <a class="more" href="#" id="pin_2" name="pin_2" title="Text3">Read More »</a></p> <div id="dialog_2"> <p>Phasellus lacinia dignissim mattis. Nullam arcu elit, mollis quis pharetra vitae, egestas nec est. Nunc bibendum scelerisque dapibus. Vivamus pulvinar sapien non tincidunt vehicula. Mauris a lacus vulputate, fringilla neque congue, lacinia mi. Mauris semper dui mi, ut blandit ante euismod et. Phasellus vitae odio id nisl blandit consectetur ut nec lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Mauris sem ante, sollicitudin nec fermentum non, pellentesque id lacus.</p> </div> $( "#dialog" ).dialog({ // Run when the dialog is created. create: function( e, ui ) { // Get the dialog components we need to adjust. var $parent = $( this ).parent(), $button = $parent.find( ".ui-dialog-titlebar-close" ), $title = $parent.find( ".ui-dialog-title" ); // Adjust the dialog title styles. $title.css( { width: "auto", margin: "0.44em 0" } ); // Reconfigure the close button to show text, not an icon. // Also make style adjustments. $button.removeClass( "ui-dialog-titlebar-close" ) .css( "float", "right" ) .button( "option", { icons: {primary: false}, text: true, label: "Close" }); } }); $.fx.speeds._default = 800; $(function () { $("#dialog_1").dialog({ position: ["center", "center"], height: 400, width: 700, title: 'Links', autoOpen: false, hide: "explode", show: "explode", title: "Building Defects" }); $("#pin_1").click(function () { $("#dialog_1").dialog("open"); return false; }); }); $.fx.speeds._default = 800; $(function () { $("#dialog_2").dialog({ position: ["center", "center"], height: 400, width: 700, title: 'Links', autoOpen: false, hide: "explode", show: "explode", title: "Building Disputes" }); $("#pin_2").click(function () { $("#dialog_2").dialog("open"); return false; }); }); Edited January 4, 201412 yr by Vulcan
January 10, 201412 yr Your ids are not unique! #pin_1 and #pin_2 are both duplicated, and only the first one encountered will be recognised. Either use unique ids, or use classes for a shorter and more generic solution.
January 16, 201412 yr Author What do I need do to make it work? (I'm new to JavaScript so still learning)
January 16, 201412 yr Well, if it was me, I'd probably do something along the lines of <div class='container'> <div class='title-block'><a class='the-title open-dialog' href='#'>Title 1</a></div> <p>Visible text 1... <br /><a class='read-more open-dialog' href='#'>Read More</a> </p> <div id='dialog_1'> <p>The dialog1 text.... </p> </div> </div> <div class='container'> <div class='title-block'><a class='the-title open-dialog' href='#'>Title 2</a></div> <p>Visible text 2... <br /><a class='read-more open-dialog' href='#'>Read More</a> </p> <div id='dialog_2'> <p>The dialog2 text.... </p> </div> </div> and jQuery(function($){ var dialogOnCreate = function( ev, ui ){ // do whatever needs to be done to each of the dialogs... var $parent = $( this ).parent(), $button = $parent.find( '.ui-dialog-titlebar-close' ), $title = $parent.find( '.ui-dilaog-title' ); // etc, etc... }; $.fx.speeds._default = 800; $( '.container' ).each( function(){ var dialog = $( this ).children( 'div' ).last(); dialog.dialog({ height: 400, width: 700, title: $( this ).find( '.the-title' ).text(), autoOpen: false, hide: 'explode', show: 'explode', create: dialogOnCreate }); $( this ).find( '.open-dialog' ).data( 'openDialog', '#' + dialog.attr( 'id' ) ) }).on( 'click', '.open-dialog', function(){ $( $( this ).data().openDialog ).dialog( 'open' ); return false; }); }); Please note that this is off-the-cuff, completely untested, and therefore prone to error.
January 18, 201412 yr Author I've pasted in the code and it doesn't work. I've played around adding bits in and cant work out why doesnt work. The text that is meant to be in the dialog box isn't doing anything. (no dialog box is showing)
January 19, 201412 yr My code was intended to illustrate the concept - classes instead of ids - rather than the execution. And I did state that it was off-the-cuff (though based on your original) and completely untested (there's at least one typo)! I would suggest that you use your browser's developer tools to see what errors the browser is reporting, and to see what the javascript is doing to the HTML (if anything). Personally I'm a huge fan of Firefox and its Firebug extension, but other browsers do have tools of their own. Since you haven't given much of a clue as to what might or might not have been working in your original code, I don't even know whether your page is loading jQuery and jQuery UI correctly, or what versions of either you are using.
Create an account or sign in to comment