469,923 Members | 1,550 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

What am I doing wrong? js & function()

Ken
I want to write a js variable inside a function to a web page.

test.js

try_picture();
function try_picture() {
var picture = '<tr><td colspan=2 align=right><font size=2>Picture
&nbsp;&nbsp;5:&nbsp;</font></td><td colspan=2><input type=file size=68
name="picture5"></td></tr>';
}

HTML

This is part of a table.

<script language="javascript"
type="text/javascript">document.write(picture);</script>

If I remove the function command, the picture is written into the HTML. The
function is the problem.

I have tried try_picture(picture) and function try_picture(picture). It did
not work.

I would appreciate any suggestions.

I am missing something.

Ken

Jul 23 '05 #1
5 1165
Ken wrote:
try_picture();
function try_picture() {
var picture = '<tr><td colspan=2 align=right><font size=2>Picture
&nbsp;&nbsp;5:&nbsp;</font></td><td colspan=2><input type=file size=68
name="picture5"></td></tr>';
}


Does this do what you want?

<head>
<script type="text/javascript">
function try_picture() {
var picture = '<tr>\n<td colspan=2 align=\"right\">\n<font size=2>';
picture += 'Picture 5: </font>\n</td>\n<td colspan=2>\n';
picture += '<input type=\"file\" size=68
name=\"picture5\">\n</td>\n</tr>';
document.write(picture);
}
</script>
</head>

<body>
<form>
<input type="button" onclick="try_picture()" value="Try Picture">
</form>
</body>

MIke

Jul 23 '05 #2
Ken
Close, but I want to write the js script on the original html page and not a
new page.
form>
<script language="javascript"
type="text/javascript">document.write(picture);</script>
<input type="button" onclick="try_picture()" value="Try Picture">
</form>

"mscir" <ms***@access4less.com.net.org.uk> wrote in message
news:10*************@corp.supernews.com...
Ken wrote:
try_picture();
function try_picture() {
var picture = '<tr><td colspan=2 align=right><font size=2>Picture
&nbsp;&nbsp;5:&nbsp;</font></td><td colspan=2><input type=file size=68
name="picture5"></td></tr>';
}
Does this do what you want?

<head>
<script type="text/javascript">
function try_picture() {
var picture = '<tr>\n<td colspan=2 align=\"right\">\n<font size=2>';
picture += 'Picture 5: </font>\n</td>\n<td colspan=2>\n';
picture += '<input type=\"file\" size=68
name=\"picture5\">\n</td>\n</tr>';
document.write(picture);
}
</script>
</head>

<body>
<form>

<script language="javascript"
type="text/javascript">document.write(picture);</script> <input type="button" onclick="try_picture()" value="Try Picture">
</form>
</body>

MIke

Jul 23 '05 #3
mscir wrote:
Ken wrote:
try_picture();
function try_picture() {
var picture = '<tr><td colspan=2 align=right><font size=2>Picture
&nbsp;&nbsp;5:&nbsp;</font></td><td colspan=2><input type=file size=68
name="picture5"></td></tr>';
}


Sorry, I goofed, this approach might work:

<script type="text/javascript">
function showpic(targetID) {
var picture = '<tr><td colspan=2 align=\"right\"><font size=2>';
picture += 'Picture 5: </font></td><td colspan=2>';
picture += '<input type=\"file\" size=68 name=\"picture5\"></td></tr>';
document.getElementById(targetID).innerHTML=pictur e;
}
</script>
</head>

<body>
<table width=100 border=1>
<tr>
<td><div name="pic1" id="pic1"></div>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
<br>
<form>
<input type="button" onclick="showpic('pic1')" value="Show Pic">
</form>
</body>

Jul 23 '05 #4
Lee
Ken said:

I want to write a js variable inside a function to a web page.

test.js

try_picture();
function try_picture() {
var picture = '<tr><td colspan=2 align=right><font size=2>Picture
&nbsp;&nbsp;5:&nbsp;</font></td><td colspan=2><input type=file size=68
name="picture5"></td></tr>';
}

HTML

This is part of a table.

<script language="javascript"
type="text/javascript">document.write(picture);</script>

If I remove the function command, the picture is written into the HTML. The
function is the problem.


The problem is that you have declared picture to be a local
variable of try_picture(). If you want to reference it from
outside of that function, it must be a global variable.
The simplest fix is to remove the "var" keyword.

Jul 23 '05 #5
Ken wrote:
I want to write a js variable inside a function to a web page.

test.js

try_picture();
function try_picture() {
var picture = '<tr><td colspan=2 align=right><font size=2>Picture
&nbsp;&nbsp;5:&nbsp;</font></td><td colspan=2><input type=file size=68
name="picture5"></td></tr>';
}

HTML

This is part of a table.

<script language="javascript"
type="text/javascript">document.write(picture);</script>

If I remove the function command, the picture is written into the HTML. The
function is the problem.

I have tried try_picture(picture) and function try_picture(picture). It did
not work.

I would appreciate any suggestions.

I am missing something.

Ken


function try_picture() {
// split acorss multiple lines to prevent wrapping
return '<tr><td colspan=2 align=right>' +
'<font size=2>Picture&nbsp;&nbsp;5:&nbsp;</font>' +
'</td><td colspan=2>' +
'<input type=file size=68 name="picture5">' +
'</td></tr>';
}

<script type="text/javascript">document.write(try_picture());</script>

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by tin | last post: by
2 posts views Thread by Phil Certain | last post: by
8 posts views Thread by watkinsdev | last post: by
4 posts views Thread by Robert Dufour | last post: by
16 posts views Thread by SirG | last post: by
16 posts views Thread by John Doe | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.