1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

OnClick select first few characters of text in div or td

Discussion in 'JavaScript' started by khu84, Feb 19, 2009.

  1. #1
    How can we select lets say first 500 characters in a td or div by clicking a button.

    Obviously it will be a function being using button's OnClick event... can anyone give code to select first 500 characters in a td preferably or div in other case.

    --

    Ok in this regard I found a piece of code which select the whole text in div, next step is to select a specific lets say fist 500 characters of div. This will be happening by moving the start or end points of range but not sure how, here is the code...

    see if someone can help moving start and end points of range... actually end point needs to be moved.
     
    khu84, Feb 19, 2009 IP
  2. gnp

    gnp Peon

    Messages:
    137
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can do that in two easy lines :)

    
    function grabText( elementID, howmany )
    {
      var div = document.getElementById( elementID );
      return div.innerHTML.substr( 0, howmany ) ;
    }
    
    Code (javascript):
    take care
     
    gnp, Feb 19, 2009 IP
  3. khu84

    khu84 Active Member

    Messages:
    441
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #3
    @gnp: You didn't got my point. I don't want to get the text in a variable, I just want to select(highlight) it inside the div box. So that someone just right click it and copy it, that is it.
     
    khu84, Feb 21, 2009 IP
  4. gnp

    gnp Peon

    Messages:
    137
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Got it :)

    ok .. here it goes..

    
    function selectText(ID, start, length){
    
    
    if (document.selection)
    	{
    		document.selection.empty();
    		var textC = document.getElementById(ID);
    		var div = document.body.createTextRange();
    		div.moveToElementText(textC);
    		div.setEndPoint("EndToEnd", div);
    		div.moveStart('character', start);
    		div.moveEnd('character', -(textC.innerHTML.length - start - length));
    		div.select();
    	}
    else
    	{
    		window.getSelection().removeAllRanges();
    		var textC = document.getElementById(ID);
    		while ( textC.hasChildNodes() ) textC = textC.childNodes[0];
    		var div = document.createRange();
    
    		div.setStart(textC, start);
    		if (start+length > textC.length) length = textC.length - start;
    		div.setEnd(textC, start+length);
    	
    		window.getSelection().addRange(div);
    	}
    return false;
    }
    
    Code (javascript):
    This is the basic idea ..
    But two important things.
    1. In IE (first case) you define the end by going backwards from the end, thus the negative number. And since we i use innerHTML to do this it means that i do not compensate for tags in that text... so the number i come up with will not be exact characters viewed by the user if there are tags inside the selected text..

    2. in the rest browsers (second case) you can either only make the selection by nodes (like you had), or you can specify start & end (like i have) if and only if you are dealing with a text node.
    So what do i do ? i loop trying to find the first textnode ...

    that is the best i can come up with .. a bit limited albeit ..

    references:
    https://developer.mozilla.org/en/DOM/range
    http://www.quirksmode.org/dom/intro.html#nodes
    https://developer.mozilla.org/en/DOM/Selection

    good luck ..
     
    gnp, Feb 22, 2009 IP
    khu84 likes this.
  5. khu84

    khu84 Active Member

    Messages:
    441
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Many many thanks man. It look great.

    I will implement the code on to the required place in web application and will come up with my feedback on this...
     
    khu84, Feb 22, 2009 IP
  6. khu84

    khu84 Active Member

    Messages:
    441
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #6
    Ok for FF it works great but in IE if your string in the box is smaller than the specified characters it goes to select the next few objects.

    I am going to fix it and going to paste final code here.
     
    khu84, Feb 23, 2009 IP
  7. khu84

    khu84 Active Member

    Messages:
    441
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #7
    Ok finally an if in first portion fixed this and here goes the final code
    
    
    function selectText(ID, start, length){
    
    
    if (document.selection)
        {
            document.selection.empty();
            var textC = document.getElementById(ID);
            var div = document.body.createTextRange();
            div.moveToElementText(textC);
            div.setEndPoint("EndToEnd", div);
            div.moveStart('character', start);
            if(length>textC.innerHTML.length)
            	length=textC.innerHTML.length;
            div.moveEnd('character', -(textC.innerHTML.length - start - length));
            div.select();
        }
    else
        {
            window.getSelection().removeAllRanges();
            var textC = document.getElementById(ID);
            while ( textC.hasChildNodes() ) textC = textC.childNodes[0];
            var div = document.createRange();
    
            div.setStart(textC, start);
            if (start+length > textC.length) length = textC.length - start;
            div.setEnd(textC, start+length);
       
            window.getSelection().addRange(div);
        }
    return false;
    }
    
    
    Code (markup):
    We reached to a very good working code, many thanks gnp.
     
    khu84, Feb 23, 2009 IP
    gnp likes this.
  8. gnp

    gnp Peon

    Messages:
    137
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Excellent !

    glad it helped, and thanks for sharing the changes :)

    [edit]
    i think you should edit your if statement to handle the case where the start is different than 0
    
     if(length>textC.innerHTML.length)
            	length=textC.innerHTML.length;
    Code (javascript):
    should be
    
     if(length + start>textC.innerHTML.length)
            	length=textC.innerHTML.length-start;
    
    Code (javascript):
    take care

    ps. and an additional tip: when you write the code tag you can type
     and it will highlight the code according to javascript syntax, for better readability :)
    Code (javascript):
     
    gnp, Feb 23, 2009 IP
  9. khu84

    khu84 Active Member

    Messages:
    441
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #9
    Many thanks GNP, Nd thanks for the correction plus how to highlight the code in terms of JS script....

    U Rock buddy... :)
     
    khu84, Feb 26, 2009 IP