MBH
MBH

Reputation: 119

Cannot get child element's href value

Being spoiled by jQuery for so long, now I'd like to rewrite this line in vanilla javascript:

let slug = $(this).children().find('.pjt').attr('href');

What I tried (among others) is:

 const goToPage = (e) => {
     var slug = e.target.querySelector(".pjt").attr("href");
    console.log("slug is:", slug);
    //window.location.href = slug;
   };

But I get this error:

Uncaught TypeError: e.target.querySelector(...).attr is not a function

How can I fix this?

Upvotes: 1

Views: 580

Answers (1)

Tushar Gupta
Tushar Gupta

Reputation: 15923

It happens to most of us, use getAttribute in Vanilla JS

e.target.querySelector(".pjt").getAttribute('href')

Upvotes: 2

Related Questions