www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 07-27-2005, 03:17 AM
    softengilker softengilker is offline
    Registered User
     
    Join Date: Jul 2005
    Location: İstanbul/Turkey
    Posts: 4
    getElementByName

    I am trying to get a check box element by its name with the code below:

    var element = document.Form1.getElementByName( 'myRepeater:_ctl' + i + ':myProjectCheck' );

    But this code gives error. Where is the fault? Is there any other way of it?
    Reply With Quote
      #2  
    Old 07-27-2005, 03:24 AM
    Kor's Avatar
    Kor Kor is offline
    Red Devil Moderator
     
    Join Date: Dec 2003
    Location: Bucharest, ROMANIA
    Posts: 11,142
    the correct syntax is getElementsByName(name) and it returns a collection of elements with the same name, so that furthermore, the reference by index is necessary.

    More than that, there is no need to specify the form as reference.

    document.getElementsByName( 'elementname')[0];

    and if there are more elements with the same name
    document.getElementsByName( 'elementname')[1];
    document.getElementsByName( 'elementname')[2];
    ....
    Reply With Quote
      #3  
    Old 12-29-2006, 12:29 PM
    duke_okc duke_okc is offline
    Registered User
     
    Join Date: Nov 2006
    Posts: 137
    getElementsByName()

    I remember reading somewhere that the name attribute is only valid for a limited set of HTML elements and therefore getElementsByName() is only applicable to those elements. Can someone tell me which elements are those please? I am really intrested to know if getElementsByName() would work for radio element but it would be nice to know the entire list. Thanks.
    Reply With Quote
      #4  
    Old 12-29-2006, 12:37 PM
    toicontien's Avatar
    toicontien toicontien is offline
    er, I mean toicantien
     
    Join Date: Feb 2003
    Location: Chicago Area, IL
    Posts: 5,434
    The name attribute really only applies to form controls, like INPUT, SELECT, BUTTON and TEXTAREA. If you want to get one specific tag, the use document.getElementById('foo'), that's assuming you give your form controls unique IDs too. You can also do something like below:
    HTML Code:
    <input type="text" name="txt1" onclick="doSomething(this);">
    <input type="text" name="txt2" value="Yeah buddy!!!">
    <script type="text/javascript">
    function doSomething(obj) {
      // The obj variable in this case refers to an INPUT element. All form elements have
      // a javascript property called form, which is a node reference to the FORM element
      // that contains the INPUT element. The FORM element then has a javascript
      // property called elements that is an integer indexed array of all the FORMs controls,
      // and also an associative array of those FORM controls where the control name is
      // the index you use to refer to the control.
      
      alert(obj.form.elements['txt2'].value);
    }
    </script>
    Reply With Quote
      #5  
    Old 12-29-2006, 12:52 PM
    duke_okc duke_okc is offline
    Registered User
     
    Join Date: Nov 2006
    Posts: 137
    Quote:
    Originally Posted by toicontien
    The name attribute really only applies to form controls, like INPUT, SELECT, BUTTON and TEXTAREA.
    I am having trouble accessing the input type radio with getElementsByName. I thought the input type radio is part of form control? If thats the case, then shoud I be able to use getElementsByName or not. Lets say, on a form, I have an html code like this:

    Code:
    <p class="text_boxes">
       <fieldset id="areaFieldSet"><legend id="areaLegend">Select one of the following</font></legend>
       <label class="labels"><input name="radArea" id="radArea" type="radio"
        onclick="areaSelection('WholeState');"/>Whole State</label>
       <label class="labels"><input name="radArea" id="radArea" type="radio"
        onclick="areaSelection('Area');"/>Area</label>
       <label class="labels"><input name="radArea" id="radArea" type="radio"
        onclick="areaSelection('County');"/>County</label>
         </fieldset>
       </p>
    I try to access it like this:
    document.getElementsByName('radArea')[0];
    document.getElementsByName('radArea')[1]; etc...
    but it does not work?.....
    Reply With Quote
      #6  
    Old 12-29-2006, 01:21 PM
    _Aerospace_Eng_'s Avatar
    _Aerospace_Eng_ _Aerospace_Eng_ is offline
    CSS Master (somewhat)
     
    Join Date: Feb 2005
    Posts: 1,018
    I'm not sure how you are accessing the radios. It sounds like you are trying to access them even before the page is loaded. This works fine
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    window.onload = function()
    {
    	var radios = document.getElementsByName('radArea');
    	for(var i = 0; i < radios.length; i++)
    	{
    		alert(radios[i].onclick);
    	}
    }
    </script>
    </head>
    <body>
    <form>
    	<fieldset id="areaFieldSet">
    	<p class="text_boxes">
    		<legend id="areaLegend">Select one of the following</legend>
    		<label class="labels">
    		<input name="radArea" type="radio"
        onclick="areaSelection('WholeState');"/>
    		Whole State</label>
    		<label class="labels">
    		<input name="radArea" type="radio"
        onclick="areaSelection('Area');"/>
    		Area</label>
    		<label class="labels">
    		<input name="radArea" type="radio"
        onclick="areaSelection('County');"/>
    		County</label>
    	</p>
    	</fieldset>
    </form>
    </body>
    </html>
    Also a fieldset can't go in a paragraph.
    __________________
    If you can't handle the job then don't take the job or ask for help on it if you are getting paid for it.
    Web Developer's Handbook.::.Why Tables for Layout is Stupid?.::.Why I won't help you
    Reply With Quote
      #7  
    Old 12-29-2006, 01:53 PM
    duke_okc duke_okc is offline
    Registered User
     
    Join Date: Nov 2006
    Posts: 137
    Quote:
    Originally Posted by _Aerospace_Eng_
    I'm not sure how you are accessing the radios. It sounds like you are trying to access them even before the page is loaded.
    Thanks for replying. I am accessing the radios after the page is loaded. What I really wanted to do was to check and see if radio button was selected or not. I was trying to do it like this:

    Code:
    var e=document.getElementByName('radDat');
            if  !( e[0].checked == true ){
           //do something;
          //rest of the code;
          }
    Does it support checked?

    Last edited by duke_okc; 12-29-2006 at 01:56 PM.
    Reply With Quote
      #8  
    Old 12-29-2006, 03:12 PM
    felgall's Avatar
    felgall felgall is offline
    Computer Consultant
     
    Join Date: Mar 2005
    Location: Sydney, Australia
    Posts: 7,979
    var e=document.getElementsByName('radDat');
    if (!e[0].checked){
    // first radio button is not checked ;
    }
    Reply With Quote
      #9  
    Old 12-29-2006, 05:18 PM
    duke_okc duke_okc is offline
    Registered User
     
    Join Date: Nov 2006
    Posts: 137
    Thank you guys for all the suggestions! I will work on this when I get back from 4 days weekend. See you guys next year....Happy New Year everyone...
    Reply With Quote
      #10  
    Old 12-29-2006, 05:23 PM
    konithomimo konithomimo is offline
    Registered User
     
    Join Date: Jan 2005
    Posts: 3,071
    Quote:
    Originally Posted by softengilker
    I am trying to get a check box element by its name with the code below:

    var element = document.Form1.getElementByName( 'myRepeater:_ctl' + i + ':myProjectCheck' );

    But this code gives error. Where is the fault? Is there any other way of it?
    As Kor said, it seems you are meaning to use getElementsByName(). There is a function called getElementByName(), but it does not work as intended in most browsers. It works properly in IE, but in better browsers, such as Mozilla, it will search both the name and ID attributes of each element, which can result in you getting the wrong element.
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 12:18 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.