[phpBB Debug] PHP Notice: in file /includes/bbcode.php on line 112: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Notice: in file /includes/bbcode.php on line 112: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Notice: in file /includes/bbcode.php on line 112: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
www.dhtmlgoodies.com • View topic - Beginners question 1 - Javascript loop issues
Advertisement

Beginners question 1 - Javascript loop issues

Moderators: Calcifer, slothy

Postby Batalf » Fri Oct 06, 2006 12:57 pm

Batalf
Site Admin
 
Posts: 2160
Joined: Sat Oct 22, 2005 9:38 pm

Postby Batalf » Fri Oct 06, 2006 12:59 pm

yes,

var radio = document.getElementsByName('type');

is equivalent to

var radio = document.forms['myForm'].elements['type'];

in my code.
Batalf
Site Admin
 
Posts: 2160
Joined: Sat Oct 22, 2005 9:38 pm

Postby divtag32 » Fri Oct 06, 2006 1:24 pm

Works great both ways! Cool, I'm getting this, just one bit to double check - I've written below what I 'think' is going on!


for(var no=0;no<radio.length;no++){

I'm never certain what exactly 'for' does,

you've essentially got:

for (x){y}

So is it a case for each loop of (X) do {y}?

(x) is obviously just incrementing the no as a loop.

{y} is saying the radio being checked by the 'no' loop is checked then print it's value below

if(radio[no].checked)document.getElementById("result").innerHTML=radio[no].value;
}

}


Is that on the right lines?
divtag32
 
Posts: 50
Joined: Mon Sep 25, 2006 12:54 pm

Postby Batalf » Fri Oct 06, 2006 1:28 pm

"for" is as you say a loop.

for(var no=0;no<radio.length;no++)

can be explained like this:

no=0; // Start value of loop
no<radio.length; // The loop should run as long as no is less than radio.length,i.e. number of radio buttons
no++; For each iteration, increment no with 1

Another example:

for(var no=5;no<10;no++){
alert(no);
}

this will alert you the numbers 5,6,7,8 and 9.

You can also change the increment like this:

for(var no=0;no<10;no+=2){
alert(no);
}

This will alert you the numbers 0,2,4,6 and 8.
Batalf
Site Admin
 
Posts: 2160
Joined: Sat Oct 22, 2005 9:38 pm

Postby divtag32 » Fri Oct 06, 2006 1:49 pm

I see - that's a really good explanation of that looping thing - I was just spitting it out parrot-style before, but I get it completely now.

Now I'm playing around with the settings to make it sink into my soft brain! ;) - with the script below, it's now saying 'checked is null or not an object' - the trouble is I can't see what is different apart from I've renamed the var?

<html>
<head>
<script language="JavaScript">

function evaluate()
{
var radiotype = document.getElementsByName=("type");
for(var i=0;i<radiotype.length;i++)
{
if(radiotype[i].checked)document.getElementById("result").innerHTML=radiotype[i].value;
}
}
</script>
</head>

<body>
Type in name: <input type="textbox" id="name"><br />
<form>
<input type="radio" name="type" value="First type"> First type
<br />
<input type="radio" name="type" value="2nd Type"> 2nd Type
<br />
</form>
<input type="submit" value="Evaluate" onclick="evaluate()">
<div id="result"></div>
</body>
</html>
divtag32
 
Posts: 50
Joined: Mon Sep 25, 2006 12:54 pm

Postby Batalf » Fri Oct 06, 2006 2:05 pm

Batalf
Site Admin
 
Posts: 2160
Joined: Sat Oct 22, 2005 9:38 pm

Postby divtag32 » Fri Oct 06, 2006 2:11 pm

divtag32
 
Posts: 50
Joined: Mon Sep 25, 2006 12:54 pm

Previous

Return to Javascript

Who is online

Users browsing this forum: No registered users and 1 guest

cron