Javascript help

Want to just shoot the breeze? Forum 42 is the place!

Moderator:Moderators

Post Reply
User avatar
HK-47
Moderator
Posts:3598
Joined:Thu Jul 15, 2004 2:17 pm
Location:/dev/moderator/
Contact:
Javascript help

Post by HK-47 » Mon Jan 30, 2006 6:30 pm

I know this isnt a programming forum, but its all i got. I have a question about how to do something in javascript.
Lets say I have a thing that looks like this: (roughly)

Code: Select all

<script>
function checkvalue (x, y) {
	cellh = 'document.row' + y + '.col' + x + 'h.value'
	alert(cellh);
}
</script>

<form name="row1"><input name="col1"><input name="col1"></form>

<form name="checkvalueform">
	X: <input type="text" name="x"> Y: <input type="text" name="y">
	<input type="button" value="check value" onclick="checkvalue(document.checkvalueform.x.value, document.checkvalueform.y.value)">
</form>
Now when I go to use the checkvalue function it gives me, for example, "document.row1.col1h.value" rather then the value of document.row1.col1h . Now, is there any way I can do this the way I am? Or should I rewrite the entire concept?

User avatar
marshallh
Moderator
Posts:2986
Joined:Sat Sep 10, 2005 2:17 pm
360 GamerTag:marshallh
Location:here and there
Contact:

Re: Javascript help

Post by marshallh » Mon Jan 30, 2006 6:36 pm

HK-47 wrote:

Code: Select all

	cellh = 'document.row' + y + '.col' + x + 'h.value'
	
Am I missing something, or can you just change that last bit to 'h'? I'd go to an online order form and see what code they have to handle invalid entries.
Image

User avatar
HK-47
Moderator
Posts:3598
Joined:Thu Jul 15, 2004 2:17 pm
Location:/dev/moderator/
Contact:

Post by HK-47 » Mon Jan 30, 2006 6:43 pm

No, its all one big string.
I want to know how to convert the string to a what the string represents. (the value of document.row1.col1h rather then the string "document.row1.col1h.value")

User avatar
gannon
Moderator
Posts:6974
Joined:Sun Apr 04, 2004 4:48 pm
Location:Near that one big lake
Contact:

Post by gannon » Mon Jan 30, 2006 8:20 pm

I don't have much experience with variable variables in java. I'll look around though
Edit:
Fixed

Code: Select all

<script type="text/javascript">
function checkvalue () {
var x = document.checkvalueform.x.value;
var y = document.checkvalueform.y.value;
   alert(eval("document.row" + y + ".col" + x + ".value"));
}
</script>
<form name="row1"><input name="col1"></form>
<form name="checkvalueform">
X: <input type="text" name="x">
Y: <input type="text" name="y">
<input type="button" value="check value" onclick="checkvalue()">
</form>
Just remember to evaluate the code inside the alert.
Last edited by gannon on Mon Jan 30, 2006 8:33 pm, edited 1 time in total.

User avatar
HK-47
Moderator
Posts:3598
Joined:Thu Jul 15, 2004 2:17 pm
Location:/dev/moderator/
Contact:

Post by HK-47 » Mon Jan 30, 2006 8:22 pm

Its ok, Ive rewritten some of it. Its working now. Thanks for offering though. :)

Post Reply