Forum Moderators: open

Message Too Old, No Replies

ajax radio buttons getElementById

         

fintan

4:02 pm on Apr 11, 2006 (gmt 0)

10+ Year Member



I have a form with 3 radio buttons. The form works fine but for the radio buttons. My javascript isn't picking up the values. Can anyone see where I'm going wrong?
The document.getElementById("Type").value; always returns 0. Thanks in advance

fintan.


function callServer() {
// Get the Search field and search type from the web form
var getStaff = document.getElementById("getStaff").value;
var Type = document.getElementById("Type").value;

<input id="Type" name="Type" type="radio" checked="checked" value="0" onclick="callServer();"> All

<input id="Type" name="Type" type="radio" value="1" onclick="callServer();"> Forename, Surname

<input id="Type" name="Type" type="radio" value="2" onclick="callServer();"> Organisational Unit

daosmith

4:10 pm on Apr 11, 2006 (gmt 0)

10+ Year Member



Maybe you are having this problem because each id tag should be unique whereas you have three radio elements all with the same id tag? I would have thought that this wouldn't be an issue for the submitted form data, but it might be worth checking.

fintan

4:23 pm on Apr 11, 2006 (gmt 0)

10+ Year Member



Thanks for the reply, if the names are different they you won't be able to select between them.

daosmith

4:31 pm on Apr 11, 2006 (gmt 0)

10+ Year Member



Yes of course the name attributes have to be the same, but (maybe) the ids are not allowed to be. You won't be able to do a simple getElementById() call then but you could traverse the submitted form data structure and check each item's name attribute e.g.

var formData = form.childNodes; // I have no idea how submitted form data is structured but I'm sure it's possible to somehow access an array of all submitted fields.
var currFormElem = null;

for (var i = 0; i < formData.length; i++) {

currFormElem = formData[i];

if (currFormElem.getAttribute("name") == "Type")
// Do whatever
}

A lot more verbose than a getElementById() call but feasible - and you could wrap all your other form handling in the same for loop if you were so inclined.

But again, this is all based on the supposition that it is the multiple id attributes that are causing the error.

fintan

7:02 pm on Apr 11, 2006 (gmt 0)

10+ Year Member



Thanks daosmith I'll give a shot in the morning and see how it goes.

fintan.