/    Sign up×
Community /Pin to ProfileBookmark

loosing variable

hello,

i have a little problem with a javascript
the function is named Calculate

in the form I call this function with:
onChange=”Calculate(this.id)”

in the function it all works fine except the focus.
in the alert line it seems to be that he knows that the value of ‘test’
is comming from the form ‘this.id’

but in the line below ‘test’ seems to be no longer a variable
and so I get the error that ‘document.CapExp.test is null or not an object’
how can I get the same value as in the alert line.

function Calculate(test)
{
alert(test);
document.CapExp.test.focus();
}

regards,
bart

to post a comment
JavaScript

15 Comments(s)

Copy linkTweet thisAlerts:
@AdamBrillJul 23.2003 — Hmm... Try changing this line:

document.CapExp.test.focus();

to this:

eval("document.CapExp."+test+".focus();");

I think that should fix the problem. ?
Copy linkTweet thisAlerts:
@bartlauthorJul 23.2003 — Adambrill,


thank you for your help, but I tried this already and I tried it now once more but it still doesn't work.

I don't get an error message anymore but that's all, the pointer just goes to the next formfield after showing the alert.


regards,

bart
Copy linkTweet thisAlerts:
@AdamBrillJul 23.2003 — Do some of your elements have the same ID? If so, that might be your problem... If that's not the problem, post your page so I can look at the code.
Copy linkTweet thisAlerts:
@bartlauthorJul 23.2003 — adambrill,

i've pasted my page below,

it is a page written in coldfusion.

there is a query with after that the output of the result.

In this result i do another query which I output on his turn and which has as result the formfield.

I'll hope you can help me and many thanks in advance,

bart



<cfquery name="qAllocDiv" DATASOURCE="DBIntra"
username="#Decrypt(URLDecode(Session.DtbaseCon[t][2]), Session.key)#"
password="#Decrypt(URLDecode(Session.DtbaseCon[t][3]), Session.key)#">
SELECT *
FROM WW_DivPrdLin
WHERE DivPrdLinID > 99999
ORDER BY DivPrdLinDes
</cfquery>



<table border="0" cellpadding="0" cellspacing="0" style="width : 630px ;">


<cfloop query="qAllocDiv">

<tr>

<td <!--- class="Label" ---> colspan="5" class="LabelTitle">

<span style="text-align : left ; width : 250px ; line-heigth : 20px ; vertical-align : top ; "><cfoutput>#qAllocDiv.DivPrdLinDes#</cfoutput></span>

<span style="text-align : right ; width : 360px ; line-heigth : 20px ; vertical-align : top ; ">Total : <input type="text" name="Total<cfoutput>#qAllocDiv.DivPrdLinID#</cfoutput>" size="8" disabled value="0" class="InputDisabled"> </span>

<cfset whichPrdLin = #qAllocDiv.DivPrdLinID#>
</td>
</tr>


