I want to call some JS function when particular div with class is loaded in my Rails 4 app.
<div class="myClass">
hello world
</div
How to call some js code only when this div is loaded.
I want to call some JS function when particular div with class is loaded in my Rails 4 app.
<div class="myClass">
hello world
</div
How to call some js code only when this div is loaded.
I dont think, it is relevant to rails
There can be two approach:
1: write a JS code below your html DIV
<div class="myClass">
hello world
</div
<script type="text/javascript">
yourfuncion();
</script>
2: call a function when document is ready, like:
$(document).ready(function(){
yourfuncion();
});
You can bind to DOMNodeInserted
to check when a certain element has been loaded.
Example:
$(document).on('DOMNodeInserted', function(event){
if($(event).hasClass("myClass")){
// do something
}
});
Use with caution if your page is large as this is known to cause performance concerns. Alternative to DOMNodeInserted
What you can do is check is the div is loaded every few seconds until it's loaded.
$(document).ready(function(){
checkDiv();
});
function checkDiv () {
if($('#myDiv').is(':visible'))){
// what you whant
} else {
setTimeout(checkDiv, 50); //wait and try again.
}
}
I hope it's helps.