I used document.activeElement.href
for get target location of clicked tag. Its worked property in firefox, but not working in chrome.
I want to get "href"
location of a clicked(active) element in other function outside of it.
like this:
function animate() {
alert(document.activeElement.href); //not working in chrome
});
Thanks alot
EDIT :
Remember i dont want to use "this" or $(this) because it doesn't work in a function outside of the element.
I know i can use onclick="...."
then use "$(this)"
for each element but i dont want this.
My question is simple:
can i get clicked(active) elementID in a function outside of it? (Except in firefox)
I used document.activeElement.href
for get target location of clicked tag. Its worked property in firefox, but not working in chrome.
I want to get "href"
location of a clicked(active) element in other function outside of it.
like this:
function animate() {
alert(document.activeElement.href); //not working in chrome
});
Thanks alot
EDIT :
Remember i dont want to use "this" or $(this) because it doesn't work in a function outside of the element.
I know i can use onclick="...."
then use "$(this)"
for each element but i dont want this.
My question is simple:
Share edited Aug 16, 2013 at 12:39 Nader asked Aug 16, 2013 at 11:01 NaderNader 3764 silver badges13 bronze badges 3can i get clicked(active) elementID in a function outside of it? (Except in firefox)
-
@Liam, your link says
activeElement
is supported by Chrome 2 and later (MDN says the same, the accepted answer saying Chrome 9+ is apparently a mistake). – Frédéric Hamidi Commented Aug 16, 2013 at 11:06 - @FrédéricHamidi, sorry must of misread it. My bad – Liam Commented Aug 16, 2013 at 11:07
-
@Nader, is there any chance you set the focus to another element before calling
animate()
? – Frédéric Hamidi Commented Aug 16, 2013 at 11:07
3 Answers
Reset to default 3Here is a solution using addEventListener
from Vanilla JS
document.addEventListener('click', function (e) {
var elm = e.target;
if (elm && elm.nodeType === 1 && elm.nodeName === 'A') {
alert(elm.href);
}
});
That element would be focused then
$("a:focus").prop("href")
http://jsfiddle/kYJ3n/
you could do this with jQuery
$(document).on('click', 'a', function() {
alert($(this).attr('href'));
});
this does though depend on when you actually call your event. You haven't specified this so it's hard to tell.