views:

33

answers:

7

Trying to pass a form value into a global var to be use in two functions but get this error document.PostReply is undefined or the javascript just crashes because getReplyId is undefined within the functions. No matter what I have tried I get one or the other errors

Can anyone see why the form value "ReplyID" is not being passed into the var.
The var is placed at the begining of the script before any functions.

form

<CFFORM NAME="PostReply" ID="PostReply" METHOD="POST">
<CFINPUT TYPE="hidden" NAME="ReplyID" ID="ReplyID" VALUE="#ReplyID#">
</CFFORM>

what I have tryed
1 - var getReplyId = document.PostReply.ReplyID;
2 - var getReplyId = document.PostReply.ReplyID.value;
3 - var getReplyId = document.getElementById("ReplyID");
4 - var getReplyId = document.PostReply.getElementById("ReplyID");
5 - var getReplyId = document.getElementById("PostReply.ReplyID").value;

If I just do this var getReplyId = 1 the script works great, but I need the ReplyID value.

Ok here is everything for the most part

<CFOUTPUT>
<STYLE>
targetPostReply#ReplyID#{visibility : visible}
targetPostReplyResponse#ReplyID#{visibility : visible}
</STYLE>
</CFOUTPUT>

<cfajaxproxy cfc="CFC/PostReply" jsclassname="PostReplyCFC">

<SCRIPT SRC="js/PostReply.js" TYPE="text/javascript"> var getReplyId = document.getElementById("ReplyID").value;
$(document).ready(
function () {
$("form#PostReply").submit(
function PostNewReply(ReplyID, ReplyComment){
var cfc = new PostReplyCFC();
cfc.setCallbackHandler(getNewReply)
cfc.setForm('PostReply')
cfc.PostNewReply('ReplyComment');
document.getElementById("targetPostReplyResponse"+getReplyId).style.display='block';
document.getElementById("targetMakeReply"+getReplyId);
$('#targetMakeReply'+getReplyId).slideToggle("slow");
$(this).toggleClass("targetMakeReply"+getReplyId);
return false;
}); // .submit()
});
function getNewReply(newReply)
{
$('#replyResponse'+getReplyId).html(newReply);
return false;
}
</SCRIPT>

<CFOUTPUT>
<DIV ID="targetMakeReply#ReplyID#">
<CFFORM NAME="PostReply" ID="PostReply" METHOD="POST">
<CFINPUT TYPE="hidden" NAME="ReplyID" ID="ReplyID" VALUE="#ReplyID#">
<textarea name="ReplyComment" rows="5" cols="95"></textarea>
<CFINPUT type="image" name="Submit" id="submitButton" value="Post Reply" src="images/PostReply.gif" onmouseover="src='images/PostReplyO.gif'" onmouseout="src='images/PostReply.gif'">
</CFFORM>
</DIV>
</CFOUTPUT>

<CFOUTPUT>
<STYLE>##targetPostReplyResponse#ReplyID#{display : none}</STYLE>
<DIV ID="targetPostReplyResponse#ReplyID#">
<TABLE ALIGN="center" VALIGN="top" WIDTH="750" BGCOLOR="FFFFFF" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD COLSPAN="2" CLASS="text12B"><IMG SRC="images/blank.gif" WIDTH="1" HEIGHT="5" ALT="" BORDER="0"><BR><SPAN ID="replyResponse#ReplyID#"></SPAN></TD>
</TR>
</TABLE>
</DIV>
</CFOUTPUT>

A: 

To make a variable global, just leave away the var.

getReplyId = document.getElementById("ReplyID").value;

note though that global variables are often sub-optimal practice. If possible, consider having one function call the other and pass the variable as a parameter.

Pekka
Still getting error "document.getElementById("ReplyID") is null" from getReplyId = document.getElementById("ReplyID").value;
Kelly Kc Clements
@Kelly can you show the finished HTML instead of the CF source?
Pekka
Sounds like it's being run before the document has finished creating. You'll need to have the script run onLoad or using $(document).ready() function of jQuery (or similar functionality from other frameworks)
Cfreak
<SPAN ID="replyResponse#ReplyID#"></SPAN>
Kelly Kc Clements
will give it a try
Kelly Kc Clements
Script is in the head and it works if i hard code the ID in.
Kelly Kc Clements
@Kelly put the code in there. If there is an element with the ID `ReplyID`, it will work.
Pekka
@Kelly look in the HTML code whether an input with the ID `ReplyHidden` exists. If it does, it should work. Also, make sure the JS code is in the `ready()` block you posted
Pekka
Ok, put the JS code in the `ready()` block before the function (as i need it outside any function). Now get this "missing ) after argument list". Also went thought the HTML with FireBug and didnt see any input with the ID `ReplyHidden`. Should I Edit my question and add the complete JS?
Kelly Kc Clements
@Kelly you should probably add the JS and HTML, yes.
Pekka
Code is up for you to look at
Kelly Kc Clements
Thanks for your help Pekka. I'm still working it to see if I can do better so that the Form can have the ReplyID tag attached to the name and ID and pass it though the cfajaxproxy, so I can have as many Reply forms as I need (all with there own ReplyID). If you come up with what I am trying to do PLEASE let me know.
Kelly Kc Clements
@Kelly I see! I'm not familiar with CF so I don't know how to enumerate the forms. Maybe you need to use jQuery to get the element inside a specific form
Pekka
Passing the ID into cfajaxproxy is done though the JS. All I have to figure out is passing the ID before the `$("form#PostReply").submit()` part of the script and not after. Something like this `$("form#PostReply"+getReplyId).submit()`. Any ideas? Should I wrap the JS inside a function and call it SubmitReply and pass the ID with an ONCLICK SubmitReply in the INPUT Submit button? I just tryed it and no errors but nothing happened ether. Im a Coldfusion programmer and Have done a little JS, but this Ajax is still new to me.
Kelly Kc Clements
Wrapping the JS inside the "SubmitReply" function with an onclick to pass the ID worked!! Just nedded to adjust the the `var CFC` with the new ID `cfc.setForm('PostReply'+getReplyId)` for the cfajaxproxy to read it. Thanks again for your help Pekka. You got me to look in the right areas.
Kelly Kc Clements
@Kelly good stuff! Nice to hear it worked out.
Pekka
A: 

Well i dont know why this works but i put getReplyId = document.getElementById("ReplyID").value; inside the PostNewReply function and it still works as a global var. From everything I know this shouldn't work but it does.

Kelly Kc Clements
A: 

Wrapped all the JS inside a new function call "SubmitReply" and passed the ID to it with an onclick "SubmitReply" in the INPUT Submit button. The ID (ReplyID) was then passed to a var getReplyId = (Id). This made the getReplyId use able by all the JS shown above. Now the JS, cfajaxproxy, and CFC can use the ID making the form dynamic with their own ID.

Kelly Kc Clements