Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Aug 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Trying to use functional programming to iterate through elements

    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:
    Code:
    var paragraphs = $$('p');
    
    each(paragraphs, function(element) {
    element.style.color = "red";
    });
    HTML Code:
    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>

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,189
    Thanks
    10
    Thanked 569 Times in 550 Posts
    i think this is what you want:
    Code:
    var paragraphs=$$("div");
    paragraphs.each(function(element) {
      element.style.color = "red";
    });
    props on using functional programming, fight the good fight!
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #3
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    Code:
    $$('p').each(function(s){s.setStyle(color: 'red');})

  • #4
    New Coder
    Join Date
    Aug 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    <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:

    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:

    Code:
    var box = $('input[type="checkbox"]')
    
    box.each(function(element) {
      element.checked = true;
    })

  • #5
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    to get the inputs:
    Code:
    var box = $('formname').getInputs('checkbox');

  • #6
    New Coder
    Join Date
    Aug 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can only disable and enable with getInputs function. This isn't working

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,189
    Thanks
    10
    Thanked 569 Times in 550 Posts
    "[]" is not part of a valid id, why are you using it again?

    the following changes might fix your problem:


    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[]'])">

    Code:
    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...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #8
    New Coder
    Join Date
    Aug 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #9
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,189
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by cd_gary View Post
    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...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #10
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello. I'm new to programming. Can someone help me in coding my php forms. I know about form builder but I don't want spend too much money.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •