...

View Full Version : Prototype Trying to use functional programming to iterate through elements



cd_gary
10-21-2009, 05:06 AM
I'm currently trying to turn all the paragraphs in the below html script the color red and when I run the following javascript code in firefox, I'm getting a Reference error that says each is not defined:

Javascript Code:

var paragraphs = $$('p');

each(paragraphs, function(element) {
element.style.color = "red";
});

HTML Code:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script type="text/javascript" src="scriptaculous.js"></script>
<script type="text/javascript" src="prototype.js"></script>

</head>

<body>

<p>His chad</p>
<p>How are you</p>

</body>
</html>

rnd me
10-21-2009, 05:46 AM
i think this is what you want:


var paragraphs=$$("div");
paragraphs.each(function(element) {
element.style.color = "red";
});


props on using functional programming, fight the good fight!

ohgod
10-21-2009, 06:50 PM
$$('p').each(function(s){s.setStyle(color: 'red');})

cd_gary
10-21-2009, 07:42 PM
Thanks rnd_me,

Any ideas on how can chek all checkboxes using Prototype without using the name attribute of the checkbox. The below is the html:


<input type="checkbox" name="list[]" value="1">Java<br>
<input type="checkbox" name="list[]" value="2">Javascript<br>
<input type="checkbox" name="list[]" value="3">Active Server Pages<br>
<input type="checkbox" name="list[]" value="4">HTML<br>
<input type="checkbox" name="list[]" value="5">SQL<br>

As you can see I'm using an array for the name attribute so I can't just use
Javascript Code:


function checkAll(field)
{
for (i = 0; i < field.length; i++)
field[i].checked = true ;
}


<input type="button" name="CheckAll" value="Check All"
onClick="checkAll(document.myform.list[])">

It won't work.

I tried using something like the following, but got an error that said box is null:


var box = $('input[type="checkbox"]')

box.each(function(element) {
element.checked = true;
})

ohgod
10-21-2009, 08:23 PM
to get the inputs:

var box = $('formname').getInputs('checkbox');

cd_gary
10-21-2009, 11:03 PM
You can only disable and enable with getInputs function. This isn't working

rnd me
10-22-2009, 12:53 AM
"[]" is not part of a valid id, why are you using it again?

the following changes might fix your problem:



function checkAll(field)
{
for (i = 0; i < field.length; i++)
field[i].checked = true ;
}


<input type="button" name="CheckAll" value="Check All"
onClick="checkAll(document.myform['list[]'])">



var box = $$('input[type="checkbox"]')

box.each(function(element) {
element.checked = true;
})

but you should really just remove "[]" from all the names even if it works...

cd_gary
10-22-2009, 01:06 AM
Can't remove the [] from the names. I need it to create a checkbox array.

see http://www.plus2net.com/php_tutorial/array_checkbox.php.

rnd me
10-22-2009, 01:33 AM
Can't remove the [] from the names. I need it to create a checkbox array.

see http://www.plus2net.com/php_tutorial/array_checkbox.php.

there has to be a different way to do it php.
a simple naming convention like "chkList1", "chkList2" should be easy to catch in an iteration of all fields...

this is covering up poor but hidden server code with poor and visible client code; what's worse?

something to consider is that your page as coded is impossible for a blind person to use. Looking at each input alone, there is no way to tell what it changes. input's aren't connected at to just any old content sitting next to them. Now, the only unique chunk is the number... At least add a label wrap, or a title attrib if you don't want to use unique names; something connecting the choice to the control. it's yet another reason why unique names are so important on form controls.

I'm not sure if you care or not, but you probably should.

I've also found that unique names makes it a LOT easier to debug your app, since you know what is what, and if using GET to dev, everything is in the log...


just my $0.02 as a client-side web developer; i know that time, quality, and features are often exclusionary...

cyclerider
04-30-2010, 05:30 PM
Hello. I'm new to programming. Can someone help me in coding my php forms. I know about form builder (http://*************/tutorial/html-basics/form-builder.html) but I don't want spend too much money.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum