/    Sign up×
Community /Pin to ProfileBookmark

Wrong target when click on svg element

The popup menu with icons gives correct value back when clicking on the text
[url=https://postimages.org/][img]https://i.postimg.cc/BZD93yTy/htmlobject.png[/img][/url]

But when clicking on the svg icon, it refers to this element and gives wrong value back.
[url=https://postimages.org/][img]https://i.postimg.cc/Sx9zfmCd/svgelement.png[/img][/url]

How do I target the li regardless where I click on the popup menu?
[url=https://postimages.org/][img]https://i.postimg.cc/h4xjY2f4/ul.png[/img][/url]

Live: https://task.go4webdev.org/tasks

Here is the code behind the “button”

“`
window.addEventListener(“click”, (event) => {
const
// Provide elements being needed at first level:
row = event.target.closest(‘tr’),
id = row.dataset.id,
ulPopMore = event.target.closest(‘ul.popmore’),
ulPopStatus = event.target.closest(‘ul.popstatus’);
// Now check which element was clicked:
// Was an item in the ul for “more” clicked?

if (ulPopMore) {
const action = event.target.dataset.action;
alert(action)
switch (action) {
case “edit”:
alert(action) //<—never fires
edit(id);
break;
case “del”:
alert(action + id)
del(id);
break;
default:
alert(“wrong”)
}
}
if (ulPopStatus) {
alert(“klick ” + event.target)//random svg and li
const action = event.target.dataset.action;
alert(“action “+ action)
switch (action) {
case “0”:
alert(action)
break;
case “1”:
alert(action)
break;
case “6”:
alert(action)
break;
default:
alert(action)
alert(“wrong”)
}
}
});
“`

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumMay 24.2022 — Try to use the function `closest` as you did in different context.
Copy linkTweet thisAlerts:
@sibertauthorMay 24.2022 — Thank you! closest did the trick!

event.target.closest('li').dataset.action;
×

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.25,
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,
)...