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

205
Views
how can I dynamically add ondrag event in a created span

New to javascript, sorry my English is bad.

I call getpb() from HTML, this is what I want

<span id="drag1" title="teeth" draggable="true" ondragstart="dragThis(event)">my teeth</span>

This is what I have, I access array , I set the for loop to 10 instead of array length

function getpb(){
    j=1;
    var pbdiv = document.getElementById("pickBox");
    for (var i=0; i<10;i++)
    {
        var elem = document.createElement("span");
        document.getElementById("pickBox").appendChild.elem;
        elem.id = [j];
        elem.title = "teeth";
        elem.draggable = "true"; 
        elem.ondrag ? does not work
        elem.innerHTML = "my teeth";
        console.log(elem);
        pbdiv.appendChild(elem);
        i++;
        j++;
    }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You're almost there. You just need to add ondragstart event there

function dragThis(event){
  //TODO: Add your logic here
  console.log('dragging')
  console.log(event.target.id)
}

function getpb(){
    j=1;
    var pbdiv = document.getElementById("pickBox");
    for (var i=0; i<10;i++)
    {
        var elem = document.createElement("span");
        document.getElementById("pickBox").appendChild.elem;
        elem.id = [j];
        elem.title = "teeth";
        elem.draggable = "true"; 
        elem.ondragstart = dragThis
        elem.innerHTML = "my teeth";
        pbdiv.appendChild(elem);
        i++;
        j++;
    }
}

getpb()
<div id="pickBox">

</div>

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!