I have this code in the footer of my html page
<script type="text/javascript">
// using jQuery
$('video,audio').mediaelementplayer();
</script>
the above code is adding video player on the html page.
Now I have created a separate js file in which I have already have some line of code which is creating owl sliders, tooltips, number counters etc.
When I add the above code into that separate js file
it does not work, instead when I keep it in the footer of the html page
it works fine.
You may wait until jQuery is full loaded or
ready
.Ex.
This code goes in external
js
file, then you need to include the file in the HTML<script type="text/javascript" src="path/to/your/js/file"></script>
Do you have a proper link to the separate js file in your page, generally at the bottom of the body? It should look something like this:
If you've done that properly, have you tried clearing your browser cache? You may need to do that to detect new javascript files.
Try placing your code within
$(function(){ ... }
. This will execute when the DOM is loaded (currently your code is being executed before jQuery is loaded, if you check the JavaScript console, you will see an error something like$ is not defined
)or
You can read about what that is doing here.
$(function()
is the same as$( document ).ready()
your html(basically) should look like this:
and your jquery file:
How do you call your external js file ?
You must add your references js before your external js file.
you must add your function on document.ready.