jquery click elsewhere function [duplicate] - javascript

This question already has answers here:
How do I detect a click outside an element?
(91 answers)
Closed 8 years ago.
I'm trying to remove the css when somewhere on the page has been clicked. I know how to implement the css changes but can't figure out how to or where to place the listener for a mouse click.
Any suggestions would be great.
$(document).ready(function() {
var friends = false;
$('#friends').click(function() {
friends = true;
$('#cont').css({'left':'502px'});
$('#members').css({'left':'251px','display':'inline','opacity':'1'});
alert(friends);
});
if($(document).click() && friends){
alert('here');
};
});

Got it working eventually using this but modified it alot:
jQuery().ready(function(){
$('#nav').click(function (event) {
$(this).addClass('activ');
event.stopPropagation();
});
$('html').click(function () {
if( $('#nav').hasClass('activ') ){
$('#nav').removeClass('activ');
}
});
});

Related

Dinamically created elements with ng-angular didn't trigger onClick event [duplicate]

This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 6 years ago.
I've got some cards (forms) created with ng-angular based on Array list.
On each card i have onClick event which doesn't trigger. If I copy paste the function code on browser console, all events work ! I guess that events aren't bind when the DOM is fully loaded.
These are my functions :
$('.floating-open').on('click', function () {
var $this = $(this);
$this.parents('.clickable-button').addClass('clicked');
$this.parents('.clickable-button').next('.layered-content').addClass('active');
setTimeout(function () {
$this.parents('.card-heading').css('overflow', 'hidden');
}, 100);
});
$('.floating-close').on('click', function () {
var $this = $(this);
$this.parents('.layered-content').prev('.clickable-button').removeClass('clicked');
$this.parents('.layered-content').removeClass('active');
setTimeout(function () {
$this.parents('.card-heading').css('overflow', 'initial');
}, 600);
});
Thanks in advance for your help
try to bind click event like below
$(document).on('click', '.floating-close', function(event) {
//your code
}

Using jQuery how to get right click coordinates on the target element [duplicate]

This question already has an answer here:
Right click coordinates [duplicate]
(1 answer)
Closed 6 years ago.
Is there any way to find the co-ordinates of a right click event on a div element as I have to set context menu based on the position of click.
Any help and suggestion will be appreciated. Thanks.
You can try:
$('div').on('contextmenu', function (e) {
console.log(e.pageX);
console.log(e.pageY);
});
And for whole page:
$('div').on('contextmenu', function (e) {
console.log(e.clientX);
console.log(e.clientY);
});
Fiddle: https://jsfiddle.net/shree/awf5u2xx/1/
$(document).ready(function(){
document.oncontextmenu = function() {return false;};
$(document).on('mousedown', '#TargetElementId', function (e){
if( e.button == 2 ) { // Right mouse button clicked
return {e.pageX, e.pageY} //return co-ordinates
}
return true;
});
});
From SO Answer to find right mouse button clicked

Add/Remove classes not working for some reason [duplicate]

This question already has answers here:
How do I attach events to dynamic HTML elements with jQuery? [duplicate]
(8 answers)
Closed 7 years ago.
var vaStyle = $("nav.global_nav, .rsGCaption .intro-caption");
var vid_detach = $("div.rsCloseVideoBtn");
var vid_launch = $("h1#vidLaunch");
vid_launch.click(function () {
vaStyle.addClass("hprs-video-active");
});
vid_detach.click(function(){
vaStyle.removeClass("hprs-video-active");
});
Why isn't my removeClass working? Is there a better method?
Also, when you declare a var does it work document-wide?
Sounds like your elements are getting added dynamically. Use event delegation for solving this. I made changes to the event binding.
var vaStyle = $("nav.global_nav, .rsGCaption .intro-caption");
var vid_detach = "div.rsCloseVideoBtn";
var vid_launch = "h1#vidLaunch";
$(document).on('click', vid_launch, function () {
vaStyle.addClass("hprs-video-active");
});
$(document).on('click', vid_detach, function () {
vaStyle.removeClass("hprs-video-active");
});

Jquery appended item click function not working [duplicate]

This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 7 years ago.
Ok so I have a button that when someone clicks it appends some info to a section.
like so:
function() {
$(".blend-tile").click(function() {
$(this).hide();
var li = $('<li><div class="align-table"><div class="color-img t-align"></div><div class="t-align"><div class="infora"></div><div class="percent-mix"></div><div class="mix-value"></div></div></div><div class="clear-tile">X</div></li>');
$('#mixers').append(li);
$('.tpic', this).clone(true, true).contents().appendTo(li.find('.color-img'));
$('.infora', this).clone(true, true).contents().appendTo(li.find('.infora'));
if ($('#mixers li').length === 6) {
$(".tiles").hide();
$(".nomore").show();
}
});
});
Which is all good work's fine.
But I also want all of this to remove() when I click <div class="clear-tile">X</div>.
and for that I am using:
$(function() {
$(".clear-tile").click(function() {
$(this).parent().remove();
});
});
But nothing happens no error nothing.
I also have similar function's in my file that use remove() and such which work fine. It's just anything that I try to trigger from .clear-tile just doesn't work at all.
I have a feeling it's down to me appending it but I'm not sure any help would be much appreciated
You need to use event delegation:
$("#mixers").on("click", ".clear-tile", function() {
$(this).parent().remove();
});
Instead:
$(".clear-tile").click(function() {
$(this).parent().remove();
});

combine jQuery on ready + change [duplicate]

This question already has answers here:
JQuery combine $(document).ready and $('DropDown').change declaration
(2 answers)
Closed 8 years ago.
i want to change this line of code to run ready and change
jQuery('#form_auction_type').change(function(e) {
editing the line to
jQuery('#form_auction_type').ready(function(e) {
fixes my problem, but i still need .change function
the whole code:
jQuery('#form_auction_type').change(function(e) {
if (jQuery('#form_auction_type').val() == '2') {
jQuery('#form-row-rapper-price_bin').show();
} else {
jQuery('#form-row-rapper-price_bin').hide();
how can i put ready and change in the same line!!
Change it to
jQuery(function($) {
$('#form_auction_type').on('change', function() {
$('#form-row-rapper-price_bin').toggle($('#form_auction_type').val() == '2');
}).trigger('change');
});

Categories

Resources