Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
Why onlick function return this?
let classMessage = document.createElement('class');
document.getElementById("chat").prepend(classMessage);
classMessage.id = tabMsg[0];
classMessage.innerHTML += '<button onclick="del(' + tabMsg[0] + ')">Try it</button>'

function del(parsedata) {
console.log(parsedata)
}

tabMsg is a parsed line (with split).

i dont understand why when i click on the button the output on the console is :

<class id="vuwzbip6dr"><button onclick="del(vuwzbip6dr)">Try it</button><p>gfdgf gdfgf</p></class>

why he dont put the id vuwzbip6dr !!! ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Encapsulating strings with strings within strings generally causes more problems than it solves and therefore should be avoided.

In your case that means this statement:

'<button onclick="del(' + tabMsg[0] + ')">Try it</button>' is causing the issue for you.

That line is interpreted as:

<button onclick="del(v)">Try it</button>

Where v becomes an identifier pointing to the this, or the currentTarget element and not a string like this: "dev('v')".

Looking at the documentation one sees the onclick handler is provided the currentTarget as the first argument to the callback - hence the assignment I mention.

To fix this you should rely on the tools JavaScript provides - namely, it gives you the currentTarget (target element of the click), so all you have to do is use that and ask for it's parent element's id attribute:

classMessage.innerHTML += '<button onclick=del(this)>Try it</button>'

function del(target) {
    console.log(target.parentNode.id)
}

But that isn't recommended either. You should always try to avoid using in-line event handlers. I'll leave that to a different question for you.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!