SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast rgf207's Avatar
    Join Date
    Feb 2006
    Location
    Maryland
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help Checking all Check Boxes when variable name is an array

    I am a php guy so my javascript is not too great

    I have a script which will display all plugins in the database. The number of plugins is dynamic. I have a form which lists all the plugins and then has a checkbox to allow/ disallow users to view the plugin like this

    Code:
    <input type='checkbox name='pluginview[]' value='1'>
    Now sometimes there are a lot of plugins so I would like to have a single checkbox which will check/uncheck all the others. The problem is that JavaScript doesn't like when the variable name is an array.

    Can anyone help me figure this out?

    Thanks so much

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    instead of
    document.formname.elementname[]

    use
    document.formname.elements["elementname[]"]

  3. #3
    SitePoint Enthusiast rgf207's Avatar
    Join Date
    Feb 2006
    Location
    Maryland
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't seem to get that to work. Could you show me an example on how to use it? It just says that it's value is undefined

  4. #4
    SitePoint Enthusiast rgf207's Avatar
    Join Date
    Feb 2006
    Location
    Maryland
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nevermind. I got it to work. Here is what i did
    Code:
    function CheckAll(ThisObj)
    	{
    		var val;
    		var len;
    		len = document.getElementsByName(ThisObj).length;
    		val = document.getElementsByName(ThisObj);
    		for(var i = 0; i < len; i++)
    		{
    			if(val[i].checked == true)
    			{
    				val[i].checked=false;
    			}
    			else
    			{
    				val[i].checked=true;
    			}
    		}
    }


Bookmarks

Posting Permissions

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