jszeto
asked on
How to bind an onload event to a div element in javascript/jQuery?
Want to do something like this:
<div onload="fireevent()"></div >
but onload seems only work on window and entire dom, any ideas?
<div onload="fireevent()"></div
but onload seems only work on window and entire dom, any ideas?
ASKER
I want to fire onload event for each of the <div>s with different parameters passed into a user-defined function, so basically $(window).bind does not fit in this scenario.
Hi,
jszeto
U can do the same thing here also call multiple function from onload and pass multiple parameter to diff function.
If the same thing is not possible in your scenario then can u elaborate your purpose of use the same thing with details of your code.
Thanks
jszeto
U can do the same thing here also call multiple function from onload and pass multiple parameter to diff function.
If the same thing is not possible in your scenario then can u elaborate your purpose of use the same thing with details of your code.
Thanks
ASKER
The scenario is, when the page loads, each <div> would fire an event and pass a value (say, the <div>'s ID) into one single function and generate a new width for that specific <div>, do you have any suggestions? So basically each of the <div>s' width would dynamically generate when the page loads.
ASKER
Here is the code snippet I came across, but it did not work:
<script type="text/javascript">
$(function(){
$('.progressBar').each(function(){
var xmlhttp;
var barID = $(this).attr("ID");
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
$(this).css('width', xmlhttp.responseText);
var temp = parseInt(xmlhttp.responseText);
var indicator = temp/19*10 + '%';
$(this).find('span').html(indicator);
}
}
xmlhttp.open("GET","loadWidth.php?id="+barID,true);
xmlhttp.send();
})
}
</script>
how about:
$(window).ready(function() {
$("div").each(function(){
fireevent(this);
});
});
This will fire the fireevent function for all divs, passing the current div as a parameter to the function.
The onload event is not supported by every element (see this tutorial).
Unless you intend to fire a function at every time your divs are accessed/clicked, I suggest then to use a function that is fired on body load and that fires events for your divs only, like this example:
function .... {
var divs = document.getElementsByTagN ame("div") ;
for(i=0;i<divs.length;i++) {
(what you want)
Unless you intend to fire a function at every time your divs are accessed/clicked, I suggest then to use a function that is fired on body load and that fires events for your divs only, like this example:
function .... {
var divs = document.getElementsByTagN
for(i=0;i<divs.length;i++)
(what you want)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, really good tips.
jszeto
U can do one another thing for same function.
$(window).bind("load", function() {
fireevent();
});
Note: U must write this function between Javascript tag.
This will load on page startup.