Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2005
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Another ? about the document.write function

    In this code

    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.

  • #2
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    You don't have to use single quotes, you just need to escape special characters
    Code:
    <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>
    Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
    I code to specs, and test only in Firefox (unless stated otherwise).

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    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.
    Code:
    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:
    Code:
    document.write('...<input onclick="copyit(document.it.select1')"...');
    or simply:
    Code:
    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?
    Code:
    <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>
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •