Get variable from javascript function

Results 1 to 3 of 3

Thread: Get variable from javascript function

  1. #1
    Join Date
    Dec 1969

    Default Get variable from javascript function

    I have a javascript function that I want to pass the name of the checkbox that called it. I then want to use that name to determine if the checkbox is checked. Here is my function:<BR>&#060;script language="JavaScript"&#062;<BR>function seeIfChecked(boxName)<BR>{<BR> if (document.formRes.boxName.checked = true)<BR> {<BR> checkedflag = "Y";<BR> } <BR> alert("yes its checked");<BR>}<BR>&#060;/script&#062;<BR><BR>When I hardcode the checkbox name instead of boxName in this line: if (document.formRes.boxName.checked = true)<BR>the function performs as expected. How do I pass boxName from this line: function seeIfChecked(boxName) to this line if (document.formRes.boxName.checked = true)? Thanks

  2. #2
    Join Date
    Dec 1969

    Default RE: Get variable from javascript function

    if (document.formRes[boxName].checked = true)<BR><BR>Craig.

  3. #3
    Join Date
    Dec 1969

    Default Or you could even pass the checkbox...

    ...itself through to the function. In the checkbox:<BR><BR>seeIfChecked(this);<BR><BR>Then in the function:<BR><BR>function seeIfChecked(box)<BR>{ <BR>if (box.checked) <BR>{ <BR>checkedflag = "Y"; <BR>} <BR>alert("yes its checked"); <BR>} <BR><BR>Also, your script is a bit wrong:<BR><BR>if (document.formRes.boxName.checked = true)<BR><BR>This sets the checked status of the box and doesn&#039;t check if it&#039;s checked. You want this:<BR><BR>if (document.formRes.boxName.checked == true)<BR><BR>Or rather, you want what ReaperMan suggested, but with two equal signs:<BR><BR>if (document.formRes[boxName].checked == true)<BR><BR>And your alert() box comes up all the time, whether the checkbox is checked or not. Don&#039;t you want to have the alert inside the if statement?<BR><BR>Oliver.<BR>

Posting Permissions

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