<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: how to get selected textarea value using javascript</title>
	<atom:link href="http://corpocrat.com/2008/08/10/how-to-get-selected-value-in-textarea-using-javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://corpocrat.com/2008/08/10/how-to-get-selected-value-in-textarea-using-javascript/</link>
	<description>Daily Blog from Internet Entrepreneur/Webmaster</description>
	<lastBuildDate>Fri, 30 Jul 2010 03:24:48 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Stephen Andrew Carter</title>
		<link>http://corpocrat.com/2008/08/10/how-to-get-selected-value-in-textarea-using-javascript/#comment-7583</link>
		<dc:creator>Stephen Andrew Carter</dc:creator>
		<pubDate>Thu, 03 Jun 2010 14:00:18 +0000</pubDate>
		<guid isPermaLink="false">http://corpocrat.com/2008/08/10/how-to-get-selected-value-in-textarea-using-javascript/#comment-7583</guid>
		<description>function get_selection(the_id)
{
    var e = document.getElementById(the_id);
    
    //Mozilla and DOM 3.0
    if(&#039;selectionStart&#039; in e)
    {
        var l = e.selectionEnd - e.selectionStart;
        return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };
    }
    //IE
    else if(document.selection)
    {
        e.focus();
        var r = document.selection.createRange();
        var tr = e.createTextRange();
        var tr2 = tr.duplicate();
        tr2.moveToBookmark(r.getBookmark());
        tr.setEndPoint(&#039;EndToStart&#039;,tr2);
        if (r == null &#124;&#124; tr == null) return { start: e.value.length, end: e.value.length, length: 0, text: &#039;&#039; };
        var text_part = r.text.replace(/[\r\n]/g,&#039;.&#039;); //for some reason IE doesn&#039;t always count the \n and \r in the length
        var text_whole = e.value.replace(/[\r\n]/g,&#039;.&#039;);
        var the_start = text_whole.indexOf(text_part,tr.text.length);
        return { start: the_start, end: the_start + text_part.length, length: text_part.length, text: r.text };
    }
    //Browser not supported
    else return { start: e.value.length, end: e.value.length, length: 0, text: &#039;&#039; };
}

function replace_selection(the_id,replace_str)
{
    var e = document.getElementById(the_id);
    selection = get_selection(the_id);
    var start_pos = selection.start;
    var end_pos = start_pos + replace_str.length;
    e.value = e.value.substr(0, start_pos) + replace_str + e.value.substr(selection.end, e.value.length);
    set_selection(the_id,start_pos,end_pos);
    return {start: start_pos, end: end_pos, length: replace_str.length, text: replace_str};
}

function set_selection(the_id,start_pos,end_pos)
{
    var e = document.getElementById(the_id);
    
    //Mozilla and DOM 3.0
    if(&#039;selectionStart&#039; in e)
    {
        e.focus();
        e.selectionStart = start_pos;
        e.selectionEnd = end_pos;
    }
    //IE
    else if(document.selection)
    {
        e.focus();
        var tr = e.createTextRange();
        
        //Fix IE from counting the newline characters as two seperate characters
        var stop_it = start_pos;
        for (i=0; i &lt; stop_it; i++) if( e.value[i].search(/[\r\n]/) != -1 ) start_pos = start_pos - .5;
        stop_it = end_pos;
        for (i=0; i &lt; stop_it; i++) if( e.value[i].search(/[\r\n]/) != -1 ) end_pos = end_pos - .5;
        
        tr.moveEnd(&#039;textedit&#039;,-1);
        tr.moveStart(&#039;character&#039;,start_pos);
        tr.moveEnd(&#039;character&#039;,end_pos - start_pos);
        tr.select();
    }
    return get_selection(the_id);
}

function wrap_selection(the_id, left_str, right_str, sel_offset, sel_length)
{
    var the_sel_text = get_selection(the_id).text;
    var selection =  replace_selection(the_id, left_str + the_sel_text + right_str );
    if(sel_offset !== undefined &amp;&amp; sel_length !== undefined) selection = set_selection(the_id, selection.start +  sel_offset, selection.start +  sel_offset + sel_length);
    else if(the_sel_text == &#039;&#039;) selection = set_selection(the_id, selection.start + left_str.length, selection.start + left_str.length);
    return selection;
}

//Your welcome everyone :)</description>
		<content:encoded><![CDATA[<p>function get_selection(the_id)<br />
{<br />
    var e = document.getElementById(the_id);</p>
<p>    //Mozilla and DOM 3.0<br />
    if(&#8216;selectionStart&#8217; in e)<br />
    {<br />
        var l = e.selectionEnd &#8211; e.selectionStart;<br />
        return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };<br />
    }<br />
    //IE<br />
    else if(document.selection)<br />
    {<br />
        e.focus();<br />
        var r = document.selection.createRange();<br />
        var tr = e.createTextRange();<br />
        var tr2 = tr.duplicate();<br />
        tr2.moveToBookmark(r.getBookmark());<br />
        tr.setEndPoint(&#8216;EndToStart&#8217;,tr2);<br />
        if (r == null || tr == null) return { start: e.value.length, end: e.value.length, length: 0, text: &#8221; };<br />
        var text_part = r.text.replace(/[\r\n]/g,&#8217;.'); //for some reason IE doesn&#8217;t always count the \n and \r in the length<br />
        var text_whole = e.value.replace(/[\r\n]/g,&#8217;.');<br />
        var the_start = text_whole.indexOf(text_part,tr.text.length);<br />
        return { start: the_start, end: the_start + text_part.length, length: text_part.length, text: r.text };<br />
    }<br />
    //Browser not supported<br />
    else return { start: e.value.length, end: e.value.length, length: 0, text: &#8221; };<br />
}</p>
<p>function replace_selection(the_id,replace_str)<br />
{<br />
    var e = document.getElementById(the_id);<br />
    selection = get_selection(the_id);<br />
    var start_pos = selection.start;<br />
    var end_pos = start_pos + replace_str.length;<br />
    e.value = e.value.substr(0, start_pos) + replace_str + e.value.substr(selection.end, e.value.length);<br />
    set_selection(the_id,start_pos,end_pos);<br />
    return {start: start_pos, end: end_pos, length: replace_str.length, text: replace_str};<br />
}</p>
<p>function set_selection(the_id,start_pos,end_pos)<br />
{<br />
    var e = document.getElementById(the_id);</p>
<p>    //Mozilla and DOM 3.0<br />
    if(&#8216;selectionStart&#8217; in e)<br />
    {<br />
        e.focus();<br />
        e.selectionStart = start_pos;<br />
        e.selectionEnd = end_pos;<br />
    }<br />
    //IE<br />
    else if(document.selection)<br />
    {<br />
        e.focus();<br />
        var tr = e.createTextRange();</p>
<p>        //Fix IE from counting the newline characters as two seperate characters<br />
        var stop_it = start_pos;<br />
        for (i=0; i &lt; stop_it; i++) if( e.value[i].search(/[\r\n]/) != -1 ) start_pos = start_pos &#8211; .5;<br />
        stop_it = end_pos;<br />
        for (i=0; i &lt; stop_it; i++) if( e.value[i].search(/[\r\n]/) != -1 ) end_pos = end_pos &#8211; .5;</p>
<p>        tr.moveEnd(&#039;textedit&#039;,-1);<br />
        tr.moveStart(&#039;character&#039;,start_pos);<br />
        tr.moveEnd(&#039;character&#039;,end_pos &#8211; start_pos);<br />
        tr.select();<br />
    }<br />
    return get_selection(the_id);<br />
}</p>
<p>function wrap_selection(the_id, left_str, right_str, sel_offset, sel_length)<br />
{<br />
    var the_sel_text = get_selection(the_id).text;<br />
    var selection =  replace_selection(the_id, left_str + the_sel_text + right_str );<br />
    if(sel_offset !== undefined &amp;&amp; sel_length !== undefined) selection = set_selection(the_id, selection.start +  sel_offset, selection.start +  sel_offset + sel_length);<br />
    else if(the_sel_text == &#039;&#039;) selection = set_selection(the_id, selection.start + left_str.length, selection.start + left_str.length);<br />
    return selection;<br />
}</p>
<p>//Your welcome everyone <img src='http://corpocrat.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ganzorig</title>
		<link>http://corpocrat.com/2008/08/10/how-to-get-selected-value-in-textarea-using-javascript/#comment-7174</link>
		<dc:creator>Ganzorig</dc:creator>
		<pubDate>Fri, 12 Mar 2010 17:41:15 +0000</pubDate>
		<guid isPermaLink="false">http://corpocrat.com/2008/08/10/how-to-get-selected-value-in-textarea-using-javascript/#comment-7174</guid>
		<description>Thanks, dude. It has solved the problem that I&#039;m tackling with tiring several continous hours of work. It didn&#039;t work out first, but then I found out that I made misspelling in code (you know after many hours of work, our performance lowers). then it worked out wonderfully. thanks again.</description>
		<content:encoded><![CDATA[<p>Thanks, dude. It has solved the problem that I&#8217;m tackling with tiring several continous hours of work. It didn&#8217;t work out first, but then I found out that I made misspelling in code (you know after many hours of work, our performance lowers). then it worked out wonderfully. thanks again.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Amina Rabeck</title>
		<link>http://corpocrat.com/2008/08/10/how-to-get-selected-value-in-textarea-using-javascript/#comment-6056</link>
		<dc:creator>Amina Rabeck</dc:creator>
		<pubDate>Wed, 06 Jan 2010 15:47:58 +0000</pubDate>
		<guid isPermaLink="false">http://corpocrat.com/2008/08/10/how-to-get-selected-value-in-textarea-using-javascript/#comment-6056</guid>
		<description>Het there, I think you&#039;ll call me god when you&#039;ll see this &lt;a href=&quot;http://www.wix-france.com&quot; rel=&quot;nofollow&quot;&gt;create a free flash website&lt;/A&gt;</description>
		<content:encoded><![CDATA[<p>Het there, I think you&#8217;ll call me god when you&#8217;ll see this <a href="http://www.wix-france.com" rel="nofollow">create a free flash website</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pbu</title>
		<link>http://corpocrat.com/2008/08/10/how-to-get-selected-value-in-textarea-using-javascript/#comment-2118</link>
		<dc:creator>pbu</dc:creator>
		<pubDate>Wed, 05 Nov 2008 22:02:57 +0000</pubDate>
		<guid isPermaLink="false">http://corpocrat.com/2008/08/10/how-to-get-selected-value-in-textarea-using-javascript/#comment-2118</guid>
		<description>That is the problem with textarea. It does not support rich text formatting. You have to use iframe make it bold or other formatting.</description>
		<content:encoded><![CDATA[<p>That is the problem with textarea. It does not support rich text formatting. You have to use iframe make it bold or other formatting.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pqm</title>
		<link>http://corpocrat.com/2008/08/10/how-to-get-selected-value-in-textarea-using-javascript/#comment-2117</link>
		<dc:creator>pqm</dc:creator>
		<pubDate>Wed, 05 Nov 2008 19:05:02 +0000</pubDate>
		<guid isPermaLink="false">http://corpocrat.com/2008/08/10/how-to-get-selected-value-in-textarea-using-javascript/#comment-2117</guid>
		<description>So your code &quot;works&quot; in that it updates the value of my text field.  What it does NOT do is actually make the text bold (in a standard textarea).  Is it possible to format only a portion of the text in a standard textarea field?  By format I mean make it bold, or color the text or something?</description>
		<content:encoded><![CDATA[<p>So your code &#8220;works&#8221; in that it updates the value of my text field.  What it does NOT do is actually make the text bold (in a standard textarea).  Is it possible to format only a portion of the text in a standard textarea field?  By format I mean make it bold, or color the text or something?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