<cfquery name="qAllocPrdLin" DATASOURCE="DBIntra"
username="#Decrypt(URLDecode(Session.DtbaseCon[t][2]), Session.key)#"
password="#Decrypt(URLDecode(Session.DtbaseCon[t][3]), Session.key)#">
SELECT *
FROM WW_DivPrdLin
WHERE DivPrdLinID like left(#qAllocDiv.DivPrdLinID#, 1) + '%'
AND DivPrdLinID < 99999
ORDER BY DivPrdLinDes
</cfquery>

<cfoutput query="qAllocPrdLin">
<cfif #qAllocPrdLin.CurrentRow# Mod 2>


<!--- <script language="JavaScript">

var test = 'prdlin#qAllocPrdLin.DivPrdLinID#'

</script> --->

<tr>

<td class="Label">#DivPrdLinDes# :</td>

<td class="Input">

<input type="text" id="prdlin#qAllocPrdLin.DivPrdLinID#" name="prdlin#qAllocPrdLin.DivPrdLinID#" size="8" value="0" onChange="Calculate#whichPrdLin#(this.id)" onKeypress="if ((event.keyCode > 0 && event.keyCode < 46) || (event.keyCode > 46 && event.keyCode < 48) || (event.keyCode > 57 && event.keyCode < 255)) event.returnValue = false;">

</td>

<td class="WhiteSpace">&nbsp;</td>

<cfelse>

<!--- <script language="JavaScript">

var test = 'prdlin#qAllocPrdLin.DivPrdLinID#'

</script> --->

<td class="Label">#DivPrdLinDes# :</td>

<td class="Input">

<input type="text" id="prdlin#qAllocPrdLin.DivPrdLinID#" name="prdlin#qAllocPrdLin.DivPrdLinID#" size="8" value="0" onChange="Calculate#whichPrdLin#(this.id)" onKeypress="if ((event.keyCode > 0 && event.keyCode < 46) || (event.keyCode > 46 && event.keyCode < 48) || (event.keyCode > 57 && event.keyCode < 255)) event.returnValue = false;">

</td>

</tr>

</cfif>

</cfoutput>


<!--- <cfset testcfm = "prdlin5001"> --->

<script language="JavaScript">

<!-- hide for browser

function Calculate<cfoutput>#qAllocDiv.DivPrdLinID#</cfoutput>(test)

{

result = (0 <cfoutput query="qAllocPrdLin"> + parseFloat(document.CapExp.prdlin#qAllocPrdLin.DivPrdLinID#.value)</cfoutput>);

if(result > 0 && result <= 100)

{

document.CapExp.Total<cfoutput>#qAllocDiv.DivPrdLinID#</cfoutput>.value = result;

}

else if(result > 100)

{

//var test2 = test.substring(6,10)

alert('The sum of a Product Line can't be bigger than 100% ' + test);

//document.CapExp.test.focus();

eval("document.CapExp."+test+".focus();");

//document.CapExp.test.select();

//document.CapExp.prdlin3006.focus();

//document.forms['CapExp'].[test].focus();

//eval("document.CapExp." + test + ".focus()");

// return false;

}

else

{

alert(result)

}

// return true;

}

//-->

</script>



<!--- white line --->
<tr>
<td <!--- class="Label" ---> colspan="5">&nbsp;</td>
</tr>


</cfloop>

<tr>
<td <!--- class="Label" ---> colspan="5" class="LabelTitle">
<span style="text-align : left ; width : 250px ; ">Market</span>
<span style="text-align : right ; width : 370px ; ">
Total : <input type="text" name="TotalMkt" size="8" disabled value="0" class="InputDisabled">
</span>
</td>
</tr>


<cfquery name="qAllocMkt" DATASOURCE="DBIntra"
username="#Decrypt(URLDecode(Session.DtbaseCon[t][2]), Session.key)#"
password="#Decrypt(URLDecode(Session.DtbaseCon[t][3]), Session.key)#">
SELECT *
FROM Market
Where MktSts = 'MK'
AND MktID <> '*'
ORDER BY MktDes
</cfquery>

<cfoutput query="qAllocMkt">
<cfif #qAllocMkt.CurrentRow# Mod 2>
<tr>
<td class="Label">#MktDes# :</td>
<td class="Input">
<input type="text" name="#qAllocMkt.MktID#" size="8" value="0" onChange="CalculateMkt(); return true;" onKeypress="if ((event.keyCode > 0 && event.keyCode < 46) || (event.keyCode > 46 && event.keyCode < 48) || (event.keyCode > 57 && event.keyCode < 255)) event.returnValue = false;">
</td>
<td class="WhiteSpace">&nbsp;</td>
<cfelse>
<td class="Label">#MktDes# :</td>
<td class="Input">
<input type="text" name="#qAllocMkt.MktID#" size="8" value="0" onChange="CalculateMkt(); return true;" onKeypress="if ((event.keyCode > 0 && event.keyCode < 46) || (event.keyCode > 46 && event.keyCode < 48) || (event.keyCode > 57 && event.keyCode < 255)) event.returnValue = false;">
</td>
</tr>
</cfif>

</cfoutput>



<script language="JavaScript">

<!-- hide for browser

<!--- calculate automaticaly the value off Total Market --->

function CalculateMkt()

{

result = (0 <cfoutput query="qAllocMkt"> + parseFloat(document.CapExp.#qAllocMkt.MktID#.value)</cfoutput>);

if(result > 0)

{

document.CapExp.TotalMkt.value = result;

}

else

{

alert(result)

}

}

//-->

</script>



</table>
Copy linkTweet thisAlerts:
@skriptorJul 23.2003 — Hi,

I don't know coldfusion, but what do you think about (refering to your first mail):

onChange="Calculate(this)"

function Calculate(test)

{

alert(test.id);

document.CapExp.test.focus();

}

Good luck, skriptor
Copy linkTweet thisAlerts:
@SlankenOgenJul 23.2003 — document.CapExp[test].focus();
Copy linkTweet thisAlerts:
@bartlauthorJul 23.2003 — Hello skriptor,

Or I don't see it or I don't see many difference with my code.

Can you tell me what you changed


thanks
Copy linkTweet thisAlerts:
@AdamBrillJul 23.2003 — Well, I don't know ColdFusion either(and from the looks of it, I never want to ?), but my guess is the problem is from these:

id="prdlin#qAllocPrdLin.DivPrdLinID#"

id="prdlin#qAllocPrdLin.DivPrdLinID#"

They are both the same, which is probably causing an error. When I run that page, I get a ton of errors because of all of the pound signs that are in there.... Does ColdFusion do something with those???

BTW, skriptor, if he would decide to do it with your method, it should be like this:

onChange="Calculate(this)"

function Calculate(test)

{

alert(test.id);

test.focus();

}

He is returning the element, so you shouldn't(and it wouldn't work if you did) have document.CapExp. before it. ?
Copy linkTweet thisAlerts:
@bartlauthorJul 23.2003 — the 2

id="prdlin#qAllocPrdLin.DivPrdLinID#"

in the code means every time an other value.

with the data I work I have 5 divisions with in each division an average of 10 productlines.

so for example the first time there is id=prdlin5001 and the second time it will be id=prdlin5002 and the third time...

and if i replace this code

document.CapExp.test.focus();

with this :

document.CapExp.prdlin5002.focus();

then it works

but I can't use this because prdlin5002 or test is the name of the form field that is filled in as last one and if i use prdlin5002 instead of test it should always jump to this one and not to last last filled in field

and test contains the variable name of the formfields
Copy linkTweet thisAlerts:
@AdamBrillJul 23.2003 — Did you try this code?onChange="Calculate(this)"

function Calculate(test){
alert(test.id);
test.focus();
}
I think that would be easier if you would do it like that...
Copy linkTweet thisAlerts:
@SlankenOgenJul 23.2003 — You can send the text box id with 'this.id' and focus it with the array operator "[]"


onChange="Calculate(this.id)"

function Calculate(test){

alert(test);

document.formName[test].focus();

}
Copy linkTweet thisAlerts:
@bartlauthorJul 23.2003 — i tried it and it doesn't work.

but i have attacht the htm file (change txt to htm) to my reply, then you can see the generated code from the coldfusion server, this you will understand better.

[upl-file uuid=b4e80450-cc31-48cb-9e40-acf82654588c size=95kB]file.txt[/upl-file]
Copy linkTweet thisAlerts:
@AdamBrillJul 23.2003 — Ok... I think I found the problem, and possibly even a fix for it. ? The problem is that when it is running(onchange), the element still has the focus. So when you set the focus, it isn't doing anything. Then right after the code runs, it loses the focus. I don't know if that makes sense, but I think you can fix it like this: Give the focus to something else first and send it back to the element. That would be done something like this:

Where you have:

test.focus();

Switch to:

document.getElementById('test').focus();

test.focus();

Switch the 'test' to some id that is on the page(not one of the two that you want focused). You might want to create a hidden element that doesn't do anything except fix this problem. That way you won't mess something else up by doing it. ? Kind of a stupid work-around, but if it works.... ?
Copy linkTweet thisAlerts:
@bartlauthorJul 24.2003 — OK people,

AdamBrill is the best.

I did it like he told me, changed a little bit till i got this code:

document.getElementById('CEPTtl').focus(); document.getElementById(test).focus();

and then it all worked fine

Thanks AdamBrill

?
Copy linkTweet thisAlerts:
@AdamBrillJul 24.2003 — No problem. I'm glad I could help. ?
×

Success!

Help @bartl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...