Clicking on a dynamically created link not recognizing click event - php

When you click on a folder a button/link is prepended to the nav section but when i click on the added button it will not work. It does not recognize my click event for some reason..?
quick video of what i am dealing with. https://www.youtube.com/watch?v=NsW1uLbRd9w
I am clueless on why it is not working ahhhh
Any help would be awesome, thanks.
If you need more code i can send it if needed.
<script>
$(function () {
// When folder is clicked open it
$('button.<?php echo $row->folderName; ?>').bind('click', function() {
$('.open.<?php echo $row->folderName;?>').show();
// if folder is open then Prepend homebtn button to nav
var k = $('.open').css('display');
if(k == 'block') {
$('.nav').prepend('<a class="gohome">HELLO</a>');
}
});
$('.gohome').live('click', function() {
$('.open.<?php echo $row->folderName; ?>').hide();
});
});
</script>

To bind to future elements you need to bind the event to a parent that exists at the time that the procedure is executed. As your link as being added to .nav, that's what we can use. I am assuming .nav. exists when this code is run. If not, use $(document).on('click'...
$('.nav').on('click', '.gohome', function() {
$('.open.<?php echo $row->folderName; ?>').hide();
});

Related

How to reload a parent page on closing a overlay lightbox (wordpress)?

I need to refresh a parent page when closing a overlay lightbox (not popup window).
The lightbox has no close button, it's closed by clicking anywhere outside the lightbox.
I tried the following code, but it doesn't refresh the parent page.
Would you plase let me know how to refresh the parent page?
HTML:
<div class="dwqa-ask-question">
<a href="https://www.myweb.com/ask-questions/" rel="lightbox" data-lightbox-type="iframe">Ask Question
</a>
</div>
Lightbox:
<div class="nivo-lightbox-overlay nivo-lightbox-theme-default nivo-lightbox-effect-fade nivo-lightbox-open">
<div class="nivo-lightbox-wrap">
<div class="nivo-lightbox-content">
<iframe src="https://www.mycom.com/ask-questions/" class="nivo-lightbox-item" frameborder="0" vspace="0" hspace="0" scrolling="auto"></iframe>
</div>
<div class="nivo-lightbox-title-wrap"></div>
</div>
Previous
Next
Close
</div>
Added the following code in a lightbox page (=ask-questions page):
$(document).ready(function () {
if (window.opener) {
window.close();
}
if (window != top) {
top.location.replace(document.location);
}
});
jQuery(document).ready(function refreshParentWindow(){
window.opener.location.href="/{!$CurrentPage.parameters.id}";
window.top.close();
});
jQuery(document).ready(function (){
var rel = window.opener.location;
window.close();
rel.reload();
});
Thank you.
"Responsive Lightbox" is the plugin i'm using for the lightbox.
There are multiple ways to set this up. I personally always use additional css classes to show and/or hide my html elements on the page, but since you're currently using a plugin, then you could add an event listener to your page.
So use this javascript on your page:
jQuery(document).ready($ => {
console.log("You just loaded your js file onto the page"); // Testing whether you've loaded your js file correctly!
$(document).on("click", e => {
if (
document.querySelector('.nivo-lightbox-overlay').contains(e.target)
&&
!document.querySelector(".nivo-lightbox-wrap").contains(e.target)
) {
console.log("You just clicked outside of the lightbox wrap"); // Testing event listener
location.reload(); // a simple refresh
// OR
// location.reload(true); // a hard refresh
};
});
});
You see the orange area around your light-box wrap with nivo-lightbox-overlay class? Click anywhere on it an it'll reload your page!
Let me know if you were able to get it to work!
Another solution
jQuery(document).ready( $ => {
$(document).on("click", "div.nivo-lightbox-overlay", () => {
if ($("div.nivo-lightbox-overlay").length) {
location.reload(); // a simple refresh
// OR
// location.reload(true); // a hard refresh
}
})
});

Fancybox 3 on page load with delay and set cookie not working

I can not get this jQuery to work on page load? I use fancybox 3.
<script>
function openFancybox() {
setTimeout( function() {
$('[data-fancybox data-src="#newsletterFancy"]').trigger('click');
}, 20000);
}
$(document).ready(function() {
var visited = $.cookie('visited');
if (visited == 'yes') {
return false;
} else {
openFancybox();
}
$.cookie('visited', 'yes', { expires: 7 });
$('[data-fancybox data-src="#newsletterFancy"]').fancybox();
});
</script>
I have also added this to my body tag: <body OnLoad="openFancybox()" class="body">
I basically have my pop up in a included file called newsletter.php. The link in my sidebar works fine when i click that. But i want to make it pop up and open on page load with a delay and also for it to set a cookie.
I have also included the cookie js as well:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
This is the line I use in my sidebar link for it to open when you click it:
<a class="buttons" data-fancybox data-src="#newsletterFancy" href="javascript:;">Newsletter Subscribe</a>
Thanks
You are simply not using valid selector. Replace
$('[data-fancybox data-src="#newsletterFancy"]')
with, for example:
$('[data-src="#newsletterFancy"]')

jquery validation and changing of div content

I am having name, email in content of a html page. i have to validate it on clicking continue button. and also the content in the content div has to change. how can i do both on clicking the continue button. help me with some suggestions. thanks.
am using the following code for changing div content.
<script>
$(document).ready(function () {
$("#button").click(function () {
$("#content").load("<?php echo base_url().'survey/categories' ?>");
});
});
$(document).ready(function () {
$("#button1").click(function () {
$("#content").load("<?php echo base_url().'survey/budget_overview' ?>");
});
});
</script>
As i understand your question, i think what you want is to do the two tasks at the same time on click of the button. So here's some sample code assuming this is what you want. If you have any doubts just ask. And i have added just sample Email Validation Using RegExp in JavaSript at the end of the code as a function.. Get the idea, Hope this helped you,,
//
$(document).ready(function() {
//Button1 Click
$("button").click(function(){
//initialize inputs here
var email =$("#email").val;
var input1=$("#input1").val;
//Validate Inputs Here
if(IsEmail(email)==true && input1!=""){
return true
}
else{
return false;
}
//Loading the first content after validating inputs
$("#content").load("<?php echo base_url().'survey/categories' ?>");
//To Trigger the other button
$('#button1').trigger('click');
});
//Button1 Trigger Will fire Click event
$("#button1").click(function () {
$("#content").load("<?php echo base_url().'survey/budget_overview' ?>");
});
});
//Email Validation In JavaScript
function IsEmail(email) {
var regex = /^([a-zA-Z0-9_\.\-\+])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
}
I think the problem is with your syntax. You have to specify a file URL in the load method like this:
$(document).ready(function(){
$("form").submit(function(){
$("#content").load("myfile.php");
return false;
});
});
You can have a look on the load documentation.

Ajax Load Into <div> with child window scrolls to top of webpage

Here is my dilemma, I am calling my external pages via ajax into a <div>, within the <div> I have links that callback to the parent window which triggers the ajax load event for the next page. My problem is the call back brings the website to the top of the page. Here is the code;
Parent Window:
function Display_Load() {
$("#loading").fadeIn(900,0);
$("#loading").html("<img src='<?php echo $reg->get ('rel_addr'); ?>img/load.gif' height='50' width='50' />"); return false;
}
function Hide_Load() {
$('#midback').fadeIn('slow');
$("#loading").fadeOut('slow');
}
function loadContent(page) {
var param = "";
if (page) { param = page; } else { param='home'; }
Display_Load();
$('#midback').fadeOut('slow', function() {
$(this).load("<?php echo $reg->get ('rel_addr'); ?>"+param+".php",
Hide_Load()); return false;
});
Child Window Code:
<span id="events" class="more">more events...</span>
<script type="text/javascript">
$(document).ready(function() {
$('.more').click(function() {
var params = $(this).attr("id");
window.parent.loadContent(params);
}); return false;
});
I would also like to animate the height of the <div> (close it then reopen it to correct height of new page) as well, first I would like to get this out of the way however.
Try changing your click on the child to:
$('.more').click(function(e) {
e.preventDefault();
// . . .
return false;
});
See http://api.jquery.com/event.preventDefault/
The problem is when you click on the more class, you are likely appending a # to the end of your URL. This causes the page to jump up to the top. Per the documentation, the preventDefault will stop this from happening. For good measure, also return false from the click function.

Call prototype(javascript) function on link click?

I am working with AJAX with prototype and PHP. It is working for me but I need some small changes. Following is my running code for AJAX request:
JS/Prototype:
function ajaxRequest(url) {
new Ajax.Request( url, {
method: 'get',
onSuccess: function( transport ) {
// get json response
var json = transport.responseText.evalJSON( true );
alert(json);
},
onFailure: function() {
alert('Error with AJAX request.');
}
});
return false;
}
HTML:
<a href='javascript:ajaxRequest("/testajax/ajaxresponse");'>Testing AJAX</a>
Question:
Now I want to change my link like this:
<a href='/testajax/ajaxresponse' class='AjaxLink'>Testing AJAX</a>
So prototype function should capture click event of class='AjaxLink' links and then get href part of clicked link and proceed. How can I change my above prototype function for such kind of links.
Thanks
If you have Prototype 1.7 then this way is available:
document.on('click', 'a.AjaxLink', ajaxRequest.curry('/testajax/ajaxresponse'));
Otherwise you'll have to rely on good old Event.observe:
$$('a.AjaxLink').invoke('observe', 'click',
ajaxRequest.curry('/testajax/ajaxresponse'));
Just re-read the question and I see you want to use the href attribute. Jan Pfiefer was very close.
document.on('click', 'a.AjaxLink', function(event, element) {
return ajaxRequest(element.href);
});
This wont work. Why do you want such a link? If a link is specified in this way any click on it will follow its href and change location of actual document. Only way to prevent such a behavior then is by adding onclick again or in $(document).ready bind onclick handler, and manualy cancel the event.
UPDATE
However to bind onclick event on all links with AjaxLink class,execute request and cancel the event:
$(document).ready(function(){
$('.AjaxLink').click(
function(e){
ajaxRequest(this.href);
event.preventDefault ? event.preventDefault() : event.returnValue = false;
}
);
});
This will work:
$$('a.AjaxLink').each(function(element) {
element.observe('click', function(e) {
var element = e.element()
Event.stop(e)
alert(element.href)
});
})

Categories