To select text using div tag

Discussion in 'JavaScript' started by OPETH, Sep 29, 2008.

  1. #1
    <form name="select" class="seckod">

    <script type="text/javascript">

    function selectext()
    {
    document.getElementById("testarea").select()
    }

    </script>

    <input class="button" type="button" value="Select Codes" onClick="selectext()">
    <textarea readonly="readonly" id="testarea">


    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
    Select Me
    &lt;/body&gt;
    &lt;/html&gt;




    </textarea>

    </form>


    The codes in above works using textarea tag.
    How to make it to use div tag instead of textarea tag?
     
    OPETH, Sep 29, 2008 IP
  2. OPETH

    OPETH Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <html>
    <head>
    <script language="javascript">
    
    function selectText(divid)
    {
    if (document.selection)
    {
    var div = document.body.createTextRange();
    
    div.moveToElementText(document.getElementById("divid"));
    div.select();
    }
    else
    {
    var div = document.createRange();
    
    div.setStartBefore(document.getElementById("divid"));
    div.setEndAfter(document.getElementById("divid")) ;
    
    window.getSelection().addRange(div);
    }
    
    }
    
    </script>
    </head>
    
    <body>
    
    <button onclick="selectText(divid)">Select Text</button>
    
    
    <div id="divid">Select Me</div>
    
    </body>
    </html>
    Code (markup):
    it is work. the codes in above written by a sirepoint member.
     
    OPETH, Sep 30, 2008 IP