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

227
Views
How to prevent function on children's elements?

I have a code for draggable element

var offsetX;
var offsetY;
Element.prototype.makeDraggable=function(){
  var o=this
  o.onmousedown=function(e){
     offsetX=e.pageX-parseInt(o.style.left)
     offsetY=e.pageY-parseInt(o.style.top)

    document.onmousemove=function(e) {
      o.style.left=Math.min(Math.max(e.pageX-offsetX,o.parentNode.clientWidth-o.clientWidth),0)+'px';
      o.style.top=Math.min(Math.max(e.pageY-offsetY,o.parentNode.clientHeight-o.clientHeight),0)+'px';
    }
    document.onmouseup = function(e) {
      document.onmousemove=o.onmouseup=null
    }
  }
  o.ondragstart = function(){return 0}
}
document.getElementById('object1').makeDraggable();

let spot = document.querySelectorAll('#grid-holder > div');
for(var i = 0; i < spot.length; i ++)
{
  spot[i].onclick = function(){ inventar(); };
}
function inventar()
{
  alert();
}
<div id="parent">
  <div id="object1" style="left: 1px; top: 1px;">

     <div class="grid-holder" id="grid-holder">
         <div></div><div></div><div></div>
         <div></div><div></div><div></div>
         <div></div><div></div><div></div>
         <div></div><div></div><div></div>
         <div></div><div></div><div></div>
         <div></div><div></div><div></div>
         <div></div><div></div><div></div>
         <div></div><div></div><div></div>
     </div>
  
  </div>
</div>

When i click #grid-holder > div i have alert message, but how to prevent this action when i drag #object1 element by holding onto children's element?

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

0

var offsetX;
var offsetY;
var moved = false;

Element.prototype.makeDraggable = function(ev){
  var o = this;

  o.onmousedown = function(e){
  offsetX = e.pageX-parseInt(o.style.left);
  offsetY = e.pageY-parseInt(o.style.top);
  moved = false;  

    document.onmousemove = function(e) {
      o.style.left = Math.min(Math.max(e.pageX-offsetX,o.parentNode.clientWidth-o.clientWidth),0)+'px';
      o.style.top = Math.min(Math.max(e.pageY-offsetY,o.parentNode.clientHeight-o.clientHeight),0)+'px';
      moved = true;    
    }
    document.onmouseup = function(e) {
      document.onmousemove = o.onmouseup = null;
    }
  }
  o.ondragstart = function(){
   return 0; 
  }
}
document.getElementById('object1').makeDraggable();

let spot = document.querySelectorAll('#grid-holder > div');
for(var i = 0; i < spot.length; i ++)
{
  spot[i].onclick = function(){ inventar(); };
}
function inventar()
{
  if(!moved) alert();
}
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!