1

I'm trying to execute javascript function - onload only in specific div, but I'm stuck, so if someone can help me, please. Thank you very much in advance

function dropsFall() {

}
.drop {
  display: block;
  min-height: 280px;
  overflow: hidden;
}
<div class="row">
  <div class="col-xl drop"></div>
</div>

7
  • what you meant actually Commented Dec 16, 2017 at 19:13
  • welcome to stack overflow , you want to execute function when div clicked ? please search about it then post your question. Commented Dec 16, 2017 at 19:15
  • Thanks for the welcome :) I'm ashamed from this question at all but .... Actually, no. The function is an animation of shapes and color changing, which is now executing all over the page, not at the specific place/div where I want it to be. Commented Dec 16, 2017 at 19:20
  • It's unclear what you're asking for ,what do you want exactly , execute function on div ? on click , on drag , drop ?? Commented Dec 16, 2017 at 19:21
  • 1
    @xx5ko could you post the whole code of annimation here Commented Dec 16, 2017 at 19:31

4 Answers 4

1

This snippet of code performs an animation inside a specified div (that acts like a container):

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>

</head>
<body>
<style>
.rectangle
{
   width: 300px;
   height: 20px;
   display: block;
   position: relative;
   border: 1px solid black;
   margin: 20px 0;
}

.square-small
{
   display: block;
   width: 20px;
   height: 20px;
   position: absolute;
   background-color: green;
}
</style>
<script
  src="https://code.jquery.com/jquery-2.2.4.min.js"
  integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
  crossorigin="anonymous"></script>

  <script>
  $('body').ready(function(){
    $('.rectangle')
   .find('.square-small')
   .animate({
      left: $('.rectangle').width() - $('.rectangle').find('.square-small').width()
    }, 'slow');});
</script>

<div class="rectangle">
   <div class="square-small"></div>
</div>

</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

Then why not write your script inside your div like following:

<div class="row">
  <div class="col-xl drop">
  <script>dropsFall()</script>
  </div>
</div>

Comments

0

Pass your div ID to the function as a parameter and use if statement to check if it is the required div or not

function dropsFall(my_div) {
   if(my_div==‘your required div’){
       //your code will be here
   }
}

Comments

0

Do you want to add drop class in div on click event ?

<div class="row">
     <div class="col-xl" onclick="dropsFall(this)"></div>
</div>

1 Comment

explain your requirement exactly.

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.