Click to See Complete Forum and Search --> : using getElementsByName


webtekie
07-25-2003, 09:19 AM
Hello,

I found this DHTML object that allows regular comboBox to be searchable. Script also submits values for the form to itself. However, I can't get a new field that I've added (text field) to submit its value -- it's always empty. Any help is appreciated.

here is the code:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<title>ComboBox Form Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script src="include/ComboBox.js"></script>
<script>
function go(){
document.getElementsByName('combobox')[0].value = poShipVia.value;
document.getElementsByName('field').value = testfield.value;
document.getElementById('test-form').submit();
}
</script>
<form id="test-form">
<input type="hidden" name="combobox"/>
<input type="hidden" name="field"/>
</form>
<p>Select shipping type:</p>
<p id="shipviacell">
<script type="text/javascript" language="JavaScript">
var poShipVia = new ComboBox("poShipVia", document.getElementById("shipviacell"));
poShipVia.add(new ComboBoxItem("FedEx","FedEx"));
poShipVia.add(new ComboBoxItem("UPS","UPS"));
poShipVia.add(new ComboBoxItem("US Postal Service","US Postal Service"));
</script>
</p>
<br>
<p id="testfield">
<input type="text" name="testfield">
</p>
<br>
<br>
<p>
<button onclick="javascript:go()">Submit</button>
</p>
</body>
</html>




P.S. The source code for enitre object is here http://webfx.nu/dhtml/combobox/

Khalid Ali
07-25-2003, 09:25 AM
Originally posted by webtekie

<script>
function go(){
document.getElementsByName('combobox')[0].value = poShipVia.value;
document.getElementsByName('field').value = testfield.value;
document.getElementById('test-form').submit();
}

This line of code

document.getElementsByName('field').value = testfield.value;
is in correct.
document.getElementsByTagName returns a list of elements sharing the same name,to access itemin the list you have to go through the array and do the processingon any element.the code above should have been something like this


document.getElementsByName('field')[0].value = testfield.value;


thats the obvious error..fix it and let us know if you still have probs

webtekie
07-25-2003, 09:40 AM
thanks.

I changed the code to document.getElementsByName('field')[0].value = testfield.value; as you said, but now in location bar I get &field=underfined

Charles
07-25-2003, 09:46 AM
I don't believe that there is a document.getElementByName() method. Just use document.getElementById().

There is, however, a document.getElementsByTagName() that returns an array, but it's not what you want. See http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-core.html.

webtekie
07-25-2003, 09:48 AM
it's not getElementByName, it's getElementsByName.
Also, if I use getElementById(), I still get field=undefined.

Charles
07-25-2003, 09:50 AM
There isn't one of those either. Think about it. Why, if you can only have one element with any given name, would you have a method that returns an array?

webtekie
07-25-2003, 09:53 AM
ok, I got it.
I named my <p> with same name as input field, so I guess that caused the problem.
Thanks guys.

Charles
07-25-2003, 09:55 AM
I was, however, wrong. There is a getElementsByName method that returns an array. It's in chapter two of the DOM (http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-html.html). Still, it seems a little odd.

Khalid Ali
07-25-2003, 10:08 AM
Originally posted by Charles
[font=georgia]... There is a getElementsByName method that returns an array. ....

Good ...quick reply charles...I was getting ready for anohter bout..:D