...

View Full Version : Retrieving CheckBox Values with function Argument



ruggeddesign
09-08-2006, 04:24 PM
Every time I try to use a text box to trigger a function in Javascript I run into the same problem:



function checKer(inputID) // where inputID = checkbox
{
document.forms[0].inputID.checked // does not work
if(document.getElementById(inputID).checked == true) {} // does not work
var x = document.getElementById(inputID).value // returns null value
}


It seems that you can't use a function's argument/variable in a document.forms[0]... line (this method does seem to work when you hard-code the checkbox name into the line). And with the lack of success I'm having with the getElementById() methods I am hoping that one of you will tell me if there's a better way?!

I thought it might help to know how I'm setting up my checkboxes as well, like this:



<input name="testcheck" type="checkbox" id="testcheck" value="" onClick="checKer('testcheck');"/>


Yar.

angst
09-08-2006, 05:17 PM
hmm,
I'm no javascript pro,
but it seems to me that you need to add the checkbox number.

like:
document.forms[0].inputID[0].checked

ruggeddesign
09-08-2006, 05:45 PM
hmm,
I'm no javascript pro,
but it seems to me that you need to add the checkbox number.

like:
document.forms[0].inputID[0].checked

Well... let me explain a bit better, this line would work:

if(document.forms[0]."SalesCheck".checked) { do something; }
- where "SalesCheck" is the id of one of my checkboxes.

Now, essentially what I am doing is passing the name of the input ("SalesCheck") to my function as the argument: "inputID". (If I alert(inputID) it would alert: "SalesCheck") - but when I type:

document.forms[0].inputID.checked

the script fails. JavaScript Console in FF tells me that document.forms[0].inputID has no properties - I think this means the line doesn't recognize the use of the functions argument in this type of line.

Is my problem any more clear?

_Aerospace_Eng_
09-08-2006, 08:10 PM
getElementById() should work fine. Look

<!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></title>
<script type="text/javascript">
function checKer(inputID) // where inputID = checkbox
{
var x = document.getElementById(inputID);
if(x.checked == true)
{
alert('Is checked and has a value of x = ' + x.value);
}
else
{
alert('Is not checked');
}
}
</script>
</head>
<body>
<form>
<input name="testcheck" type="checkbox" id="testcheck" value="this is the value" onclick="checKer('testcheck');"/>
</form>
</body>
</html>
Of course X is going to return null because value is null unless you put stuff in it like I did in the example. This line is expecting a name not an ID

document.forms[0].inputID.checked
What you could do is this

function checKer(inputID) // where inputID = checkbox
{
var x = document.forms[0].elements[inputID];
if(x.checked == true)
{
alert('Is checked and has a value of x = ' + x.value);
}
else
{
alert('Is not checked');
}
}

ruggeddesign
09-08-2006, 09:03 PM
The first example didn't seem to work for me when I applied the concept to my page - well actually, as a suprise to me, it WAS working in IE but failed in FF. I realize that this isn't because of the concept you illustrated because that sample code you provided runs perfectly in a test file on my server but I have no idea why it won't work for me in my file.

Either way, it's all good. No reason to worry over checkboxes when it's working now with your second example.

Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum