SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Jul 2004
    Location
    Brooklyn, NY
    Posts
    316
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    selecting text inside <div>

    is it possible to use a javascript function to select text within a <div>?

    I know it's possible to use document.getElementById(id).select() to select text within an input or textarea, but what about divs or spans?

  2. #2
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    You would need to use the DOM to get the value of the text inside of the DIV or SPAN.

    Something like this (off of the top of my head so this may be wrong):

    $text = document.getElementById('myid').firstChild.nodeValue;

  3. #3
    SitePoint Addict
    Join Date
    Jul 2004
    Location
    Brooklyn, NY
    Posts
    316
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wouldn't that simply retrieve the text?
    I want it to be selected so the user can CTRL+C it or drag it somewhere

  4. #4
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    In that case I am not sure JavaScript can do that

  5. #5
    SitePoint Addict
    Join Date
    Jul 2004
    Location
    Brooklyn, NY
    Posts
    316
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see, so it's only possible with input or textareas?

  6. #6
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    As far as I know. I would have though a guru would have jumped in and corrected my if I was wrong already so I am pretty sure it is not possible.

  7. #7
    I'm not a human
    Join Date
    Aug 2006
    Location
    India
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi

    i think the following code may help you


    Code:
    <script type="text/javascript">
    	function fnSelect(objId) {
    		fnDeSelect();
    		if (document.selection) {
    		var range = document.body.createTextRange();
     	        range.moveToElementText(document.getElementById(objId));
    		range.select();
    		}
    		else if (window.getSelection) {
    		var range = document.createRange();
    		range.selectNode(document.getElementById(objId));
    		window.getSelection().addRange(range);
    		}
    	}
    		
    	function fnDeSelect() {
    		if (document.selection) document.selection.empty(); 
    		else if (window.getSelection)
                    window.getSelection().removeAllRanges();
    	}
    	</script>
    <body>
    
    <div id="test1">
    	<p>jhsdgfhlsdlfkjsdklgjs</p>
    	<p>jhsdgfhlsdlfkjsdklgjs</p>
    	<p>jhsdgfhlsdlfkjsdklgjs</p>
    </div>
    <div id="test2">
    	<p>jhsdgfhlsdlfkjsdklgjs</p>
    	<p>jhsdgfhlsdlfkjsdklgjs</p>
    	<p>jhsdgfhlsdlfkjsdklgjs</p>
    </div>
    <a href="javascript:fnSelect('test1');">Select 1</a>
    <a href="javascript:fnSelect('test2');">Select 2</a>
    <a href="javascript:fnDeSelect();">DeSelect</a>
    </body>
    Regards,
    Vijay
    Follow me on twitter @vijaycbe
    World Holiday Calander || My Cricket Blog


Bookmarks

Posting Permissions

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