This question already has answers here:
Can I find events bound on an element with jQuery?
(9 answers)
Closed 9 years ago.
for example I have:
asdasd
...
//js code in other place or other file hard to find, I have this:
$('#activador').click(simplefunction); or
$(document).on('click','#activador',simplefunction); or
$('#activador').live('click',simplefunction);
function simplefunction(){
alert('hello');
}
...
How can I find if the html ('#activador') have a function attached, and how can I find what function the element ('#activador') will trigger?
I know using
var clickEvents = $('#activador').data("events").click;
but I cannot use something like this:
var clickEvents = $('#activador').data("events").on; or
var clickEvents = $('#activador').data("events").live;
You can use _data function to find out event associated with that element,
console.log($._data($('#activador'), "events").click);
Related
This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 2 years ago.
I have the following piece of code in my javascript file(helper.js):
var a = $('li')
$(window).keydown(function(e) {
\\whatever
})
it is supposed to select all tags in my html file and store in in a, however when I use this code, it a is null(which it shouldn't be). when I change my code to:
$(window).keydown(function(e) {
var a = $('li')
\\whatever
})
a is initialized correctly. Does anyone knows why? I am using jquery 3.3.1.
If you are dealing with the DOM (Document Object Model), use $(document).
If you are dealing with how user interact with the window, screen and so on, use $(window)
Here is a link for better understanding window and document
Window vs Dom
This question already has answers here:
Select multiple jQuery objects with .add()
(3 answers)
Closed 5 years ago.
Here is my code:
var $btnNone = $('#btn-none');
var $btn1234 = $('#btn-1, #btn-2, #btn-3, #btn-4');
// This selector works fine
var $btnReview1234None = $('#btn-1, #btn-2, #btn-3, #btn-4, #btn-none')
// HOW TO MAKE THIS SELECTOR WORK.
// This selector ignores $btnNone but respects $btn1234.
// This listens only first item in the selector
var $btnReview1234None = $($btn1234, $btnNone);
$btn1234None.click(function(){
alert('Lorem')
});
The issue is because $($btn1234, $btnNone) will be treated as a contextual selector, ie. jQuery will search the DOM to find the $btn1234 element within $btnNone.
To fix this you could provide an array of both elements to the selector:
var $btnReview1234None = $([$btn1234, $btnNone]);
Or you could use add():
var $btnReview1234None = $btn1234.add($btnNone);
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I'm animating an svg file, and in order not to bloat my code I have written the follow function:
function animateSingleAtt(svgName, element, attToAnimate, attValue, animationTiming) {
svgName.select( element ).animate({
attToAnimate : attValue
}, animationTiming);
}
All parameters are loaded just fine, except for attToAnimate. I have no clue as to why this is – it is just passed to the svg element as an attribute named 'attToAnimate'.
I have tried logging it outside of the animate-function, and when I do that, it's passed just fine.
Any help will be greatly appreciated!
It looks like you are using attToAnimate as a key on an object. To do this properly, change the code to something like this:
function animateSingleAtt(svgName, element, attToAnimate, attValue, animationTiming) {
var anObject = {};
anObject[attToAnimate] = attValue;
svgName.select( element ).animate(anObject, animationTiming);
}
This should properly build the object you want to pass to .animate
This question already has an answer here:
How do you assign an event handler to multiple elements in JavaScript?
(1 answer)
Closed 7 years ago.
I am trying to run a function when user clicks on a class.
Here is a fiddle with a similar setup. Except that I am not using buttons in the my code.
FIDDLE
document.querySelectorAll('.menu').onclick = function () { alert("test"); };
I've also tried using the getElementsByClassName, with the same results. Is there something I am missing here?
*Note: I need to accomplish this without jQuery
querySelectorAll returns a list of elements, so you need to specify the one you want, in your case [0]:
document.querySelectorAll('.menu')[0].onclick = function () {
alert("test");
};
jsFiddle example
This question already has answers here:
JavaScript DOM remove element
(4 answers)
Closed 8 years ago.
Ok I've been so spoiled with Jquery that I don't even know how to remove an element anymore with plain Javascript.
So I have this which I would like to remove with Javascript.
Any help appreciated.
Try this
function RemoveElement(elemID) {
var elem = document.getElementById(elemID);
if (elem.parentNode) {
elem.parentNode.removeChild(elem);
}
}
Node.removeChild is the method, see documentation here: https://developer.mozilla.org/En/DOM/Node.removeChild
Example:
var parent = document.getElementById("mydiv");
var child = document.getElementById("other");
parent.removeChild(child);
like:
container.removeChild(child);