I am trying to have a popup only display when I click the .add-to-cart button using jQuery/javascript.
Let's say my popup is "#popup", and the button which will trigger the popup is ".add-to-cart", how do I go about this? I am fairly new to javascript, but I've managed to change a div's color and background the moment a user scrolls 200px, I tried using the same method to make it change properties by clicking without any luck.
The idea here was to change the property of the popup from "display:none" to "display:block" to use that in my problem here.
Also, inside the popup there is a button which when clicked, closes that popup again, we'll call it ".continue"
Is this the right way to do it, or is there other ways to display/close this popup?
I would post my code but it's not gonna help much in solving this as I believe this is simple for the experts on here. But here is the javascript i used for my other function that I mentioned which changes properties based on scroll. I suspect I can modify this to be used in this instance as well?
var mybutton = document.getElementById("backBtn");
var mytext = document.getElementById("tilbakeP");
var myarrow = document.getElementById("arrow");
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 200 || document.documentElement.scrollTop > 200) {
mybutton.style.position = "fixed";
mybutton.style.top = "20px";
mybutton.style.backgroundColor = "rgba(0,0,0,0.4)";
mytext.style.color = "#fff";
myarrow.style.borderColor = "#fff";
} else {
mybutton.style.position = "absolute";
mybutton.style.top = "";
mybutton.style.backgroundColor = "#fff";
mytext.style.color = "#333";
myarrow.style.borderColor = "#333";
}
}
I figured it out, if anyone comes across this, this worked by settings the popup to "display:none" in css, and using this code to make it display, by pressing add-to-cart CTA:
$('.button.el-button.cta.normal-btn.add-to-basket').click(function(){
$('.installation-popup').show();
});
Related
Hi I am adding a iframe dynamically, It displays an image from a server. I need to disable the context menu for this item. In chrome I can inspect element and if I add oncontextmenu="return false" I do get the wanted affect. However I am unable to do this while the page is generated. Here is an example of the working html.
However I can not reproduce this when i frame is being created. Here is my code.
$(window).scrollTop(0);
$('#secVerify').show();
$("#popWaitLoad").modal("hide");
imgLoading.hide();
dvIframe.empty();
//else load deposit data into interface
$("#spanType").text(deposit.DepositType);
$("#spanReference").text(deposit.Reference);
$("#spanAmount").text("R " + deposit.Amount.toFixed(2));
$("#spanDate").text(deposit.DateCreatedOffsetS);
imageID = deposit.Deposit_Doc_imageID;
var url = imageUrl + '/' + deposit.Deposit_Doc_imageID + '/false';
var imgFrame = document.createElement("iframe");
imgFrame.src = url;
imgFrame.frameBorder = '0';
imgFrame.scrolling = 'no';
imgFrame.width = '100%';
imgFrame.height = '100%';
imgFrame.align = 'middle';
imgFrame.id = "iframeImg";
dvIframe.append(imgFrame);
I have tried examples like.
$("#iframeImage").contents().find("img").attr("oncontextmenu", 'return false');
$('#iframeImage img').on('contextmenu', function (e) {
e.stopPropagation();
// Your code.
return false;
});
But because the img element seems to be only created is done after page load it seems to not work. I know disabling the the menu will not help much and I have explained all the other methods of obtaining the image that is still available but the client really wants this.
I have added nocontextmenu to the body tag and it works everywhere except for the iframe.
So let me clarify, My iframe is working like it should however I would like to disable the right click aka context menu on the specific iframe.
I have used setAttribute to set the attributes and targeted a container to appendChild.
function example(){
var target = document.getElementById('container');
var element = document.createElement('img');
element.setAttribute('src', 'http://gopalshenoy.files.wordpress.com/2011/04/product_demos.jpg');
//element.setAttribute('width','100%');
//element.setAttribute('height','100%');
element.setAttribute('id','iframeImage');
element.setAttribute("oncontextmenu","return false;");
target.appendChild(element);
}
// Demo-Snippet use.
window.onload=example;
<!-- Demo Snippet use -->
<div id="container"></div>
If you build more than one element using this function you might find further issues due to duplicated ID's.
ID's are used to target a specific element 'one of' so if you want to build multiple elements I would recommend giving them unique ID's.
I hope this helps. Happy coding!
So I am creating a new div using javascript and then I am trying to set the focus on the new div but it just isn't working. I have looked at this post Difficulty setting focus on newly created object in javascript but I still can't get it to work Here is my function:
function overlayDIV(clickedId) {
// see if div already exists if not create it
if (!document.getElementById("overlay" + clickedId)) {
var div = document.createElement("div");
div.id = "overlay" + clickedId;
div.style.width = "12px";
div.style.height = "12px";
div.style.background = "red";
div.style.position="absolute";
div.style.zIndex = "1";
//added this line after reading the other post,
div.setAttribute('tabindex', '0');
document.getElementById(clickedId).appendChild(div);
}
document.getElementById("overlay" + clickedId).focus();
}
I have no idea what Im doing wrong if anyone can help that would be awesome thanks.
EDIT:
So I made a JSfiddle of it: http://jsfiddle.net/m7cQA/ Now what I want to be able to do is let the user click on the div and have a new div be created (this is happening already) and the user should be able to just start typing in the newly created div (this is the part that isn't working) when you start typing the text goes in the parent div behind the new div. You have to re-click on the divs to get the cursor to move to the new div in order to start typing in it.
Just like the question says, I'm trying to clear a form from a modal window while the modal stays up. I've tried:
if (myDocument.title == "Modal Window") {
parent.document.getElementById("textbox")
}
(I need it to do more than 1 tb, but used that just to try to get there. No luck.
It is contained within an iFrame, so I tried:
if (myDocument.title == "Modal Window") {
var ifr = document.getElementById("iFrame")
var form = ifr.document.getElementById("form")
ClearForm(form)
}
The ClearForm(form) function I stole from another Stack Overflow answer:
function ClearForm(form) {
$(':input', form).each(function () {
var type = this.type;
var id = this.id;
if (type == 'text' && id != 'text2')
this.value = "";
});
}
That 'text2' is one specific tb that we need to remain populated.
Any idea what I'm missing? I've been plagued with this bug for weeks.
I expect your issue is that the form is within an iFrame - most browsers won't allow you to modify elements within an iFrame, from the parent page, if they aren't from the same origin (or if the server is set up to deny it, or if you're looking at the page locally... see here for more details)
To double-check, try moving the form markup into the same page as the modal is in and run your function ClearFormfrom there. I expect that you'll then find it works.
Your only way around this would be to include the ClearForm function within the iFrame'd page, and then trigger it from the parent.
I have popup with autosized attribute. Panel contains fields for search, search button, datatable and datascroller. After press search button or select page in datascroller javascript is called. Javascript is used for move popup to center of browser visible area. All work fine, except popup shadow is stay for previous panel position when popup is already moved.
Button code:
<a4j:commandButton id="searchButton"
value="#{msg.search}"
action="#{chAction.searchSegments}"
styleClass="richButton"
render="clientCategoryCandidateTable"
oncomplete="centerPanel();" />
Javascript:
function centerPanel() {
var modalPanel = #{rich:component('addToSegmentsPanel')};
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var popupWidth = modalPanel.width();
var popupHeight = modalPanel.height();
modalPanel.setLeft(Math.max(0, (windowWidth-popupWidth)/2));
modalPanel.setTop(Math.max(0, (windowHeight-popupHeight)/2));
};
I have tried different version of javascript, but with same side effect:
function centerPopup() {
var popup = #{rich:component('addToSegmentsPanel')};
popup.moveTo(Math.max(0, (window.innerWidth-popup.width())/2),
Math.max(0, (window.innerHeight-popup.height())/2));
};
Screen shots:
Thanks for any help or comments.
Problem was fixed with adding hide and show in end of javascript:
modalPanel.hide(); modalPanel.show();
I resolved the problem on calling resize () action of the popup.
In your case, it should be modalPanel.resize().
I am trying to create some javascript that when an object is added to the window, a listener listens for any click on the body except for the placed object and removes the object if anywhere on the window except the actual object itself is clicked.
Through numerous unsuccessful attempts, the idea I came up with is to dynamically add an overlay div to the screen called overlay2 (or whatever, it doesnt matter) and then listen for clicks on that div. When I add the overlay to the window and set the zIndex to a higher number than the top element already placed (say 5000) and then set the zIndex of the only object to be placed above the overlay to an even higher number (say 6000), the overlay still appears on top of everything and I cannot select any of the objects in the div I meant to place above it.
var overlayDiv = document.createElement('div');
overlayDiv.setAttribute('id', 'overlay2');
overlayDiv.style.zIndex = '5000';
overlayDiv.style.width = '100%';
overlayDiv.style.height = '100%';
overlayDiv.style.left = '0';
overlayDiv.style.top = '0';
overlayDiv.style.position = 'absolute';
document.body.appendChild(overlayDiv);
$(container).append(template);
template.style.zIndex = '6000';
//Listeners
//Page click listener. Closes the tool when the page is clicked anywhere but inside the parent.
var initialClick = false;
$('body').on('click.editObjectListeners', function(event) {
var target = EventUtility.getTarget(event);
if(initialClick) {
console.log(target.id);
if(target.id == 'overlay2' && target.id != '') {
$(overlayDiv).remove();
finish();
};
}
initialClick = true;
});
I've determined that this has everything to do with the absolute positioning of the overlayDiv. While testing, if I used absolute positioning to place the template and if I append the template object directly to the body like I did the overlayDiv, the zIndex works above the overlayDiv as I originally anticipated. Unfortunately absolutely positioning this element doesn't make much sense for me beyond testing purposes. Is there a way to get around this?
Turns out that z-index can really only be used successfully with absolute placed elements. Therefore the original plan to solve the body click listener will not work. Instead, I decided to use jQuery and listener objects to listen for the click instead. Its a much cleaner solution, I just had to wrap my head around it. You can view my other solution here.
I don't know how much this will help your particular problem, but I just happened to notice that you may have a problem with your use of jQuery's "on()" method.
First off, you are using jQuery version 1.7+ with that, correct?
Unlike "live", I believe that the on() parameters are event, selector, function.
So where you have this:
$('body').on('click.editObjectListeners', function(event) {
var target = EventUtility.getTarget(event);
if(initialClick) {
console.log(target.id);
if(target.id == 'overlay2' && target.id != '') {
$(overlayDiv).remove();
finish();
};
}
initialClick = true;
});
I think you want this:
$('body').on('click.editObjectListeners', [SOME SELECTOR], function(event) {
var target = EventUtility.getTarget(event);
if(initialClick) {
console.log(target.id);
if(target.id == 'overlay2' && target.id != '') {
$(overlayDiv).remove();
finish();
};
}
initialClick = true;
});
Hope that helps in some small way.
Good luck!
Try setting the zIndex before appending it to container.
template.style.zIndex = '6000';
$(container).append(template);