|
|||||||
| JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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?
|
|
#2
|
||||
|
||||
|
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]; .... |
|
#3
|
|||
|
|||
|
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.
|
|
#4
|
||||
|
||||
|
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>
__________________
My Blog: FundaMental Disaster Accessible DHTML Tabs | Quick CSS Positioning Explanation | Quick Floated Elements Explanation | 50% + 50% != 100% | Gaps Under Images | ID vs. Class | Gappy Lists in Internet Explorer | Why Tables Are Slower | Benefits of XHTML vs. HTML | Linking to External Style Sheets | About DOCTYPES | Web Design is a Peanut Butter & Jelly Sandwhich | CSS: To Hack Or Not To Hack | Internet Explorer and Transparent PNGs |
|
#5
|
|||
|
|||
|
Quote:
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>
document.getElementsByName('radArea')[0]; document.getElementsByName('radArea')[1]; etc... but it does not work?..... |
|
#6
|
||||
|
||||
|
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>
__________________
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 |
|
#7
|
|||
|
|||
|
Quote:
Code:
var e=document.getElementByName('radDat');
if !( e[0].checked == true ){
//do something;
//rest of the code;
}
Last edited by duke_okc; 12-29-2006 at 01:56 PM. |
|
#8
|
||||
|
||||
|
var e=document.getElementsByName('radDat');
if (!e[0].checked){ // first radio button is not checked ; }
__________________
Stephen Free Computer Help, blog, forum Web design ebooks and software JavaScript scripts and tutorials |
|
#9
|
|||
|
|||
|
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...
|
|
#10
|
|||
|
|||
|
Quote:
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|