I am making a website in which there are around 20 modals (bootstrap) on a single page. Each modal contains an iframe which plays a youtube video.
Till now, all I did was created all 20 modals with div #myModal1, #myModal2 ........... #myModal20.
Then, I looked for a javascript code which can stop a youtube video when I close the modal and found the jquery code (shown below)
jQuery(".modal-backdrop, #myModal2 .close, #myModal2 .btn").on("click", function() {
jQuery("#myModal2 iframe").attr("src", jQuery("#myModal2 iframe").attr("src"));
});
The above code stops the youtube video when I close the modal with div #myModal2.
Now, since there are 20 modals and I wrote the same code for all of them just changing #myModal3, #myModal4,...........and so on.
Can I do the same using for loop (I am new to this javascript) but I tried something and it is not working.
The code I wrote was:
for(i=1; i<21; i++) {
var modal = "#myModal"+i;
jQuery(".modal-backdrop, " + modal +" .close, " + modal +".btn").on("click", function() {
jQuery(modal+" iframe").attr("src", jQuery(modal+" iframe").attr("src"));
});
}
It is not working.
Essentially you do not need a for loop for this, just bind a click handler to [id*='myModal'] which are all the elements having id starting with myModal
jQuery("[id*='myModal'] .close, [id*='myModal'] .btn").on("click", function() {
var that = this;
jQuery(that).find("iframe").attr("src", jQuery(that).find("iframe").attr("src"));
});
And now clicking on the backdrop should stop all the videos
jQuery(".modal-backdrop").on("click", function() {
jQuery([id*='myModal']).find("iframe").attr("src", jQuery([id*='myModal']).find("iframe").attr("src"));
});
Why your code is not working because the value of i is constantly changing and is not preserved per click handler.
To make it work you should use IIFE.
I changed my code a little bit by defining immediately-invoked function expression IIFE.
I had the jquery to stop the youtube video on clicking close button and just needed to input a variable which will work for all the modals so I added [id^='myModal] to the input that is i which the function takes to execute the code.
(function(i) {
//Defining a variable "modal" equating it to "[id^='myModal']"
//'i' will be any modal having id starting with **myModal**
var modal = i;
//Jquery to stop the youtube video
jQuery(".modal-backdrop, " + modal +" .close, " + modal +".btn").on("click", function() {
jQuery(modal+" iframe").attr("src", jQuery(modal+" iframe").attr("src"));
});
}
("[id^='myModal']"));
Related
I created a button that will take in a website url and then display stars to collect a website rating. That part works fine. When trying to click on the stars to highlight the clicked and all prev (prevAll) stars and change the source to staron.gif but nothing happens on click. I want to use the delegate function to do this, and then a function. My function is not working.
$('#websiteButton').mousedown(function() {
var inputSite = prompt("Enter one of your favorite website urls");
var appendHTML = "<div class=\"webfav\"><a href='"+inputSite+"'>"+inputSite+"</a> <img id=\"s1\" src=\"staroff.gif\"/><img id=\"s2\" src=\"staroff.gif\"/><img id=\"s3\" src=\"staroff.gif\"/><img id=\"s4\" src=\"staroff.gif\"/><img id=\"s5\" src=\"staroff.gif\"/> <br></div>";
$('#sect2').append(appendHTML);
});//end of add favorites
$('.webfav').delegate('img', 'click', function() {
$(img, this).attr('src', 'staron.gif');
$(img, this).prevAll().attr('src', 'staron.gif');
});
I've also tried just this and this.id
I ended up just needing to move "});//end of add favorites" to the bottom after the delegate function executed. It was executing before the stars were added to the page the way I had it.
I have a gallery that opens with floatbox plugin. The problem is that i want to addclass to the popup div as the gallery opens. Before the popup opens the html is not hidden, its just not generated yet. I've tried few things, the last one is with timeout, but it doesn't work.
var delay = $(".gallery_floatbox");
function timeout(){
setTimeout(function() {
delay.addClass('asd');
}, 2000); }
$('.afd_gallery_first a').click(function(){
timeout(); });
NOTE: for other div this code works, but for the popup it doesn't.
You can try afterBoxStart callback like,
Add in your anchor tag HTML,
<a href="your-linkp" class="floatbox" data-fb-options="afterBoxStart:'myFunc();'">
talk about fruit
</a>
In Script,
var delay = $(".gallery_floatbox");
function myFunc() {
delay.addClass('asd');
return true;
}
I use an external header for a Multi-Page Template JQM 1.4
$('#pageprostoriheader').load('header.html', function () {
$.mobile.pageContainer.pagecontainer("getActivePage").enhanceWithin();
});
$('#pagetestheader').load('header.html', function () {
$.mobile.pageContainer.pagecontainer("getActivePage").enhanceWithin();
});
In the header I have a button with ID gumbiskanje, that opens a popup dialog.
$(document).on('click', '#gumbiskanje', function(e){
var niz = $('#niziskanje').val();
prikaziiskanje(niz);
window.location.href = "#pageiskanje";
});
when i had only one page worked all fine, but now that i have multiple page. It only works on the first page in html, but on other pages it doesn't start the dialog.
Update
Popup div should be either place internally as a child of page div, or externally outside page div.
If placed externally, you need to call the below function in order to enhance/create it. Then you will be able to call it from any page.
$(function () {
$("[data-role=popup]").popup();
});
To call it
$(document).on("click", "#gumbiskanje", function () {
$("#popupLogin").popup("open");
});
Demo
If you have the same popup in each page with the same id, you need to call it from within the active page too.
$(document).on("click", "#gumbiskanje", function () {
var active = $.mobile.pageContainer.pagecontainer("getActivePage");
$(active).find("#popupLogin").popup("open");
});
Demo
I'm using classed links to change FlowPlayer content. Here is a working version: http://jsfiddle.net/r9fAj/
In my actual page using the same code the first link clicked works fine. The second one does not fire the click function at all. Even if I comment out everything but the console.log()...
$('.playerLink').click( function() {
audioPlayer.unload();
initAudioPlayer();
$('#player').css('display', 'block');
$('#player').animate({"height":"50px"}, 1000);
var newClip = {'url':$(this).attr('ajax-data'),'autoplay':true};
audioPlayer.play(newClip);
console.log('playing ' + $(this).attr('ajax-data'));
});
HTML like so
Listen
Listen
<a id="flowplayer" href="/audio/episodes/09_27_2013_Happy_Hour_88509726.mp3"></a>
And the player initialized like so:
var audioPlayer;
var initAudioPlayer = function () {
$f("flowplayer", "/player/flowplayer-3.2.16.swf", {
plugins: {
controls: {
fullscreen: false,
autoHide: false,
}
},
clip: {
autoPlay: false,
url: "",
}
});
audioPlayer = $f();
};
initAudioPlayer();
Since the jsFiddle works over and over I assume something else in my page is preventing the second click() from working but the console has no errors for me.
So my question is, short of posting the whole site's code how do I pursue debugging this?
So it sounds like your .click() event handler is only being fired for the first link you click and not for additional clicks. For general debugging, you could take your page that is not working and gradually comment out / remove other part of the JS and HTML until you are able to make it work correctly. Or start with the minimal amount that is working (the fiddle) and gradually add in the rest to see when it stops working.
So this is the first site I have done where content is delivered via AJAX and internal links are caught by
$("a:not([href^='http://'])").click( function(e) {
var url = $(this).attr("href");
var title = ($(this).attr("title")) ? ': ' + $(this).attr("title") : '';
e.preventDefault();
if(url!=window.location){
window.history.pushState({path:url},title,url);
$('#contentMain').load(url);
document.title = "It's New Orleans" + title;
}
});
For some reason it does work once to click a link with the class but the second time gets preventDefault()ed.
Listen
The fix was adding [href^='#'] to not() e.g.
$("a:not([href^='http://'],[href^='#'])").click( function(e) {
Hi everybody,
I have some issue with one of my project. I'm currently developing a toolbar for Google Chrome. The concept is that my extension insert by using a content script an iframe in every page i visit. Materialized in Red on my Printscreen.
After that i've created another iframe who appear when i click on the "Menu" Button. This iframe appear like a dropMenu. Materialized in orange in the printscreen.
Well now let me explain my problem :
When i click on the "dropMenuButton" i execute this code :
$( "#dM1").click( function() {
dropMenu('dropMenu1', $(this).position().left);
});
To be clear the function "dropMenu" will call my background page (by messaging exchange) to show or hide the dropMenu, in function if it's allready activated or not.
Here is the executed code by the "dropMenu function"
if(document.getElementById("dropMenu"))
{
$("#dropMenu").slideUp(800, function() {
$(this).remove();
});
}
else
{
var body = $('body'),
MenuURL = chrome.extension.getURL(dropMenuPage + ".html"),
iframe = $('<iframe id="dropMenu" scrolling="no" src="'+MenuURL+'">');
body.append(iframe);
$("#dropMenu").hide().slideDown(800);
// Shift the menu (Left)
$("#dropMenu").css({left: marginLeft+'px'});
}
So the event on dropMenuButton work perfectly but i want to provide some ameliorations like a .ClickOut event. What i want is that when somebody click outside the dropMenu (in orange) the menu will be hide.
I've tried a lot of things but nothing work...
Hope somebody will provide me some help !
Thanks in advance.
Edit (Injection) :
I've tried to inject the javascript like this :
var injectJs = $('<script type=text/javascript>' +
'$(document).click(function() {' +
'dropMenu("dropMenu1", 0);' +
'});');
body.append(injectJs);
injectJs = $('$("#dropMenu").click( function(e) {' +
'e.stopPropagation();' +
'});' +
'</script>');
body.append(injectJs);
But it didn't seems to inject on the page. It should have a problem somewhere...
Can't you just add a click event on the document? Then on the actual drop down menu (or any other events where you don't want to hide the drop down) prevent any clicks from bubbling up:
$(document).click(function(){
// hide drop down clicking anywhere on page:
$("#dropMenu").slideUp(800, function() {
$(this).remove();
});
});
$("#dropMenu").click( function(e) {
e.stopPropagation(); // prevent click on drop menu from removing the drop down.
});
It works great but like this :
$(document).click(function(){
// hide drop down clicking anywhere on page:
dropMenu('slideUp', 0);
});
$("#dM1").click( function(e) {
e.stopPropagation(); // prevent click on drop menu from removing the drop down.
dropMenu('dropMenu1', $(this).position().left);
});
Now i have to insert the similar code on the global page, someone have an idea how i can insert dynamically a js code ?