View Full Version : ASP Substitution within Javascript?
gorilla1
03-05-2003, 04:54 PM
I am trying to set something in javascript based on a variable in ASP, like so (below), but the alert shows up as though n were blank.. Shouldn't this work?
<script language="JavaScript">
function onButton()
{
window.document.form_1.u_input<% =n %>.value = 6;
var junk = "<% =n %>"
alert(junk)
}
</script>
arnyinc
03-05-2003, 05:01 PM
View the source and make sure "n" exists. All the ASP code is doing is creating text that will fit in your javascript.
gorilla1
03-05-2003, 05:16 PM
Thanks. No, n shows up as blank in the source. n is an Asp variable, so it is going to vary. Maybe I need to be doing a document.write or something? Not sure.
G
arnyinc
03-05-2003, 05:19 PM
The way that your code is written is correct. n must be defined before you access it though. It all depends on what n is and where you are getting that value from. For example, make it:
<%
n=5
%>
<script language="JavaScript">
function onButton()
{
window.document.form_1.u_input<% =n %>.value = 6;
var junk = "<% =n %>"
alert(junk)
}
</script>
gorilla1
03-05-2003, 05:42 PM
Ah, ok. I am looping through like so - so really n is unknown until img is clicked and I guess I need something defined around the iamge that I can pass to the javascript and then set the value of the hidden input.
<%
for n=1 to 5
<img border=""0"" src=""images1/xxx.jpg"" height=""11"" width=""11"" alt=""Give it 5 stars"" onClick=""onButton();"">
Response.Write("<input type=""hidden"" name=""u_input" & n & """ value="""">")
next
arnyinc
03-05-2003, 05:51 PM
I think you just want to pass n as a parameter. I always have trouble with concatenating variables in functions for some reason. I can't figure out how to fix the line in the function that I marked as "this line is incorrect", but you have to concatenate the variable. It shouldn't be hard, but I always screw it up. :)
<script language="JavaScript">
function onButton(n)
{
window.document.form_1.u_input+n.value = 6; //this line is incorrect
var junk = n
alert(junk)
}
</script>
<%
for n=1 to 5
<img border=""0"" src=""images1/xxx.jpg"" height=""11"" width=""11"" alt=""Give it 5 stars"" onClick=""onButton("&n&");"">
Response.Write("<input type=""hidden"" name=""u_input" & n & """ value="""">")
next
%>
Roy Sinclair
03-05-2003, 06:13 PM
Originally posted by gorilla1
Ah, ok. I am looping through like so - so really n is unknown until img is clicked and ...
Make sure you aren't trying to intermix client side actions with server side code because that'll never work. Everything on the server will have finished running before the client code or actions even get started.
gorilla1
03-05-2003, 06:24 PM
Thanks, Roy. But this will set up what I need. The loop is simply wirting out html. Set up right, that will enable the onclcik function to modify the form value as needed. It all looks good, except that this line triggers a javascript error
window.document.form_1.u_input + n +.value = 6;
arnyinc
03-05-2003, 06:27 PM
Change that line to these two lines and it is fixed. It should be easy to do this on one line, but I keep goofing it up.
temp='window.document.form_1.u_input'+n
temp.value=6;
gorilla1
03-05-2003, 06:42 PM
Yep, that did it. Very nice. I couldn't get it to work all on one line either.
G
gorilla1
03-05-2003, 06:53 PM
Ah, well, not sure it worked after all. That got rid of the jscript error, but the following yields an empty result and the value on the hidden input also comes up empty.
temp1 = window.document.form_1.u_input0.value
alert(temp1);
gorilla1
03-05-2003, 08:07 PM
Got to work finally by junking the function and putting it all in the onClick statement:
onClick=""window.document.form_1.u_input" & n & ".value=" & ncnt & ";""
Roy Sinclair
03-05-2003, 08:49 PM
Using the separate function, this should work too:
function onButton(n)
{
document.form_1["u_input"+n].value = 6;
}
gorilla1
03-06-2003, 12:30 AM
Ah, ok, thanks Roy, that may come in handy.
G
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.