www.webdeveloper.com
Results 1 to 6 of 6

Thread: doc.write and tag characters

Hybrid View

  1. #1
    Join Date
    Jun 2012
    Posts
    3

    doc.write and tag characters

    Hello – I’m trying to create a script that takes values from a form and creates XML tags. For example, if I submit “att:int” and “3” the following document.write should execute on the web page

    <att:int>1</att:int>
    <att:int>2</att:int>
    <att:int>3</att:int>

    But i’m having trouble passing the tag like characters (i.e. </). I tried using \, I tried storing </ in a variable but whenever I submit the form, that </ doesn’t allow the script to achieve the intended outcome. I’ve attached a stripped down version of the code below, can’t get “closeTag” to write. Does anyone have a handy trick for this type of output or is javascript not built for this? Thanks for your time!


    <html>
    <head>
    <title>Loop</title>
    </head>
    <body>
    <script language="javascript" type="text/javascript">
    var openTag="<";
    var endTag=">";
    var closeTag="</";

    for(var i=1;i <= 55; i++)
    {
    document.write(openTag + i + endTag);
    document.write(openTag + i + endTag + '<br>');
    }

    </script>


    </body>
    </html>

  2. #2
    Join Date
    Jun 2012
    Posts
    3
    sorry, the loop should be

    for(var i=1;i <= 55; i++)
    {
    document.write(openTag + i + endTag);
    document.write(closeTag + i + endTag + '<br>');
    }

  3. #3
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,143
    If you want to actually write the markup instead of have it rendered, you'll want to replace your "<" with &lt; and your ">" with &gt;. Ex:

    http://jsfiddle.net/dfreema1/hSvKJ/

  4. #4
    Join Date
    Jun 2012
    Posts
    3
    Hi CB, as I was reading your response I thought “duh, you know that!”. I haven’t coded in quite some time. Thanks for your help!

  5. #5
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,143
    No problem! Good luck!

  6. #6
    Join Date
    Nov 2010
    Posts
    1,097
    an alternative to document.write...
    Code:
    <html>
    <head>
    <title>Loop</title>
    </head>
    <body>
    <script type="text/javascript">
    var openTag="<";
    var endTag=">";
    var closeTag="</";
    
    for(var i=1;i <= 55; i++)
    {
    document.body.appendChild(document.createTextNode(openTag + i + endTag));
    document.body.appendChild(document.createTextNode(closeTag + i + endTag));
    document.body.appendChild(document.createElement('br'))
        }
    
    </script> 
    
    
    </body>
    </html>​​​​​​​​​​

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles