0

here's how i create element

var a = document.createElement("a");
a.setAttribute("id","remove");
a.setAttribute("uk-icon","icon: trash");

when i inspect

enter image description here

i want to get a tag but it only gives icon

document.addEventListener('click', function(e){
    console.log(e.target);
});

enter image description here

what is the correct way on creating element?

2
  • 1
    what's the real issue ? create a element or get the <A> tag in event ? you listen the document event : document.addEventListener... then if you click inside <A> the pointer "see" the svg inside, the target of event is the svg element. create "a" element with a=document.createElement... and attach to them the listener : a.addEventListener... Commented Dec 13, 2019 at 2:17
  • sorry, Thank you Commented Dec 13, 2019 at 2:29

1 Answer 1

1

Your question/problem is unclear. document.createElement() is the correct way to create an element (although an a element does not have a uk-icon attribute). The inspect code you shared clearly shows that you made a new element. It looks like you've then placed an SVG element within the a element and that's what you'll see on the page. And, since that's what you see, then that's what you are clicking on. Instead of document.addEventListener(), use document.querySelector("#remove").addEventListener() to react to the a being clicked.

Sign up to request clarification or add additional context in comments.

1 Comment

sorry, Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.