How to use datepicker in dynamically created jQuery UI dialog? - javascript

I have trouble to active the datepicker() in a dynamically created jQuery UI dialog():
index.html
$(document).ready(function() {
var $loading = $('<img src="./images/loading.gif" alt="loading">');
$('.page-popup').each(function() {
var $dialog = $('<div></div>')
.append($loading.clone());
var $link = $(this).one('click', function() {
$dialog
.load($link.attr('href'))
.dialog({
title: $link.attr('title'),
width: 600,
height: 300
});
$link.click(function() {
$dialog.dialog('open');
return false;
});
return false;
});
});
$( ".datepicker" ).datepicker({
dateFormat: "yy-mm-dd"
});
});
The external page which gets loaded by a link like that:
Input
it has just a form to select or correct the date:
input.html
<form method="post" action="?">
<input type="text" name="date" value="2000-01-01" class="datepicker">
<input type="submit">
</form>
How can I activate the datepicker for the different dialogs?

Render the datepicker in the open event of the dialog as follows.
$dialog
.load($link.attr('href'))
.dialog({
title: $link.attr('title'),
width: 600,
height: 300,
open: function(){
$( ".datepicker" ).datepicker({
dateFormat: "yy-mm-dd"
});
}
});

The problem was that the subpage can not reload jquery.js and jquery-ui.js. So here my solution:
index.html
<html>
<head>
<script type="text/javascript" src="./js/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="./js/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var $loading = $('<img src="./images/loading.gif" alt="loading">');
$('.page-popup').each(function() {
var $dialog = $('<div></div>').append($loading.clone());
var $link = $(this).one('click', function() {
$dialog
.load($link.attr('href'))
.dialog({
title: $link.attr('title'),
width: 600,
height: 300
});
$link.click(function() {
$dialog.dialog('open');
return false;
});
return false;
});
});
$( ".datepicker" ).datepicker({
dateFormat: "yy-mm-dd"
});
});
</script>
</head>
<body>
Input
</body>
</html>
input.html
<html>
<head>
<!-- Don't load jquery and jquery-ui again!!! -->
<script type="text/javascript">
$( ".datepicker" ).datepicker({
dateFormat: "yy-mm-dd"
});
</script>
</head>
<body>
<form method="post" action="?">
<input type="text" name="date" value="2000-01-01" class="datepicker">
<input type="submit">
</form>
</body>
</html>

Related

Jquery Dialog doesnt show

