0

I want my child elements of body tag to be used when document is fully loaded. like I have

<body> Country: <br> <div> <datalist id="country"></datalist> <input type="text" list="country" id="search" onmouseover = 'populate("country")'/> </div> </body> 

In the input element, i am using onmouseover event to call javascript function. But I want that thing to be enabled when all element are fully loaded.

Up till now, I have seen online that there is an onload event that can be used in body tag to call any particular function. But I don't want to call any function on onload event, just want to make sure that onmouseover event of input element should fired when body is fully loaded.

1
  • 1
    Try $(document).ready(function(){ }); function instead.. Commented Jul 25, 2016 at 10:40

3 Answers 3

2

You can use jQuery to listen on the window load event and then create your mouseover listener on the input whenever all media is fully loaded:

$(window).on("load", function() { $("#search").on("mouseover", function() { populate("country"); }); }); 

Instead of window.load you could even use document.ready. The first will even wait until all other things, like images, are loaded. The last will only wait until the DOM is finished ...

// $(function() {}) is a shorthand for $(document).ready(function() {}); $(function() { $("#search").on("mouseover", function() { populate("country"); }); }); 
Sign up to request clarification or add additional context in comments.

2 Comments

what if I want to make all elements, not to be used before window loaded. Elements can be any, having any event (rather than just mouseover).
That belongs to your actual project. One quick idea could be to hide the body by default and just show them whenever the page load has been finished. But as I said, that belong to your project. In general I don't see why you have to wait until load. That is a special case ...
1

you can use jQuery for this

Your HTML code

<body> Country: <br> <div> <datalist id="country"></datalist> <input type="text" list="country" id="search" /> </div> </body> 

in you javascript code

$(document).ready(function(){ $("#search").hover(function(){ populate('country'); }); }); 

1 Comment

Note that hover() is not the same as a mouseover event - it's a mouseenter and mouseleave combined
1

In jquery use the

$(document).ready(function(){ $('#search').on('mouseenter', function(){ console.log('mouse over'); }) })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> Country: <br> <div> <datalist id="country"></datalist> <input type="text" list="country" id="search" /> </div>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.