Click to See Complete Forum and Search --> : change text in textfield


ragawu
06-15-2004, 08:41 AM
Hi I'm tryign to use javascript to copy a value into a text field when the user clicks on an image.
The whole script is generated by php and I may be having single-quote or double-quote issues though feel free to correct me on this.

function: (in head of page)

<script language="JavaScript">
<!--

var isReady = false;

function changeText(helpID, helpText)
{
if (isReady){
document.getElementByName(helpID).value = helpText;
document.getElementByName(helpID).focus();
document.getElementByName(helpID).select();
}
else{
alert("This page is not fully loaded yet...\nPlease wait for the page to finish loading.");
}
}
//-->
</script>


in body tag

<body onload="isready=true">


this is the line that calls the function:
<font size='1' color='#666666'><img src='http://www.ginezbukov.com/phpsurveyor/help.gif' alt='Help' align='left' onMouseDown='changeText('1X1X5', 'Cleeland')'>Cleeland</font>

I'm receiving an error internet explorer on the line that calls the function. Error is "syntax error".
I realise this has probably ben asked before, but I've tried searching and can't find anything relevant.
hope you can help,.
alasdair

Pittimann
06-15-2004, 08:59 AM
Hi!

You are having quotes issues; besides that, in your body you say isready, but in the script it is isReady. Example which should work:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
var isReady = false;
function changeText(helpID, helpText)
{
if (isReady){
document.getElementById(helpID).value = helpText;
document.getElementById(helpID).focus();
document.getElementById(helpID).select();
}
else{
alert("This page is not fully loaded yet...\nPlease wait for the page to finish loading.");
}
}
//-->
</script>
</head>
<body onload="isReady=true">
<font size='1' color='#666666'><img src="http://www.ginezbukov.com/phpsurveyor/help.gif" alt="Help" align="left" onMouseDown="changeText('1X1X5', 'Cleeland')">Cleeland</font>
<br>
<input id="1X1X5">
</body>
</html>To avoid the quotes problem in PHP, you should use this (I am assuming that you are echoing the code):<? echo "<img src=\"http://www.ginezbukov.com/phpsurveyor/help.gif\" alt=\"Help\" align=\"left\" onMouseDown=\"changeText('1X1X5', 'Cleeland')\">Cleeland</font>"; ?>Cheers - Pit

Edit: working example with the image stuff in PHP like above:
http://www.pit-r.de/onloadSetvar.php

ragawu
06-15-2004, 09:07 AM
Thanks for yoru reply,
does the fact that I'm using getelementbyname instead of id affect the results? I can only use the name in this case, though I know that this 'name' only appears once in the page.
alasdair

Pittimann
06-15-2004, 09:19 AM
Hi!

document.getElementsByName always is an array of elements with the given name (even if there is only one). You can use this to refer to the element correctly, if it is the first element in the document with that name or if it is the only one like you said:

document.getElementsByName(helpID)[0].value = helpText;
document.getElementsByName(helpID)[0].focus();
document.getElementsByName(helpID)[0].select();

The rest can remain untouched except <input id="1X1X5"> which will of course need a name and not an id.

Cheers - Pit

ragawu
06-15-2004, 09:27 AM
Hi,
thanks for all the advice. I now have the script workign well. I used the first example, just fixing all my quote issues. I edited the php to output an id as well as a name. I think id is better in the long run, can be sure what I'm targeting.
thanks once again!
Alasdair

Pittimann
06-15-2004, 09:29 AM
Hi!

You're welcome!

Cheers - Pit