I want to generate a dialog box that gets password from user, here's a bit my code.
<html>
<head>
<script type="text/javascript" src="jquery.min.js"></script>
<link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
</head>
<body>
<input type='button' id='btnDelete' name='btnDelete' value='Delete' onclick='deleteRecord(".$row['ID'].")'/>
<div id="delete-dialog" style="display: none;" title="Delete Record">
<label>Please type in admin's password:</label></br>
<input type='password' size='25' id='inpAdminPassword'/>
</div>
</body>
</html>
JS
function deleteRecord(ID){
var answer = confirm("Are you sure you want to delete this record?");
if(answer == true){
$( "#delete-dialog" ).dialog( "open" );
}
}
$(document).ready(function() {
$( "#delete-dialog" ).dialog({
autoOpen: false,
});
Somehow, the dialog box doesn't show, what must be missing?
I have also tried and included this code, but still it doesn't work.
$(document).ready(function() {
$( "#delete-dialog" ).dialog({
autoOpen: false,
});
$("#delete-dialog").dialog({
modal: true,
autoOpen: false,
height: 255,
width: 300,
buttons: {
"Delete": function() {
var password = document.getElementById('sessionPW').value;
var adminpw = document.getElementById('inpAdminPassword').value;
alert(password);
if(password == adminpw){
window.location = "deleteThreat.php?threatID="+ID;
}
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});
I just was trying your first solution and making the confirmation like this works for me:
js:
$("#delete-dialog").dialog({
autoOpen: false,
});
$("#open").click(function () {
if (confirm("are you sure?")) {
$("#delete-dialog").dialog("open");
}
});
html:
<div id="delete-dialog" style="display: none;" title="Delete Record">
<label>Please type in admin's password:</label></br>
<input type='password' size='25' id='inpAdminPassword'/>
</div>
<button id="open" >Open</button>

JQuery ui modal dialog external ui

I am trying to load another page in a jquery ui modal
This is my Js File
function eventWindow(url) {
getElementById("eventviewer").style.display="block";
$("#eventviewer").load(url).dialog({
modal:true,
buttons: {
Ok: function() {
$(this).dialog("close");
}
}
});
}
this the markup
<!doctype html>
<html>
<head>
<title><?php echo "Date:".$firstDayArray['month']." ".$firstDayArray['year'];?>
</title>
<link href="../css/main.css" rel="stylesheet" type="text/css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.0/jquery-ui.min.js"></script>
<script src="event.js" type="text/javascript"></script>
</head>
<body>
.....
<?php
echo "<td class=\"days\">".$dayArray["mday"]."<br/>".$event_title."</td>\n";
?>
<div id="eventviewer"></div>
......
</body
</html>
when i use window.open it works and opens in separate window.
But i am not able to open it in a jquery ui modal dialog.
There are multiple ways you can do this but I am not sure which one is the best practice. You can append an iFrame in the dialog container like this:
$("#dialog").append($("<iframe />").attr("src", "your link")).dialog({dialogoptions});
OR:
Working Fiddle
$(function () {
var iframe = $('<iframe frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>');
var dialog = $("<div></div>").append(iframe).appendTo("body").dialog({
autoOpen: false,
modal: true,
resizable: false,
width: "auto",
height: "auto",
close: function () {
iframe.attr("src", "");
}
});
$(".thumb a").on("click", function (e) {
e.preventDefault();
var src = $(this).attr("href");
var title = $(this).attr("data-title");
var width = $(this).attr("data-width");
var height = $(this).attr("data-height");
iframe.attr({
width: +width,
height: +height,
src: src
});
dialog.dialog("option", "title", title).dialog("open");
});
});

2 sliders with difference version of jQuery on one page

I have problem with 2 sliders on one page, because first use jQuery 1.9.1 and second 1.5.1, I found how to fix this problem, but it doesn't want to work.
I do it right that:
<script type="text/javascript" src="jquery/js/jquery-1.5.1.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.min.js" type="text/javascript"></script>
<script>var $j = jQuery.noConflict(true);</script>
<script>
$(document).ready(function(){
console.log($().jquery); // This prints v1.5.1
console.log($j().jquery); // This prints v1.9.1
});
</script>
<script src="js/jquery.slides.min.js" type="text/javascript"></script>
<script type="text/javascript">
$j(function() {
$j('#slides').slidesjs({
width: 950,
height: 364,
navigation: {
effect: "fade",
active: false
},
pagination: {
active: false,
effect: "fade"
},
effect: {
fade: {
speed: 1500
}
},
play: {
effect: "fade",
auto: true,
pauseOnHover: true
}
});
});
</script>
<link type="text/css" href="jquery/css/blitzer/jquery-ui-1.8.11.custom.css" rel="stylesheet" />
<script type="text/javascript" src="jquery/js/jquery-ui-1.8.11.custom.min.js"></script>
<script type="text/javascript" src="scripts/jquery.nivo.slider.pack.js"></script>
<div id="main">
<script type="text/javascript">
$(function() {
$( "#dateFrom" ).datepicker({ dateFormat: 'dd.mm.yy',minDate: +0, maxDate: '+8M +10D',monthNames: ['Styczeń','Luty','Marzec','Kwiecień','Maj','Czerwiec','Lipiec','Sierpień','Wrzesień','Październik','Listopad','Grudzień'],dayNamesMin: [ 'Nd','Po', 'Wt', 'Śr', 'Cz', 'Pt', 'So']});
});
$(function() {
$( "#dateToo" ).datepicker({ dateFormat: 'dd.mm.yy',minDate: +1, maxDate: '+8M +10D',monthNames: ['Styczeń','Luty','Marzec','Kwiecień','Maj','Czerwiec','Lipiec','Sierpień','Wrzesień','Październik','Listopad','Grudzień'],dayNamesMin: [ 'Nd','Po', 'Wt', 'Śr', 'Cz', 'Pt', 'So']});
});
$(window).load(function() {
$('#slider').nivoSlider();
});
</script>
Anyone knows why it doesn't work?
When I tried alert with version of jQuery it shows property, but when I use it in sliders, it doesn't
Use <script>jQuery.noConflict();</script> above one of your jquery and replace the $ with jQuery
Like
<script type="text/javascript" src="jquery/js/jquery-1.5.1.min.js"></script>
<script>jQuery.noConflict();</script>
<script src="http://code.jquery.com/jquery-1.9.1.min.js" type="text/javascript"></script> //Now replace this jquery's $ with jQuery

Dynamic Div content in Jquery Dialog

I am trying to display the Dialog with dynamic div content, inside the div i am trying to add span,textbox and button but the Jquery Dialog is not displaying.Help me on this issue
Thanks in advance for your answers
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Jquery Dynamic Div</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
<script src="JQUERY/jquery-ui.js"></script>
<style type="text/css">
.ui-datepicker
{
width: 17.5em;
height: 16.6em;
font-size:12.5px;
margin-left: 120px;
z-index: 1000;
}
</style>
<script type="text/javascript">
$(document).ready(function ()
{
$("#btnSubmit").click(function ()
{
alert("inside");
var DynamicDiv = $('<div id="ExpiryDatePopup" style="display:block;width:680px;height:327px;background-color:#FFFFFF;></div>');
alert("Div Created");
var breakTag1 = $('</br>');
alert("Div Created");
var breakTag2 = $('</br>');
var ExpDateText = $('<span style="color: steelblue;margin-left: 55px;">Please enter the Expiration Date </span>');
var EndDateText = $('<span style="margin-left: 56px;position: relative;">End Date</span><span style="color: #f00">*</span>');
var DatePickerText = $('<input type="text" id="datepicker" readonly="true" style="margin-left:32px" style="margin-left: 59px;margin-top: 29px;position: static;top: 85px;"/>');
var SubMitButton = $('<input type="button" id="popUpSubmit" value="Submit" style="margin-left: 128px;margin-top: 78px;height: 28px;width: 82px;" />');
alert("Div Created");
$('body').append(DynamicDiv);
$('#ExpiryDatePopup').append(breakTag1);
$('#ExpiryDatePopup').append(breakTag2);
$('#ExpiryDatePopup').append(breakTag1);
$('#ExpiryDatePopup').append(ExpDateText);
$('#ExpiryDatePopup').append(DatePickerText);
$('#ExpiryDatePopup').append(SubMitButton);
alert("hi sam");
$('#ExpiryDatePopup').dialog(
{
height: 400,
width: 600,
title: 'Expiration Date',
position: 'center',
background: 'red',
opacity: '0.3',
modal: true,
open: function ()
{
$("#datepicker").datepicker({ dateFormat: 'yy-mm-dd',
minDate: dateToday,
showOn: 'button',
buttonText: 'Show Date',
buttonImageOnly: true,
buttonImage: 'Images_/icon_calendar.jpg'
});
}
});
});
});
</script>
</head>
<body>
<input type="button" id="btnSubmit" value="Submit"/>
</body>
</html>
well i have tried your code and its working fine for me.
only problem is creating <div id="ExpiryDatePopup"></div>
You forget to close ExpiryDatePopup div style attribute.
Tested Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Jquery Dynamic Div</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
<style type="text/css">
.ui-datepicker
{
width: 17.5em;
height: 16.6em;
font-size:12.5px;
margin-left: 120px;
z-index: 1000;
}
</style>
<script type="text/javascript">
$(document).ready(function ()
{
$("#btnSubmit").click(function ()
{
var DynamicDiv = $('<div id="ExpiryDatePopup" style="display:block;width:680px;height:327px;background-color:#FFFFFF;" /></div>');
var breakTag1 = $('</br>');
var breakTag2 = $('</br>');
var ExpDateText = $('<span style="color: steelblue;margin-left: 55px;">Please enter the Expiration Date </span>');
var EndDateText = $('<span style="margin-left: 56px;position: relative;">End Date</span><span style="color: #f00">*</span>');
var DatePickerText = $('<input type="text" id="datepicker" readonly="true" style="margin-left:32px" style="margin-left: 59px;margin-top: 29px;position: static;top: 85px;"/>');
var SubMitButton = $('<input type="button" id="popUpSubmit" value="Submit" style="margin-left: 128px;margin-top: 78px;height: 28px;width: 82px;" />');
$('body').append(DynamicDiv);
$('#ExpiryDatePopup').append(breakTag1);
$('#ExpiryDatePopup').append(breakTag2);
$('#ExpiryDatePopup').append(breakTag1);
$('#ExpiryDatePopup').append(ExpDateText);
$('#ExpiryDatePopup').append(DatePickerText);
$('#ExpiryDatePopup').append(SubMitButton);
$('#ExpiryDatePopup').dialog(
{
height: 400,
width: 600,
title: 'Expiration Date',
position: 'center',
background: 'red',
opacity: '0.3',
modal: true,
open: function ()
{
$("#datepicker").datepicker({ dateFormat: 'yy-mm-dd',
minDate: dateToday,
showOn: 'button',
buttonText: 'Show Date',
buttonImageOnly: true,
buttonImage: 'Images_/icon_calendar.jpg'
});
}
});
});
});
</script>
</head>
<body>
<input type="button" id="btnSubmit" value="Submit"/>
</body>
</html>
You also have error in creating div you forget to close style attribute.
$('#ExpiryDatePopup').live('click',function(){
//write append code here
});

JQuery UI Dialog displaying, not working as dialog

Problem:
The dialog div is displaying without the buttons being pressed, and does not appear as an overlay when I press them. I'm tearing my hair out, so thanks in advance.
Code:
Includes:
<script src="js/jquery-1.5.1.min.js"></script>
<script src="js/ui/jquery.ui.core.js"></script>
<script src="js/ui/jquery.ui.widget.js"></script>
<script src="js/ui/jquery.ui.position.js"></script>
<script src="js/ui/jquery.ui.autocomplete.js"></script>
<script src="js/ui/jquery.ui.dialog.js"></script>
Css:
<link rel="stylesheet" type="text/css" href="css/jquery.ui.autocomplete.
<link rel="stylesheet" type="text/css" href="css/jquery.ui.all.
<link rel="stylesheet" type="text/css" href="css/jquery.ui.theme.
<link rel="stylesheet" type="text/css" href="css/jquery.ui.dialog.css" />
Buttons: <a class="phoneadder">Stuff</a>
Scripts:
<script>
$( "#dialog-form" ).dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true
}
);
$( ".phoneadder" ).click(function() {
$( "#dialog-form" ).dialog( "open" );
return false;
});
</script>
Dialog:
<div id="dialog-form" title="Create new phone">
<p>All form fields are required.</p>
<form>
<fieldset>
...some html here
</fieldset>
</form>
</div>
Place the initializer in your document.ready, or as shorthand:
$(function() { $("#dialog-form").dialog(autoOpen... ); } );
Alternatively, make sure your scripts are run after the div is created, so like, in the footer.
Try putting your jQuery code in the $(document).ready function like this:
$(document).ready(function () {
/// your code here
});
Try this,
$(function()
{
$( ".phoneadder" ).click(function() {
$( "#dialog-form" ).dialog({
height: xxx,
width: xxx,
modal: true
});
return false;
});
});
Why don't you just put the dialog function in the click event handler?
<script>
$(function()
{
$( ".phoneadder" ).click(function() {
$( "#dialog-form" ).dialog({
height: 300,
width: 350,
modal: true
});
return false;
});
});
</script>

Categories