Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-17-2007, 12:06 PM   PM User | #1
byte1bit
New Coder

 
Join Date: Feb 2007
Location: Lisboa
Posts: 21
Thanks: 3
Thanked 0 Times in 0 Posts
byte1bit is an unknown quantity at this point
Angry simple alert not working!

could someone please tell me why this simple code is not working? i tried all variation i could remember, i click and click, tried in firefox IE, netscape, and nothing:

<html>
<head>
<title>behavior</title>
<script language="javascript" type="text/javascript">
var parag = document.getElementsByTagName("p");
for(var i=0; i<parag.length; i++) {
parag[i].onclick = function() {
alert("click!");
}
}
</script>
</head>
<body>
<p>one paragraph</p>
<p>other paragraph</p>
</body>
</html>

any kind of help wuld ge appreciated.
byte1bit is offline   Reply With Quote
Old 02-17-2007, 03:47 PM   PM User | #2
BonRouge
Regular Coder

 
BonRouge's Avatar
 
Join Date: Mar 2006
Location: Sendai, Japan
Posts: 328
Thanks: 0
Thanked 0 Times in 0 Posts
BonRouge is on a distinguished road
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
* {
margin:0;
padding:0;
}
body {
background-color:white;
}
</style>
<script type="text/javascript">
function click() {
var parag = document.getElementsByTagName("p");
	for( i=0; i<parag.length; i++) {
		parag[i].onclick = function() {
		alert("click!");
		}
	}
}
window.onload=click;
</script>
</head>
<body>
<p>one paragraph</p>
<p>other paragraph</p>

</body>
</html>
__________________
bonrouge
BonRouge is offline   Reply With Quote
Old 02-17-2007, 04:40 PM   PM User | #3
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
...and the reason is:

The loading process works sequentially (well, not exactly, but to simplify it for your problem, let's say it does so). So that the moment you try to find the element (with javascript placed in head), that element is not yet loaded (in the body)

Most of the time javascript uses functions called by an event, following an user's action. The user's action is, in your case loading the page. For this action javascript uses window.onload event. By short onload
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 02-18-2007, 01:16 PM   PM User | #4
byte1bit
New Coder

 
Join Date: Feb 2007
Location: Lisboa
Posts: 21
Thanks: 3
Thanked 0 Times in 0 Posts
byte1bit is an unknown quantity at this point
you are right. i think that is why calling the function with

<script>
function click(){}
window.onload=click();
</script>

doesn't work and

<body onLoad="click();">

does work. thanks
byte1bit is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:30 AM.


Advertisement
Log in to turn off these ads.