www.webdeveloper.com
Results 1 to 5 of 5

Thread: Need help using ID rather than name.

  1. #1
    Join Date
    Sep 2006
    Posts
    11

    Need help using ID rather than name.

    Hello. I wrote this javascript some time ago to control the checkboxes in a form. It currently utilizes the form's name attribute, which is apparently depreciated in xhtml. I want to use the ID tag instead, with i'm assuming document.getElementById('form_id') or document.forms['form_id']. However, no matter what i try i can't seem to make it work. Here is an example of my code:

    Code:
    function uncheck_all(box_name)
    {
    	if ( eval("document.search_form['"+box_name+"[]'][0].checked==true") ){
    		for (i=1;i<eval("document.search_form['"+box_name+"[]'].length"); i++){
    			eval("document.search_form['"+box_name+"[]']["+i+"].checked=false");	
    		}						
    	}
    }
    Where search_form is current name of my form, and the variable "box_name" containing the name of an array of checkboxes to be effected. Can someone help my with my syntax here?

    Thanks for your time,
    -Scott

  2. #2
    Join Date
    Jun 2007
    Posts
    667
    A checkbox isn't the best control to use for this operation unless it also disables the other boxes, however this should do it:
    Code:
    function uncheck_all(formRef, box_name)
    {
      var group = formRef[ box_name ];
    
      if( group[ 0 ].checked )
       for( var i = 1, len = group.length; i < len;  i++ )
        group[ i ].checked = false;
    }
    .
    .
    .
    <input type='checkbox'  name='boxName[]' onclick="uncheck_all(this.form, this.name)">
    Y_U U_G_A_E_U_ B_S_A_D_

  3. #3
    Join Date
    Nov 2002
    Posts
    4,473
    I find it hard to believe that someone would depricate "NAME". It is rather necessary in a form to have a name for a field. Having said that, browsers will compensate (rather badly sometimes) for a lack of "NAME". If "ID" is present without "NAME", it uses that as if it was a "NAME".

    However, you will not be able to use "ID" like you can "NAME", because there can only be one object with a given "ID", while you can have as many things as you want with the same "NAME". So, your syntax using the "[...]" is invalid for an "ID".

  4. #4
    Join Date
    Sep 2006
    Posts
    11
    Hi guys, thanks for the responses. I just figured this one out on my own and came back to post m solution. The getElementById works fine, i just had the syntax wrong.

    Code:
    function uncheck_all(box_name)
    {
    	if ( eval("document.getElementById('search_form')['"+box_name+"[]'][0].checked==true") ){
    		for (i=1;i<eval("document.getElementById('search_form')['"+box_name+"[]'].length"); i++){
    			eval("document.getElementById('search_form')['"+box_name+"[]']["+i+"].checked=false");	
    		}						
    	}
    }
    Gil, the name field is depreciated in xhtml. HTML 4.01 added the NAME attribute, which wasn't present in HTML 4.0, and then XHTML took it away again.

    Thanks again for your time,
    -Scott

  5. #5
    Join Date
    Jun 2007
    Posts
    667
    Quote Originally Posted by Jack.Straw View Post
    Code:
    function uncheck_all(box_name)
    {
    	if ( eval("document.getElementById('search_form')['"+box_name+"[]'][0].checked==true") ){
    		for (i=1;i<eval("document.getElementById('search_form')['"+box_name+"[]'].length"); i++){
    			eval("document.getElementById('search_form')['"+box_name+"[]']["+i+"].checked=false");	
    		}						
    	}
    }
    I think that gets this week's prize for unnecessary use of eval.
    Y_U U_G_A_E_U_ B_S_A_D_

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles