I'm loading a page inside a magnific popup vie ajax:
$("#operator").magnificPopup({
delegate: 'a.edit',
mainClass: 'mfp-fade',
closeBtnInside: true,
removalDelay: 300,
closeOnContentClick: false,
type: 'ajax',
ajax: {
settings: {
url: 'index.php?p=staff/operators',
}
},
callbacks: {
elementParse: function() {
this.st.ajax.settings.data = {
operator_id: this.st.el.attr('data-id')
}
}
}
});
Inside the modal window I want to fire a trigger event on a input type="file":
$('input[name^=\'upload\']').on('change', function() {
$('#form-upload').remove();
$('#staff').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>');
$('#form-upload input[name=\'file\']').trigger('click');
if ($('#form-upload input[name=\'file\']').val() != '') {
$.ajax({
/* upload file code */
});
ecc...
});
But it doesn't trigger. Also, all other trigger events aren't working inside the magnific popup.
So how can I get trigger events working inside the magnific popup?
As the HTML is loaded by ajax, by the tiem you try to attach the event, the element is not available, so nothing is attached.
You have to change you '.on()' call, so that it selects an element which contains the popup element, and use a filter so that only the change by the name=upload is handled. Something like this:
$('body').on('change','input[name^=\'upload\']', function() { ...
By the way, to avoid escaping characters in javascript, you can exchange single and double quotes. So you can simply use this filter:
'input[name^="upload"]'
or
"input[name^='upload']"
(Please, do test them. I think only the first one is valid).
Related
I use ajaxsumit to upload file to linux server successfully.
When uploading file was done, the file name and size would appear in my website with a cancelfile button.
But when mouse over the file name, the cancelfile button can not appear. I don't know why ? who can help me ?
$(document).ready(function(){
$('#filename').mouseover(function(){
$('#cnlbtn').css("display","block");
});
$('#filename').mouseout(function(){
$('#cnlbtn').css("display","none");
});
});
$("#fileupload").change(function(){
$("#myupload").ajaxSubmit({
dataType: 'json',
beforeSend: function() {},
uploadProgress: function() {},
success: function(data) {
files.html("<br />"+"<b id='filename'>"+data.name+"("+data.size+"k)</b> <span class='delimg' rel='"+data.pic+"' id='cnlbtn'>cancelfile</span>");
btn.html("addfile");
},
error:function(xhr){}
});
My delimg style:
.delimg{margin-left:20px; color:#090; cursor:pointer;display:none}
Is not working because when you add the event listener the tag does not exist yet in the DOM. As Joe B. said you need to use event delegation: jQuery Doc Direct and delegated events
So you have to do something like this:
$(document.body).on('mouseover', '#filename', function(){
$('#cnlbtn').css("display","block");
});
$(document.body).on('mouseout', '#filename', function(){
$('#cnlbtn').css("display","none");
});
Im working on a website which has a popup for a product information. Inside the popup is a link which opens a second popup for the shipping information.
What we want is to replace the popup content when the second link is clicked.
Is this possible?
This is what I have been trying so far (no luck)..
// first popup
$('.pop-ajax').magnificPopup({
type: 'ajax',
closeBtnInside: true, // put close button on inside
callbacks: {
parseAjax: function(mfpResponse) {
mfpResponse.data = $(mfpResponse.data).find('.l-main');
},
ajaxContentAdded: function() {
// console.log('it works');
}
}
});
// Second popup (loads first popup when a link inside is clicked)
$('.pop-product').magnificPopup({
type: 'ajax',
closeBtnInside: true, // put close button on inside
callbacks: {
parseAjax: function(mfpResponse) {
mfpResponse.data = $(mfpResponse.data).find('#product-ajax');
},
ajaxContentAdded: function() {
console.log('First popup');
// var mfp = $.magnificPopup.instance;
// mfpContentContainer = mfp.contentContainer;
// mfpContent = mfp.content;
$('.pop-ajax').click(function(){
$('.pop-product').magnificPopup('close');
$('.pop-ajax').magnificPopup({
type: 'ajax',
closeBtnInside: true, // put close button on inside
callbacks: {
parseAjax: function(mfpResponse) {
mfpResponse.data = $(mfpResponse.data).find('.l-main');
},
ajaxContentAdded: function() {
// console.log('it works');
}
}
});
});
}
}
});
The second popup is a large content area on the site. One of the links inside this area has a link with the .pop-ajax class. However we can't seem to update or replace the popup content with the new content.
Can anyone help shed light on what I am doing wrong here?
Are you trying to create nested popups then according to this below link, it is not allowed.
Issues for Simultaneous Popups
But if you want to open just one popup,in which after the click on the inner link just replace the content of the pop up. Then refer to the following link.
Nested Popups
$('.first-popup-link, .second-popup-link').magnificPopup({ closeBtnInside:true});
In the above code you can find that for two different links same magnificPopup instance was created.
I am fairly new to js and jquery. I basically want to be able to change the table and id variables from an onClick event or something appended to the <a> this way I can modify the variables with php later on. This is just proof of concept, and doesnt seem to be working since I made modifications. Is there a way that I can pass variables from the a to the function?
OVERALL GOAL: I want to have an inline onclick that will pass id and table names from loadMe to the function and display table_render.php?id=someid&table=sometable in the dialog box.
<script>
$(function loadMe(table, id) {
$( "#dialog-view" ).dialog({
autoOpen: false,
height: 600,
width: 700,
modal: true,
buttons: {
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
$( "#create-view" ).click(function() {
$( "#dialog-view" ).load("table_render.php?id=" + id + "&table=" + table + "").dialog("open");
});
});
</script>
Some text
<div id="dialog-view" title="">
</div>
You are hooking up the event twice, once via onclick and once via jQuery. You need to pick one.
If you pick the jQuery way (recommended) you're going to get the jQuery arguments (which is just one arg, event). However, jQuery will set this to the element that triggered the event (in this case, your A tag). You can use that to get data off of the A tag.
For instance, if you wanted the ID of the A that got clicked, you could do this in your handler:
var clickedId = $(this).attr('id');
If you want to store some arbitrary info (eg. "tableName") for each A tag, you can either use the HTML 5 data attributes (preferable), or just make up your own attributes (which will work, but is "bad form"). For instance:
<a tableName='testimonials'>
var clickedFoo = $(this).attr('tableName');
or (a little better):
<a data-tableName='5'>
var clickedTableName = $(this).attr('data-tableName');
// Also, I believe this would work:
var clickedTableName = $(this).data('tableName');
* EDIT *
Just to try and clarify a little further, the basic overall idea is this:
1) You write out your A tags to the page, via PHP
1A) As you write them out, you put whatever data is specific to them on the A tag, in the form of an attribute or attributes (eg. id='foo' data-bar='baz').
2) You also write out some Javascript code that says "hey, whenever an A tag gets clicked, run this function"
3) Inside the function that you hooked up to the A tag's click event, you use the this variable (which points to the A tag itself) to get the data (the "variables") that you need
3A) For instance, you could use the JQuery "attr" method: $(this).attr('id')
4) Profit! (or at least do something useful with the data you just got)
so i got it working... heres the solution
function createViewer(id, table) {
var id;
var table;
$("#dialog-view").dialog({
autoOpen: false,
width:650,
minHeight:400,
show:{effect: "fade", duration: 500},
hide:"clip",
resizable: false,
modal: true,
buttons: {
"Close": function () {
$(this).dialog("close");
}
}
});
$("#dialog-view").load("table_render.php?id=" + id + "&table=" + table + "").dialog("open");
};
And the inline code
View Quote
you might want to put $(document).ready()around you functions initialising the dialog and binding the onclick event. Then open the dialog inside the callback of your ajax request.
That way you could dismiss the onclick attribute with the loadFunction, I guess. (tired)
something like:
HTML:
<a href="#" id="trigger" data-my-id="123" data-my-table="myTable">
trigger
</a>
<div id="dialog">
Dialog Contents....
</div>
JS:
$(document).ready(function() {
$("#dialog").dialog({
autoOpen: false,
height: 600,
width: 700,
modal: true,
buttons: {
Cancel: function() {
$(this).dialog("close");
}
}
});
$("#trigger").click(function() {
var id = parseInt( $(this).attr('data-my-id');
var table = $(this).attr('data-my-table');
$("#dialog").load("table_render.php?id=" + id + "&table=" + table + "",
function(){
$("#dialog").dialog("open");
});
});
});
should do the trick.
I have two problems
I am trying to open a jQuery colorbox and it is very slow. The reason is I am trying to get html content from a different page (I cannot use iframe because I just need a part of this page). The following code works but it takes time after the button is clicked:
$(document).ready(function() {
$(".cart-link a").click(function(event) {
$(this).colorbox.close();
});
$(".rest-menuitem a").click(function(event) {
event.preventDefault();
var result = null;
var sURL = $(this).attr("href");
$.colorbox({
html: function() {
$.ajax({
url: sURL,
type: 'get',
dataType: 'html',
async: false,
success: function(data) {
result = data;
}
});
return $(result).find('.product');
},
width: '650px',
height: '10px',
onComplete: function() {
$(this).colorbox.resize();
}
});
});
});
I want to know if there is a alternative way to do it. I dont mind if the colorbox popup and then takes time to load the content. The above version can be fount at this url (http://delivery3.water-7.com/index.php/restaurants/manufacturers/3/Barcelona-Restaurant-&-Winebar/products).
I am also trying to close the colorbox when a user clicks on add to cart. But some reason it is not triggered. $(".cart-link a").click is not triggered when I click on add to cart. Is there a special way to add jquery to colorbox content?
Try this instead:
$(".rest-menuitem a").colorbox({
href: function(){
return $(this).attr('href') + ' .products';
},
width: '650px',
height: '10px',
onComplete: function() {
$(this).colorbox.resize();
}
});
ColorBox uses jQuery's load() method for it's ajax handling, so you just need to add the desired selector to the link's href.
For your question 2 can you try this ?
$(document).ready(function() {
$(".cart-link a").live('click',function(event) {
$(this).colorbox.close();
});
});
For your question 1..it will be slow since you are fetching it from different page.Use a different logic for that
For your question no 1
$('selector').colorbox({onLoad: function() { /*Intially load a empty color box with only <div id="contenttoload"></div> (No other html content */
$.ajax({
url :'Your url',
data : {}, //data to send if any
type : "POST" //or get
success:function(data){ /*data means the stuff you want to show in color box which you must return from the other page*/
$('#contenttoload').html(data); //data should be well formatted i mean add your css,classes etc from the server itself */
}
});
}});
I have a very simple scenario where I want to POST the form using JQuery's ajax() method but perform request some confirmation from the user before actually performing the post.
I have a JQuery UI dialog with localized buttons in place in case you wonder what all the code with buttons below is about.
This is my code so far:
var i18n_deleteButtons = {};
i18n_deleteButtons[i18n.dialogs_continue] = function () {
return true;
$(this).dialog('close');
};
i18n_deleteButtons[i18n.dialogs_cancel] = function () {
return false;
$(this).dialog('close');
};
$('#delete-dialog').dialog({
open: function () {
$(this).parents('.ui-dialog-buttonpane button:eq(1)').focus();
},
autoOpen: false,
width: 400,
resizable: false,
modal: true,
buttons: i18n_deleteButtons
});
$("form#form_attachments").submit(function (event) {
/* stop form from submitting normally */
event.preventDefault();
/* get some values from elements on the page: */
var $form = $(this), url = $form.attr('action');
// build array of IDs to delete (from checked rows)
var jdata = { 'attachmentIdsToDelete': [] };
$('input:checked').each(function () {
jdata['attachmentIdsToDelete'].push($(this).val());
})
$.ajax({
beforeSend: function (request) {
// Return false if you don't want the form submit.
$('#delete-dialog').dialog('open');
},
url: url,
type: "POST",
dataType: "json",
data: jdata,
traditional: true,
success: function (msg) {
$('#msg').html(msg);
}
});
});
The dialog actually opens up fine but clicking at any of the buttons results in nothing happening. The post doesn't happen and the dialog does not close regardless of which button was clicked.
How can I make this work?
Why not move the actual ajax call from the submit handler and trigger it when pushing a button in the delete dialog?
You could store you ajax call in a separat function and pass this functions and the url as parameters to the confirmation routine.
[pseudo code]
function callAjax() {
...
}
function submitHandler() {
confirmDialog({callback:callAjax,param:you_url});
}
function confirmDialog(obj) {
$('#yes_btn').click(function() {
// call the callback
obj.callback(obj.param);
});
$('#no_btn').click(function() {
// just close the dialog
});
}
This way the confirmDialog don't have to know anything about the ajax call, it will just execute the callback with the given parameter when the user clicks ok.
Because the default jquery UI dialog is a bit cumbersome for regular use and trouble to configure in some custom scenarios I looked around and found this Easy Confirm plugin which is based upon jquery&jquery UI default stuff. It also allows for very simple internationalization.
https://github.com/wiggin/jQuery-Easy-Confirm-Dialog-plugin