SeyedMostafa
SeyedMostafa

Reputation: 5

Why doesn't onload event work as an event of my DIV?

I am trying to refer to a JS function for some of my DIVs. For various reasons, I need to place an on+event inside the HTML tag of these DIVs. So my code looks like this

<div id="item1" onload="HideME(this)">item 1</div>
<div id="item2" onclick="HideME(this)">item 2</div>
<script >
function HideME(which1) {
            alert (which1.innerHTML);
}
</script>

As you see, I have two DIVs in this code, the first one given an onclick event, which works fine, and the second one, is given an onload event which doesn't do anything. Why?

P.S. Please help me with correcting this method of calling my function (onload in the tag line), not other methods if there are any. Many thanks

Upvotes: 0

Views: 3148

Answers (2)

Matt
Matt

Reputation: 124

A solution might be to call a function directly AFTER your element

Example:

<div id="item1">Item 1</div>
<script type="text/javascript">
   HideMe('item1');
</script>

Upvotes: 0

Ludovit Mydla
Ludovit Mydla

Reputation: 822

As a proper answer: according to specs, onload works on <body>, <frame>, <iframe>, <img>, <input type="image">, <link>, <script>, <style> tags. So it won't work on plain div

Upvotes: 2

Related Questions