I have a code that adds elements upon the click of a button.
$(document).ready(function(){
$("#add_txt").click(function(){
$("body").prepend('<input class="style_txt">');
});
});
//However when I add...
$( function() {
$( ".style_txt" ).draggable();
} );
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<button class="btn" id="add_txt">Add Text</button>
The element can not be dragged? Why is this?
you can't do it with input element, wrap it inside span
$(document).ready(function() {
$("#add_txt").click(function() {
$("body").prepend('<span><input class="style_txt"></span>');
$(".style_txt").parent().draggable();
});
});
span {
padding: 5px;
border: 1px solid #eee;
cursor: move;
}
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<button class="btn" id="add_txt">Add Text</button>
You could do it like this:
$(document).ready(function() {
$("#add_txt").click(function() {
$("body").prepend('<div class="style_text"><input /></div>');
$(".style_text").draggable({
start: function(event, ui) {
$(this).data("preventBehaviour", true);
}
});
$(".style_text :input")
.on("mousedown", function(e) {
var mdown = document.createEvent("MouseEvents");
mdown.initMouseEvent(
"mousedown",
true,
true,
window,
0,
e.screenX,
e.screenY,
e.clientX,
e.clientY,
true,
false,
false,
true,
0,
null
);
$(this).closest(".style_text")[0].dispatchEvent(mdown);
})
.on("click", function(e) {
var $draggable = $(this).closest(".style_text");
if ($draggable.data("preventBehaviour")) {
e.preventDefault();
$draggable.data("preventBehaviour", false);
}
});
});
});
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<button class="btn" id="add_txt">Add Text</button>
It's a rather complex answer, but it works. Note: I found this workaround in another answer.
Related
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>
im having trouble setting boundaries for my draggable i found out that i need to get rid of these
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/knockout/2.3.0/knockout-min.js"></script>
the boundaries will work
for my code to work the script above are necesary for the rest of my code if i get rid of them my code will not work at all. i need to find a way to get around this problem
$(function () {
$("div[id='dragx']").draggable({
containment: "#box",
scroll: false
});
});var z = 1; //value to make div overlappable
$('#addText').click(function (e) {
/** Make div draggable **/
$('<div />', {
class: 'ui-widget-content',
appendTo: '.container',
draggable: {
containment: 'parent',
start: function( event, ui ) {
$(this).css('z-index', ++z);
}
}
});
});
$(document).on("dblclick", '.text', function()
{
$(this).hide(); $(this).closest('.item').find('.edit_text').val($(this).text()).show();
});
$(document).on("click", ".edit_text", function()
{
return false;
});
$(document).on("click", function()
{
var editingText = $('.edit_text:visible');
if (editingText.length)
{
editingText.hide();
editingText.closest('.item').find('.text').text($(editingText).val()).show();
}
});
var count = 1;
var selectedDraggable;
ko.bindingHandlers.draggable={
init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
$(element).draggable();
$(element).addClass('item' + count);
count++;
$(element).on('click', function () {
selectedDraggable = $(this);
})
}
};
var vm=function(){
var self=this;
self.items=ko.observableArray();
self.textContent = ko.observable('');
self.init=function(){
self.items([]);
}
self.remove=function(item){
console.log(item);
self.items.remove(item);
}
self.addNew = function() {
self.items.push( self.textContent() );
self.textContent('');
}
self.init();
}
ko.applyBindings(new vm());
.item{
width: 200px;
height: 200px;
padding: 0.5em;
background:transparent;
z-index: 1;
cursor: pointer;
background-color: lightpink;
display:block;
}.container {background-color: lightgrey;
width: 500px;
height: 500px;
border: 2px solid;
position: relative;
}
<textarea data-bind="value: textContent" Placeholder="Type text to append" rows="4" cols="21"></textarea>
<button data-bind="click: addNew">Create</button><div id="box" class="container" style="float:left;">
<div data-bind="foreach:items" class="fix_backround">
<div id="dragx" href="#" class="item" data-bind="draggable:true,droppable:true">
<span data-bind="click:$parent.remove">[x]</span><br/><br/>
<center><span class="text" data-bind="text:$data"></span><input class="edit_text"/></center>
</div></div>
</div><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/knockout/2.3.0/knockout-min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet"
href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script type="text/javascript" src="http://www.pureexample.com/js/lib/jquery.ui.touch-punch.min.js"></script>
<script src="http://circletype.labwire.ca/js/circletype.js"></script><script src="http://tympanus.net/Development/Arctext/js/jquery.arctext.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
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>
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
});
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>