-
Question about DIV and JavaScript
I have this line of code:
<div class="blue" onclick="myfunction()" id="blue2">
This works fine and makes the JavaScript work if you click the Div.
Is there a way to make it so you don't have to click it, but it works automatically upon page load?
Thanks.
-
Hi there davidwhite,
does this help...
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>basic template</title>
<style type="text/css">
</style>
<script type="text/javascript">
(function() {
'use strict';
function myfunction() {
}
window.addEventListener?
window.addEventListener('load',myfunction,false):
window.attachEvent('onload',myfunction);
})();
</script>
</head>
<body>
<div class="blue" id="blue2">
</div>
</body>
</html>
coothead
-
... or just
Code:
<body onload="document.getElementById('blue2').click()">
use [code]YOUR CODE GOES HERE[/code] or burn in Hell
-
Hi there Padonak,
I would suggest not. 
Further reading:-
coothead
-
hello coothead )
of course you are right, but we can use such things sometimes obtrusive or not for example when debugging smth or else
use [code]YOUR CODE GOES HERE[/code] or burn in Hell
-
Hi there Padonak,
You should also note that your example, unfortunately, does not seem to work in Safari 5.1.7. 
coothead
-
Code:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<title>basic template</title>
<style type="text/css">
</style>
<script type="text/javascript">
function xx(){
var divs=document.getElementsByTagName('div');
for(var i in divs){divs[i].onclick=function(){alert(this.id||'no id defined! innerHTML = '+this.innerHTML);}}
}
</script>
</head>
<body onload="xx();document.getElementById('blue2').click()">
<div class="blue" id="blue2">
lol
</div>
<div class="blue">
megalol
</div>
</body>
</html>
IE,FF,Chrome and Opera VS Safari
4:1 Safari loose ))
use [code]YOUR CODE GOES HERE[/code] or burn in Hell
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
|
Bookmarks