[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
Page 2 of 2

PostPosted: Fri Oct 06, 2006 12:57 pm
by Batalf

PostPosted: Fri Oct 06, 2006 12:59 pm
by Batalf
yes,

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

is equivalent to

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

in my code.

PostPosted: Fri Oct 06, 2006 1:24 pm
by divtag32
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?

PostPosted: Fri Oct 06, 2006 1:28 pm
by Batalf
"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.

PostPosted: Fri Oct 06, 2006 1:49 pm
by divtag32
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>

PostPosted: Fri Oct 06, 2006 2:05 pm
by Batalf

PostPosted: Fri Oct 06, 2006 2:11 pm
by divtag32