Forum Moderators: open
<button> "/> text to change
... instead of ....
<button> text to change
(plus it does not function)....My comments in the code explain more.
<p>--------------code below works fine--------</p>
<input type="button" value="Click" onclick="document.getElementById('b').innerHTML='newtext'"/>
<span id="b">text</span>
<p>--------------but can't get code below to work. Want to get 'text to change' to change to an input field--------</p>
<?php
$newtext = '<input type="text" name="name1" size="30" value="somevalue"/>';
?>
<input type="button" value="Edit" name="name2" onclick="document.getElementById('a').innerHTML=<?php print $newtext; ?>"/>
<span id="a">text to change</span>
<p>-----------------------</p>
Thanks, in anticipation of your help.
onclick="document.getElementById('a').innerHTML=<input type="text" name="name1" size="30" value="somevalue"/>" As you can probably see, you are missing string delimiters '...' around your newtext and there is now a mismatch of double quotes with the string you are trying to assign to innerHTML and the onclick attribute itself (these need to be escaped).
So (also swapping around the double and single quotes) ...
<?php
$newtext = "<input type=\\'text\\' name=\\'name1\\' size=\\'30\\' value=\\'somevalue\\' \/>";
?>
<input type="button" value="Edit" name="name2" onclick="document.getElementById('a').innerHTML='<?php print $newtext; ?>'"/> However, the better solution would be to not use inline JavaScript and keep it in an external file. Then you wouldn't have to mess around escaping everything.
<edit>Corrected the escapes! *sigh*</edit>