Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    66
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Retrieving CheckBox Values with function Argument

    Every time I try to use a text box to trigger a function in Javascript I run into the same problem:

    Code:
    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:

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

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    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

  • #3
    New Coder
    Join Date
    Aug 2006
    Posts
    66
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by angst View Post
    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?

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    getElementById() should work fine. Look
    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></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
    Code:
    document.forms[0].inputID.checked
    What you could do is this
    Code:
    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');
    	}
    }
    Last edited by _Aerospace_Eng_; 09-08-2006 at 09:15 PM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    New Coder
    Join Date
    Aug 2006
    Posts
    66
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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.
    Last edited by ruggeddesign; 09-08-2006 at 10:07 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •