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

144
Views
How can I detect mouseup on top of an element when the click started on a different element?

I'm trying to detect when the mouse is released over a certain html element. I would like the mouseup event to fire no matter where the dragging started initially. Here is my attempt, which behaves very weirdly (sometimes it fires, sometimes not, sometimes I get two alerts):

$("body").click(function() {
  $(".cube").mouseup(function() {
    console.log('mouseup')
  });
});
.box,
.cube {
  background: rgb(255, 0, 0);
  width: 100px;
  aspect-ratio: 1;
  display: inline-block;
}

.cube {
  background: #222;
}
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>

<body>
  <div class="box"></div>

  <div class="cube"></div>
</body>

Problem number 2 demonstrated: the event is not firing after dragging starts from the upper div:

  $(".cube").mouseup(function(){
      console.log('mouseup')
    });
  .box{
        background: rgb(255, 0, 0);
        width: 200px;
        height: 200px;
    }

    .cube{
        background: #222;
        width: 200px;
        height: 200px;
    }
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>

<body>
  <div class="box"></div>

  <div class="cube"></div>
</body>

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

0

It's even simpler than what you made. You just only need

<script>
     $(".cube").mouseup(function(){
       alert('mouseup')
     });
</script>

If you release it, it means that you have clicked anywhere before

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!