...

View Full Version : Another ? about the document.write function



TravisMath
03-15-2005, 02:17 AM
In this code



<script language="JavaScript" type="text/javascript">
document.write("<form name='it'>
<div align='center'>
<input onclick='copyit('it.select1')' type='button' value='Press to Copy the Text' name='cpy'> <p> <textarea name='select1' rows='3' cols='25'> If this is highlighted, then it has been copied. </textarea> </div> </form>");

Since you have to use ' ' for any html tags that require quotation marks anywhere in the document.write function is there a way to go inside a ' '. or is there a way to at least get the code to work. The only area i need it to work is when i enter in html code onClick="copyit('it.select1)" since the regular quotation marks become apostraphes.
If what I am trying to ask is to confusing to understand please let me know.
Thanks very much.

hemebond
03-15-2005, 03:35 AM
You don't have to use single quotes, you just need to escape special characters
<script type="text/javascript">
document.write('<form name="it">
<div>
<input onclick="copyit(\'it.select1\')" type="button" value="Press to Copy the Tex" name="cpy">
<p>
<textarea name="select1" rows="3" cols="25">If this is highlighted, then it has been copied. It\'s easy.</textarea>
</p>
</div>
</form>');
</script>

glenngv
03-15-2005, 03:36 AM
document.write('<form name="it"><div align="center"><input onclick="copyit(\'it.select1\')" type="button" value="Press to Copy the Text" name="cpy"><p><textarea name="select1" rows="3" cols="25"> If this is highlighted, then it has been copied. </textarea> </div></form>');
It is recommended to use double quotes for HTML attributes so I used single quotes as the javascript string delimiter. Also you can't have newlines in the middle of a string unless you separate it like below.

document.write('<form name="it">' +
'<div ....'

But does the copyit() function's parameter really a string and not the textarea reference object? If it's an object, then you don't need the single quotes and to make it cross-browser compatible, you must completely reference the textarea with:

document.write('...<input onclick="copyit(document.it.select1')"...');
or simply:

document.write('...<input onclick="copyit(this.form.select1')"...');

BTW, why are you document.write'ing the form when there are no dynamic data in it? Why not simply write the HTML tags?

<form name="it">
<div align="center">
<input onclick="copyit(this.form.select1)" type="button" value="Press to Copy the Text" name="cpy">
<p><textarea name="select1" rows="3" cols="25">
If this is highlighted, then it has been copied.
</textarea>
</div>
</form>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum