<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mohammed CHERIFI &#187; récuperer selection</title>
	<atom:link href="http://www.mcherifi.org/tag/recuperer-selection/feed" rel="self" type="application/rss+xml" />
	<link>http://www.mcherifi.org</link>
	<description>Another Web Developper Blog!</description>
	<lastBuildDate>Tue, 31 Jan 2012 18:42:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Javascript: comment récupérer le texte sélectionné</title>
		<link>http://www.mcherifi.org/developpement-web/javascript/javascript-comment-recuperer-le-texte-selectionne.html</link>
		<comments>http://www.mcherifi.org/developpement-web/javascript/javascript-comment-recuperer-le-texte-selectionne.html#comments</comments>
		<pubDate>Sat, 10 Oct 2009 00:35:49 +0000</pubDate>
		<dc:creator>Mohammed CHERIFI</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[javascript text selection]]></category>
		<category><![CDATA[récuperer selection]]></category>
		<category><![CDATA[selected text]]></category>

		<guid isPermaLink="false">http://www.mcherifi.org/?p=291</guid>
		<description><![CDATA[TweetEn javascript, parfois on a besoin de récupérer le texte sélectionné par le client, c&#8217;est souvent utilisé si vous développez votre propre éditeur wysiwig afin de mettre un texte en gras ou placer un lien hypertexte autour de votre sélection! 
Bref voici une fonction cross-browser qui vous permet cela!
Code : 

getSelectedText = function(){
   [...]]]></description>
			<content:encoded><![CDATA[<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.mcherifi.org/developpement-web/javascript/javascript-comment-recuperer-le-texte-selectionne.html" data-text="Javascript: comment récupérer le texte sélectionné" data-count="horizontal">Tweet</a><p><script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>En javascript, parfois on a besoin de récupérer le texte sélectionné par le client, c&#8217;est souvent utilisé si vous développez votre propre éditeur <acronym title="What You See Is What You GET">wysiwig</acronym> afin de mettre un texte en gras ou placer un lien hypertexte autour de votre sélection! </p>
<p>Bref voici une fonction <acronym title="Compatible avec tous les navigateurs">cross-browser</acronym> qui vous permet cela!</p>
<p><strong>Code : </strong></p>
<pre class="brush: javascript">
getSelectedText = function(){
    selectedText = &#039;&#039;;
    // Gecko, Webkit
    if (window.getSelection) {
        selectedText = window.getSelection();
    }
    // Si IE
    else if (document.selection) {
        selectedText = document.selection.createRange().text;
    }
    return selectedText ;
}
</pre>
<p><strong>Et si on fait un test !</strong></p>
<p><script type="text/javascript">
getSelectedText = function(){
    selectedText = '';
    // Gecko, Webkit
    if (window.getSelection) {
        selectedText = window.getSelection();
    }
    // Si IE
    else if (document.selection) {
        selectedText = document.selection.createRange().text;
    }
    return selectedText ;
}
$(window).ready(function(){
$('#selectedtextbtn').click(function(){
textselection = getSelectedText();
if(textselection == ""){ alert("Oups! vous n'avez rien sélectionné !"); }
else{alert("Voilà! \nVous avez sélectionné : \n"+textselection );}
});
});
</script></p>
<p>Sélectionnez un texte quelconque dans le blog, et cliquez sur le bouton ci dessous! </p>
<p><center><br />
<input type="button" value="Afficher le texte sélectionné" id="selectedtextbtn"/></center></p>
<p>voilà c&#8217;est aussi simple que ça!</p>



Partager cet article:


	<a rel="nofollow"  target="_blank" href="http://www.mcherifi.org/wp-content/plugins/sociable/awesmate.php?c=twitter&t=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html&d=http://twitter.com/home?status=Javascript%3A%20comment%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9%20-%20TARGET" title="Twitter"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mcherifi.org/wp-content/plugins/sociable/awesmate.php?c=facebook-post&t=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html&d=http://www.facebook.com/share.php?u=TARGET%26t=Javascript%3A%20comment%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9" title="Facebook"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html&amp;title=Javascript%3A%20comment%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9&amp;annotation=En%20javascript%2C%20parfois%20on%20a%20besoin%20de%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9%20par%20le%20client%2C%20c%27est%20souvent%20utilis%C3%A9%20si%20vous%20d%C3%A9veloppez%20votre%20propre%20%C3%A9diteur%20wysiwig%20afin%20de%20mettre%20un%20texte%20en%20gras%20ou%20placer%20un%20lien%20hypertexte%20autour%20de%20votre%20s%C3%A9lection%21%20" title="Google Bookmarks"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html&amp;title=Javascript%3A%20comment%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9&amp;notes=En%20javascript%2C%20parfois%20on%20a%20besoin%20de%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9%20par%20le%20client%2C%20c%27est%20souvent%20utilis%C3%A9%20si%20vous%20d%C3%A9veloppez%20votre%20propre%20%C3%A9diteur%20wysiwig%20afin%20de%20mettre%20un%20texte%20en%20gras%20ou%20placer%20un%20lien%20hypertexte%20autour%20de%20votre%20s%C3%A9lection%21%20" title="del.icio.us"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvibes.com/share?title=Javascript%3A%20comment%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9&amp;url=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html" title="Netvibes"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.viadeo.com/shareit/share/?url=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html&title=Javascript%3A%20comment%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9&urllanguage=fr" title="viadeo FR"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/viadeo.png" title="viadeo FR" alt="viadeo FR" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html&amp;title=Javascript%3A%20comment%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9&amp;bodytext=En%20javascript%2C%20parfois%20on%20a%20besoin%20de%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9%20par%20le%20client%2C%20c%27est%20souvent%20utilis%C3%A9%20si%20vous%20d%C3%A9veloppez%20votre%20propre%20%C3%A9diteur%20wysiwig%20afin%20de%20mettre%20un%20texte%20en%20gras%20ou%20placer%20un%20lien%20hypertexte%20autour%20de%20votre%20s%C3%A9lection%21%20" title="Digg"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html&amp;title=Javascript%3A%20comment%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9&amp;source=Mohammed+CHERIFI+Another+Web+Developper+Blog%21&amp;summary=En%20javascript%2C%20parfois%20on%20a%20besoin%20de%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9%20par%20le%20client%2C%20c%27est%20souvent%20utilis%C3%A9%20si%20vous%20d%C3%A9veloppez%20votre%20propre%20%C3%A9diteur%20wysiwig%20afin%20de%20mettre%20un%20texte%20en%20gras%20ou%20placer%20un%20lien%20hypertexte%20autour%20de%20votre%20s%C3%A9lection%21%20" title="LinkedIn"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://slashdot.org/bookmark.pl?title=Javascript%3A%20comment%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9&amp;url=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html" title="Slashdot"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://sphinn.com/index.php?c=post&amp;m=submit&amp;link=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html" title="Sphinn"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mixx.com/submit?page_url=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html&amp;title=Javascript%3A%20comment%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9" title="Mixx"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" class="sociable-hovers" /></a>
	<a  target="_blank" href="http://blogplay.com" title="Blogplay"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/blogplay.png" title="Blogplay" alt="Blogplay" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://identi.ca/notice/new?status_textarea=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html" title="Identi.ca"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/identica.png" title="Identi.ca" alt="Identi.ca" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html&amp;partner=sociable" title="Print"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/printfriendly.png" title="Print" alt="Print" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mcherifi.org/wp-content/plugins/sociable/awesmate.php?c=pingfm&t=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html&d=http://ping.fm/ref/?link=TARGET%26title=Javascript%3A%20comment%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9%26body=En%20javascript%2C%20parfois%20on%20a%20besoin%20de%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9%20par%20le%20client%2C%20c%27est%20souvent%20utilis%C3%A9%20si%20vous%20d%C3%A9veloppez%20votre%20propre%20%C3%A9diteur%20wysiwig%20afin%20de%20mettre%20un%20texte%20en%20gras%20ou%20placer%20un%20lien%20hypertexte%20autour%20de%20votre%20s%C3%A9lection%21%20" title="Ping.fm"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/ping.png" title="Ping.fm" alt="Ping.fm" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mcherifi.org/wp-content/plugins/sociable/awesmate.php?c=mailto&t=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html&d=mailto:?subject=Javascript%3A%20comment%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9%26body=TARGET" title="email"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://posterous.com/share?linkto=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html&amp;title=Javascript%3A%20comment%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9&amp;selection=En%20javascript%2C%20parfois%20on%20a%20besoin%20de%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9%20par%20le%20client%2C%20c%27est%20souvent%20utilis%C3%A9%20si%20vous%20d%C3%A9veloppez%20votre%20propre%20%C3%A9diteur%20wysiwig%20afin%20de%20mettre%20un%20texte%20en%20gras%20ou%20placer%20un%20lien%20hypertexte%20autour%20de%20votre%20s%C3%A9lection%21%20" title="Posterous"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/posterous.png" title="Posterous" alt="Posterous" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html&amp;title=Javascript%3A%20comment%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9" title="Reddit"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html&amp;submitHeadline=Javascript%3A%20comment%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9&amp;submitSummary=En%20javascript%2C%20parfois%20on%20a%20besoin%20de%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9%20par%20le%20client%2C%20c%27est%20souvent%20utilis%C3%A9%20si%20vous%20d%C3%A9veloppez%20votre%20propre%20%C3%A9diteur%20wysiwig%20afin%20de%20mettre%20un%20texte%20en%20gras%20ou%20placer%20un%20lien%20hypertexte%20autour%20de%20votre%20s%C3%A9lection%21%20&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html&amp;partner=sociable" title="PDF"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mcherifi.org/feed" title="RSS"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.diigo.com/post?url=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html&amp;title=Javascript%3A%20comment%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9" title="Diigo"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/diigo.png" title="Diigo" alt="Diigo" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://cgi.fark.com/cgi/fark/farkit.pl?h=Javascript%3A%20comment%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9&amp;u=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html" title="Fark"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/fark.png" title="Fark" alt="Fark" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.blogospherenews.com/submit.php?url=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html&amp;title=Javascript%3A%20comment%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9" title="Blogosphere News"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/blogospherenews.png" title="Blogosphere News" alt="Blogosphere News" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://cimlap.blogter.hu/index.php?action=suggest_link&amp;title=Javascript%3A%20comment%20r%C3%A9cup%C3%A9rer%20le%20texte%20s%C3%A9lectionn%C3%A9&amp;url=http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html" title="blogtercimlap"><img src="http://www.mcherifi.org/wp-content/plugins/sociable/images/blogter.png" title="blogtercimlap" alt="blogtercimlap" class="sociable-hovers" /></a>


<br/><br/>
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fwww.mcherifi.org%2Fdeveloppement-web%2Fjavascript%2Fjavascript-comment-recuperer-le-texte-selectionne.html" layout="standard" show_faces="true" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mcherifi.org/developpement-web/javascript/javascript-comment-recuperer-le-texte-selectionne.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

