...

View Full Version : document.getElementByName?



nAzGiRl2005
09-15-2005, 07:54 PM
I'm trying to use this function in my javascript code, to get the value of a form element, by its name.
but the javascript console tells me that this function does not exist.
how does this work?
please let me know.

Canuck WebGeek
09-15-2005, 07:59 PM
it's [document.]getElementsByName (note the 's'). It returns an array of elements with that name, if any.

You'd be better off giving your form elments id attributes and usung the getElementById call instead.

martin_narg
09-15-2005, 08:00 PM
Could you post up your code please, in
tags like this:


.... paste your code here


many thanks

m_n

nAzGiRl2005
09-15-2005, 08:17 PM
I have a page, that contains a textarea and a bunch of <select> options.
I also have javascript code. As I type keywords on my textarea, the <select> values that match it, get highlighted, which is what the javascript code does.

Here's the javascript code and html code in one page.
in my doSelection function, I had all the form elements hardcoded with their specific names. Right now, I want to upgrade my code so that I don't hardcode the names in the javascript code anymore. so I want to pass the names from the html code to the parameters in my javascript code, to make it usable for any code.
I have the first two parameters figured out, but I'm not sure how to pass the last parameter, match_type.
Please help me out.


<SCRIPT LANGUAGE="javascript" TYPE="text/javascript">
<!--


function doSelection (typedText, selectControl, match_type)
{
// control is the textbox control
//var typedText = control.value;
//var selectControl = document.getElementById("metricsSelect");
//when keyword starts with the typed text
if(typedText=="")
{
for (var i=0; i<selectControl.options.length; i++)
{
if(selectControl.options[i].selected == true)
{
selectControl.options[i].selected = false;
}
}
}
else
{
//if(document.getElementById("keywordSelect").checked)
if (match_type == "match_begin")
{

for (var i=0; i<selectControl.options.length; i++)
{
if(selectControl.options[i].text.indexOf(typedText)==0)
{
selectControl.options[i].selected= true;
}
else
{
selectControl.options[i].selected = false; // unselects non-matches
}

}
}
//when keyword contains the typed text
//if(document.getElementById("keywordSelect2").checked)
if (match_type == "match_contain")
{

for (var i=0; i<selectControl.options.length; i++)
{
//if (typedText!=null && typedText==selectControl.options[i].text.substring(0,typedText.length+1))
if(selectControl.options[i].text.indexOf(typedText)>=0)
{
selectControl.options[i].selected= true;
}
else
{
selectControl.options[i].selected = false; // unselects non-matches
}
}
}
//when keyword ends with the typed text
//if(document.getElementById("keywordSelect3").checked)
if (match_type == "match_end")
{

for (var i=0; i<selectControl.options.length; i++)
{
var pos = selectControl.options[i].text.indexOf(typedText);
if(selectControl.options[i].text.substring(pos)==typedText)
{
selectControl.options[i].selected= true;
}
else
{
selectControl.options[i].selected = false; // unselects non-matches
}
}
}
}


}

//-->
</SCRIPT>

<html>
<body>
<title> keyword filtering </title>
<form name="myform">
<tr>

<span>
<input type="checkbox" name="metricsSelect" value="match_begin" class="checkbox" id="keywordSelect"> starts with
&nbsp&nbsp<input type="checkbox" name="metricsSelect" value="match_contain" class="checkbox" id="keywordSelect2"> contains
&nbsp&nbsp<input type="checkbox" name="metricsSelect" value="match_end" class="checkbox" id="keywordSelect3"> ends with
&nbsp
<input type="text" name="metrics" size="40" value=""
onkeyup="doSelection(this.value, document.getElementById('metricsSelect'), this.form.metricsSelect.value);" style="query-table-item" id="metricsTxt">
</span>
<br><br>
<td>
<select name="metrics" multiple="multiple" size="5" style="width:450" id="metricsSelect">
<option value="100000018">approved</option>
<option value="100000013">auto_purchase</option>
<option value="100000035">auto_purchase_external</option>
<option value="100000033">auto_purchase_internal</option>
<option value="100000070">auto_purchase_internal_approved</option>
<option value="100000245">auto_purchase_lease</option>
<option value="100000246">auto_purchase_lease_external</option>
<option value="100000248">auto_purchase_lease_internal</option>
<option value="100000247">auto_purchase_lease_internal_approved</option>
<option value="100000239">auto_purchase_private</option>
<option value="100000240">auto_purchase_private_external</option>
<option value="100000242">auto_purchase_private_internal</option>
<option value="100000241">auto_purchase_private_internal_approved</option>
</br></br>
</form>
</body>
</html>

nAzGiRl2005
09-15-2005, 11:08 PM
ok, I've figured it out.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum