2

I am adding my external .js file as

<head>
    <script type="text/javascript" src="service/js/test.js"></script>
</head>

Inside the test.js I have one function called functionTest().

Calling this function onload of the body is working fine;

<body onload="functionTest()">

</body>

My Question is:

I want to call this function two time in the two different divs. How should I achieve this ?

<body>
    <div id="left">
        <!-- Directly Want to call functionTest() when div is getting loaded -->
    </div>
   <div id="right">
       <!-- Directly Want to call functionTest() when div is getting loaded -->
   </div>
</body>
5
  • what do you mean by div loaded.... who is loading the div Commented Jun 24, 2015 at 6:35
  • What do you want to achieve by calling a function after each div is loaded? Why don't you call the function when the document is loaded? may be use jQuery ready (api.jquery.com/ready) ? Commented Jun 24, 2015 at 6:51
  • @LowFlyingPelican : I have predefine widgets in the javaScript file. Each widget have some different UI and functioning. I just want to embed the widgets in the HTML file for Demonstration. Commented Jun 24, 2015 at 6:54
  • You could do something like, $( document ).ready( function(){ functionTest($('#left')); functionTest($('#right')); } ), then you get function called for both widgets while not having to write js inside your html Commented Jun 24, 2015 at 7:00
  • @LowFlyingPelican : Thanks for response. I will try that also. :) Commented Jun 24, 2015 at 7:01

2 Answers 2

6

Add the call to the function at the end of the div. The function functionTest will be called when the div is completely loaded.

<body>
    <div id="left">
        <!-- Directly Want to call functionTest() when div is getting loaded -->

        <script>functionTest();</script>
        <!--    ^^^^^^^^^^^^^^^ -->
    </div>
    <div id="right">
        <!-- Directly Want to call functionTest() when div is getting loaded -->

        <script>functionTest();</script>
        <!--    ^^^^^^^^^^^^^^^ -->
    </div>
</body>
Sign up to request clarification or add additional context in comments.

Comments

3

Just insert <script> functionTest() </script> inside each div

Comments

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.