/    Sign up×
Community /Pin to ProfileBookmark

Get parent class when clicking on a svg element?

I want to create a button with a svg icon in it. When clicking on the “button”, I want to fire an event based on the parent class. But `event.target.parentElement.className` does not work when clicking on the svg element.

How do I get the class **btn_main** regardless where I click on the “button”? (on the text, icon or empty area)
Now I get the className only when clicking on the text…

https://jsfiddle.net/gjcow97t/11/

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJun 25.2022 — Try to use the same procedure we used several times before: Get parent element by the function closest:
``<i>
</i>function keep_open(event) {
const prnt = event.target.closest('.btn_main');
if (prnt) {
console.log('clicked inside .btn_main');
}
}<i>
</i>
``
Copy linkTweet thisAlerts:
@Arunprakash01Jun 25.2022 — I like this article very much. The content was good. If any of the business person are looking for inventory management software, I found this site and they are providing the best service to the business person regarding the mobile app development company..

https://www.ymtsindia.com/mobile-app-development-company
Copy linkTweet thisAlerts:
@sibertauthorJun 25.2022 — > @Sempervivum#1644890 Get parent element by the function closest:

Thank you! Worked as expected!
×

Success!

Help @sibert spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...