Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data not passing to function.

Status
Not open for further replies.

PSFMIS

MIS
Feb 3, 2006
28
US
Could someone tell me why my variable ragenttxt does not pass to the funcion? Or maybe it as and I'm just not doing somthing else right.


Text Field and Checkbox
Code:
<input name="ragenttxt" type="text" size="30">
          <input name="ragentna" type="checkbox" value="NA" onClick="javascript:ChkBoxTxtDisable('ragenttxt')">

JavaScript Function
Code:
<script Language="JavaScript">
function ChkBoxTxtDisable(TxtBoxName){
		if(document.MainForm.ragentna.checked)
			{
			document.MainForm.TxtBoxName.disabled=true;
			document.MainForm.TxtBoxName.value='';
			document.MainForm.TxtBoxName.className='TxtDisabled';
			}
		else
			{
			document.MainForm.TxtBoxName.disabled=false;
			document.MainForm.TxtBoxName.className='TxtEnabled';
			}
}
</script>
 
Nevermind I just got it.... I'm new to javasript and I was not call the form correctly.

Code:
<script Language="JavaScript">
function ChkBoxTxtDisable(TxtBoxName){
		if(document.MainForm.ragentna.checked)
			{
			document.getElementById(TxtBoxName).disabled=true;
			document.getElementById(TxtBoxName).value='';
			document.getElementById(TxtBoxName).className='TxtDisabled';
			}
		else
			{
			document.getElementById(TxtBoxName).disabled=false;
			document.getElementById(TxtBoxName).className='TxtEnabled';
			}
}
</script>
 
You're still not doing it right. You're passing an element name, and then using getElementBy[!]Id[/!] to try to point to the element.

This might well work in IE with its sloppy error checking, but I doubt it would work in any other browser.

I suggest this instead:

Code:
<script type="text/javascript">
<!--

	function ChkBoxTxtDisable(TxtBoxName) {
		var frm = document.forms['MainForm'].elements;
		if(document.MainForm.ragentna.checked) {
			frm[TxtBoxName].disabled = true;
			frm[TxtBoxName].value = '';
			frm[TxtBoxName].className = 'TxtDisabled';
		} else {
			frm[TxtBoxName].disabled = false;
			frm[TxtBoxName].className = 'TxtEnabled';
		}
	}

//-->
</script>

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Now I realize I want to pass both the Check Box Name and the Field Name into that function. Could you output your example again if I was to call the function this way?

ragentna being the name of the CheckBox.
ragenttxt being the name of the TextField.

Code:
<input name="ragenttxt" type="text" size="30">
          <input name="ragentna" type="checkbox" value="NA" onClick="javascript:ChkBoxTxtDisable('ragentna','ragenttxt')">

I tried this but it doesn't work.
Code:
<script type="text/javascript">
<!--

    function ChkBoxTxtDisable(ChkBoxName,TxtBoxName) {
        var frm = document.forms[ChkBoxName].elements;
        if(document.MainForm.ragentna.checked) {
            frm[TxtBoxName].disabled = true;
            frm[TxtBoxName].value = '';
            frm[TxtBoxName].className = 'TxtDisabled';
        } else {
            frm[TxtBoxName].disabled = false;
            frm[TxtBoxName].className = 'TxtEnabled';
        }
    }

//-->
</script>
 
you are using the checkbox name when trying to get the form! i think you want something like this:

Code:
<script type="text/javascript">
<!--

    function ChkBoxTxtDisable(ChkBoxName,TxtBoxName) {
        var frm = document.forms['MainForm'].elements;
        if(frm[ChkBoxName].checked) {
            frm[TxtBoxName].disabled = true;
            frm[TxtBoxName].value = '';
            frm[TxtBoxName].className = 'TxtDisabled';
        } else {
            frm[TxtBoxName].disabled = false;
            frm[TxtBoxName].className = 'TxtEnabled';
        }
    }

//-->
</script>



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
cLFlaVA,

I get an error with that one...

Line 25
Chr 13
Error: 'Undefined' is null or not an object.

Hmm .. it looks like it should work...


-Aaron
 
OH MY ... I had undone my changes so calling the function was not passing both values... Now it works great.

Listed below in working order.

HTML
Code:
<input name="sragenttxt" type="text" size="30">
          <input name="sragentna" type="checkbox" value="NA" onClick="javascript:ChkBoxTxtDisable('sragentna','sragenttxt')">

FUNCTION
Code:
<script type="text/javascript">
<!--
    function ChkBoxTxtDisable(ChkBoxName,TxtBoxName) {
        var frm = document.forms['MainForm'].elements;
        if(frm[ChkBoxName].checked) {
            frm[TxtBoxName].disabled = true;
            frm[TxtBoxName].value = '';
            frm[TxtBoxName].className = 'TxtDisabled';
        } else {
            frm[TxtBoxName].disabled = false;
            frm[TxtBoxName].className = 'TxtEnabled';
        }
    }
//-->
</script>

Thanks for all the help guys!

-Aaron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top