Forum Moderators: open

Message Too Old, No Replies

How to get value from document.write

         

Balaji1980

10:23 pm on Aug 3, 2010 (gmt 0)

10+ Year Member



Hi,

I am looking to get the value from the document.write. The document.write value is actually a javascript value. I would like to get that value and pass it on to another javascript function.

Here is my HTML code:

<html>
<head>
<script language="JavaScript" type="text/javascript">
var entryNumber = 0;
</script>
</head>
<body>
<form>
<table>
<tr><td><b>S No</b></td><td></td><td><b>Subject</b></td><td></td></tr>
<tr><td><img style="cursor:hand" src="plus-blue.gif?OpenImageResource"onClick="getInfo('')">&nbsp;<script> var temp = ++entryNumber; document.write(temp);</script></td><td></td><td>One</td><td></td></tr>
<tr><td><img style="cursor:hand" src="plus-blue.gif?OpenImageResource"onClick="getInfo('')">&nbsp;<script> var temp = ++entryNumber; document.write(temp);</script></td><td></td><td>Two</td><td></td></tr>
<tr><td><img style="cursor:hand" src="plus-blue.gif?OpenImageResource"onClick="getInfo('')">&nbsp;<script> var temp = ++entryNumber; document.write(temp);</script></td><td></td><td>Three</td><td></td></tr>
</table>
</form>
</body>
</html>


My requirement is I want to pass the values under "S No" to getinfo function mentioned in the <img> tag. I am not able to get that value. Can anyone suggest an option?

Thanks in advance.

daveVk

3:36 am on Aug 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Replace

<tr><td><img style="cursor:hand" src="plus-blue.gif?OpenImageResource"onClick="getInfo('')">&nbsp;<script> var temp = ++entryNumber; document.write(temp);</script></td><td></td><td>One</td><td></td></tr>
<tr><td><img style="cursor:hand" src="plus-blue.gif?OpenImageResource"onClick="getInfo('')">&nbsp;<script> var temp = ++entryNumber; document.write(temp);</script></td><td></td><td>Two</td><td></td></tr>
<tr><td><img style="cursor:hand" src="plus-blue.gif?OpenImageResource"onClick="getInfo('')">&nbsp;<script> var temp = ++entryNumber; document.write(temp);</script></td><td></td><td>Three</td><td></td></tr>

with

<script>document.write(row(1,"One")+row(2,"Two")+row(3,"Three"));</script>

replace

var entryNumber = 0;

with

function row( No, Text ) {
return '<tr ... getInfo(' + No + ...
}

Balaji1980

4:40 pm on Aug 4, 2010 (gmt 0)

10+ Year Member



Thanks Dave for your response. In my example, I have just provided first 3 rows as an example. But actually there will be lot more rows. So, I do not want to hardcode the parameter for the function row(1, "One"), row (2, "Two") etc.

Based on this scenario, I would like to get the actual row number as the parameter for the 'getinfo' function. As I am already populating the row number in the 'S No' column of the table, I thought it would be good if I can get that value and pass it on to the function. Is that possible?

Thanks..!

daveVk

12:13 am on Aug 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Lots more rows seems like a compelling reason to hardcode the parameters, I do not know how you, generate, maintain this, it looks messy to me.

Alternative approaches would be to code the call to getInfo as getInfo(this) and have getInfo dig the number out of the html, or to put more document writes in html something like

... oncliick="getinfo(<script>document.write(entryNumber+1);</script>)" ...

If you are generation this from PHP (or whatever) you would be better of doing it all in PHP.