Nader
Nader

Reputation: 376

document.activeElement.href not working in chrome

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)

Upvotes: 4

Views: 4433

Answers (3)

Paul S.
Paul S.

Reputation: 66334

Here 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);
    }
});

Upvotes: 3

user2587132
user2587132

Reputation:

That element would be focused then

$("a:focus").prop("href")

http://jsfiddle.net/kYJ3n/

Upvotes: 2

Liam
Liam

Reputation: 29694

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.

Upvotes: 1

Related Questions