<?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>Linea de Codigo &#187; jQuery</title> <atom:link href="http://lineadecodigo.com/categoria/jquery/feed/" rel="self" type="application/rss+xml" /><link>http://lineadecodigo.com</link> <description>/* Programación en la red */</description> <lastBuildDate>Tue, 07 Feb 2012 07:00:08 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>Efecto leer más texto simplificado con jQuery</title><link>http://lineadecodigo.com/jquery/efecto-leer-mas-texto-simplificado-con-jquery/</link> <comments>http://lineadecodigo.com/jquery/efecto-leer-mas-texto-simplificado-con-jquery/#comments</comments> <pubDate>Fri, 06 Jan 2012 07:00:56 +0000</pubDate> <dc:creator>Víctor Cuervo</dc:creator> <category><![CDATA[jQuery]]></category> <category><![CDATA[hide]]></category> <category><![CDATA[show]]></category> <category><![CDATA[siblings]]></category> <category><![CDATA[span]]></category> <category><![CDATA[text]]></category> <category><![CDATA[toggle]]></category> <guid
isPermaLink="false">http://lineadecodigo.com/?p=3652</guid> <description><![CDATA[El otro día veíamos como poder generar un efecto leer más texto con jQuery sobre un texto que no tenía estructura predefinida y que se encontraba dentro de un DIV. Hoy vamos a ver como simplificar este ejemplo si tenemos la estructura del texto predefinida. Con lo que nos quedará un efecto leer más texto [...]]]></description> <content:encoded><![CDATA[<div
class='dd_post_share'><div
class='dd_buttons'><div
class='dd_button'><div
class='dd-google1-ajax-load dd-google1-3652'></div><g:plusone size='medium' href='http://lineadecodigo.com/jquery/efecto-leer-mas-texto-simplificado-con-jquery/'></g:plusone></div><div
class='dd_button'><script src="http://connect.facebook.net/es_ES/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Flineadecodigo.com%2Fjquery%2Fefecto-leer-mas-texto-simplificado-con-jquery%2F" send="true" show_faces="false"  layout="button_count" width="92"  ></fb:like></div><div
class='dd_button'><a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://lineadecodigo.com/jquery/efecto-leer-mas-texto-simplificado-con-jquery/" data-count="horizontal" data-text="Efecto leer más texto simplificado con jQuery" data-via="lineadecodigo" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div></div><div
style='clear:both'></div></div><div
style='clear:both'></div><p>El otro día veíamos <a
href="http://lineadecodigo.com/jquery/efecto-leer-mas-texto-con-jquery/" title="Efecto leer más con jQuery">como poder generar un efecto leer más texto con jQuery</a> sobre un texto que no tenía estructura predefinida y que se encontraba dentro de un DIV.</p><p>Hoy vamos a ver como simplificar este ejemplo si tenemos la estructura del texto predefinida. Con lo que nos quedará un efecto leer más texto simplificado.</p><p>Y es que si podemos estructurar el texto sobre el que queremos realizar el efecto sería bueno que lo insertásemos en capas <a
href="http://www.w3api.com/wiki/HTML:SPAN" title="Elemento SPAN de HTML">SPAN</a>. En concreto en tres capas <a
href="http://www.w3api.com/wiki/HTML:SPAN" title="Elemento SPAN de HTML">SPAN</a>.</p><ul><li><strong>summary</strong>, con el texto de resumen.</li><li><strong>complete</strong>, con el resto del texto.</li><li><strong>more</strong>, con el texto "leer más".</li></ul><p>Es importante que utilicemos elementos <a
href="http://www.w3api.com/wiki/HTML:SPAN" title="Elemento SPAN de HTML">SPAN</a> ya que estos son inline y se mostrarán todos seguidos, si saltos y darán uniformidad al texto.</p><p>La estructura quedará de la siguiente forma:</p><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:SPAN"><span style="color: #000000; font-weight: bold;">span</span></a> <a href="http://w3api.com/wiki/HTML:class"><span style="color: #000066;">class</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;summary&quot;</span>&gt;</span>
&nbsp;
Lorem ipsum dolor sit amet, vis alterum feugiat sanctus ea. Affert facete eu cum, ut dicta vitae quo. Cum id aeque option epicuri, nec oblique suscipit ad. Et sea odio adhuc altera. Verterem nominati an mel, his diam consetetur ei. Eligendi principes sed no, ubique patrioque vix cu, soluta veritus percipit vim ex.
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:SPAN"><span style="color: #000000; font-weight: bold;">span</span></a>&gt;</span>
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:SPAN"><span style="color: #000000; font-weight: bold;">span</span></a> <a href="http://w3api.com/wiki/HTML:class"><span style="color: #000066;">class</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;complete&quot;</span>&gt;</span>
&nbsp;
Pro populo dolorem eu. Justo conclusionemque sea ne. At dicat putent eum. Reprimique delicatissimi vel et.
&nbsp;
Posse constituam pro eu. Sit graecis appetere at, id his quas possit platonem, ius at nobis decore ullamcorper. Vix ne latine iudicabit constituto, justo fastidii eleifend et pro, ei sit solet dicant mediocrem. Te sit cibo percipit persecuti. Debet convenire suavitate ei vis. Pri utinam labitur temporibus ex.
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:SPAN"><span style="color: #000000; font-weight: bold;">span</span></a>&gt;</span>
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:SPAN"><span style="color: #000000; font-weight: bold;">span</span></a> <a href="http://w3api.com/wiki/HTML:class"><span style="color: #000066;">class</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;more&quot;</span>&gt;</span>Leer mas...<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:SPAN"><span style="color: #000000; font-weight: bold;">span</span></a>&gt;</span></pre><p>El código jQuery se quedará simplificado ya que solo tendremos que controlar el <a
href="http://www.w3api.com/wiki/jQuery:Toggle%28%29" title="Método toggle de jQuery">.toggle()</a> sobre el elemento "more". Es decir, el cambio de estado de este elemento.</p><pre class="jquery" style="font-family:monospace;"><span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:ready%28%29"><span style="color: #000066;">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.more&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:toggle%28%29"><span style="color: #000066;">toggle</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:text"><span style="color: #000066;">text</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Leer menos...&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:siblings"><span style="color: #000066;">siblings</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.complete&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:show%28%29"><span style="color: #000066;">show</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:text"><span style="color: #000066;">text</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Leer mas...&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:siblings"><span style="color: #000066;">siblings</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.complete&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:hide%28%29"><span style="color: #000066;">hide</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>En cada uno de los cambios de texto realizamos las tres acciones apoyadas en los métodos <a
href="http://www.w3api.com/wiki/jQuery:Text%28%29" title="Método .text() de jQuery">.text()</a> para cambiar el texto del elemento "more", <a
href="http://www.w3api.com/wiki/jQuery:Siblings%28%29" title="Método siblings de jQuery">.siblings()</a> para ir a por el elemento hermano "complete" y <a
href="http://www.w3api.com/wiki/jQuery:Show%28%29" title="Método show de jQuery">.show()</a>/<a
href="http://www.w3api.com/wiki/jQuery:Hide%28%29" title="Método Hide de jQuery">.hide()</a> para ocultar o mostrar el texto.</p><p>Vemos que con la estructuración predefinida de la página se nos simplifica mucho el código <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a> ya que nos evitamos la reestructuración del contenido "al vuelo" con <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a>.<strong>Similar Posts:</strong><ul
class="similar-posts"><li><a
href="http://lineadecodigo.com/jquery/efecto-leer-mas-texto-con-jquery/" rel="bookmark" title="Enero 4, 2012">Efecto leer más texto con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/efecto-slide-con-jquery/" rel="bookmark" title="Enero 5, 2012">Efecto slide con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/usando-cookies-con-jquery/" rel="bookmark" title="Diciembre 28, 2009">Usando cookies con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/ocultar-y-mostrar-elementos-con-jquery/" rel="bookmark" title="Febrero 5, 2009">Ocultar y mostrar elementos con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/volcar-el-contenido-de-un-array-con-jquery/" rel="bookmark" title="Mayo 31, 2011">Volcar el contenido de un array con jQuery</a></li></ul><p></p> <script type="text/javascript">jQuery(document).ready(function($) { window.setTimeout('loadGoogle1_3652()',1000); });</script><script type="text/javascript">function loadGoogle1_3652(){ jQuery(document).ready(function($) { $('.dd-google1-3652').remove();$.getScript('https://apis.google.com/js/plusone.js'); }); }</script>]]></content:encoded> <wfw:commentRss>http://lineadecodigo.com/jquery/efecto-leer-mas-texto-simplificado-con-jquery/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>Efecto slide con jQuery</title><link>http://lineadecodigo.com/jquery/efecto-slide-con-jquery/</link> <comments>http://lineadecodigo.com/jquery/efecto-slide-con-jquery/#comments</comments> <pubDate>Thu, 05 Jan 2012 18:52:00 +0000</pubDate> <dc:creator>Víctor Cuervo</dc:creator> <category><![CDATA[jQuery]]></category> <category><![CDATA[efecto]]></category> <category><![CDATA[hide]]></category> <category><![CDATA[jquery ui]]></category> <category><![CDATA[show]]></category> <category><![CDATA[slide]]></category> <category><![CDATA[toggle]]></category> <guid
isPermaLink="false">http://lineadecodigo.com/?p=3657</guid> <description><![CDATA[El efecto slide es el que nos permite realizar un desplazamiento de un elemento en alguna dirección (izquierda, derecha, arriba o abajo). En las siguientes líneas veremos lo sencillo que es hacerlo con jQuery. En nuestro ejemplo vamos a desplazar una capa que tengamos en la página, para ocultarla. De esta manera aplicaremos el efecto [...]]]></description> <content:encoded><![CDATA[<div
class='dd_post_share'><div
class='dd_buttons'><div
class='dd_button'><div
class='dd-google1-ajax-load dd-google1-3657'></div><g:plusone size='medium' href='http://lineadecodigo.com/jquery/efecto-slide-con-jquery/'></g:plusone></div><div
class='dd_button'><script src="http://connect.facebook.net/es_ES/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Flineadecodigo.com%2Fjquery%2Fefecto-slide-con-jquery%2F" send="true" show_faces="false"  layout="button_count" width="92"  ></fb:like></div><div
class='dd_button'><a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://lineadecodigo.com/jquery/efecto-slide-con-jquery/" data-count="horizontal" data-text="Efecto slide con jQuery" data-via="lineadecodigo" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div></div><div
style='clear:both'></div></div><div
style='clear:both'></div><p>El efecto slide es el que nos permite realizar un desplazamiento de un elemento en alguna dirección (izquierda, derecha, arriba o abajo). En las siguientes líneas veremos lo sencillo que es hacerlo con <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a>.</p><p>En nuestro ejemplo vamos a desplazar una capa que tengamos en la página, para ocultarla. De esta manera aplicaremos el efecto slide en ese momento.</p><p>Lo primero que tenemos que saber es que la función que oculta un elemento en <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a> es <a
href="http://www.w3api.com/wiki/jQuery:Hide%28%29" title="Efecto hide en jQuery">.hide()</a>. Pero si vemos la sintaxis (la más común) de esta función:</p><pre class="jquery" style="font-family:monospace;">.<a href="http://w3api.com/wiki/jQuery:hide%28%29"><span style="color: #000066;">hide</span></a><span style="color: #009900;">&#40;</span>velocidad<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span>funcion_callback<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span></pre><p>Nos damos cuenta que solo podemos aplicar una velocidad de ocultación y una función de callback para ejecutar cuando se termine la ocultación. Entonces, ¿dónde ejecuto el efecto?</p><p>Para ello tenemos la librería <a
href="http://www.w3api.com/wiki/Categor%C3%ADa:JQuery_UI" title="Librería jQuery UI">jQuery UI</a>, que es una extensión al core de <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a>, dotándole de capacidades gráficas como la de los efectos.</p><p>Así en <a
href="http://www.w3api.com/wiki/Categor%C3%ADa:JQuery_UI" title="Librería jQuery UI">jQuery UI</a> encontraremos una extensión de la función <a
href="http://www.w3api.com/wiki/jQuery:Hide%28%29" title="Efecto hide en jQuery">.hide()</a> para que podamos aplicar efectos. Lo mismo sucede con las funciones <a
href="http://www.w3api.com/wiki/jQuery:Show()" title="Efecto Show de jQuery">.show()</a> y <a
href="http://www.w3api.com/wiki/jQuery:Toggle()" title="Efecto Toggle de jQuery">.toggle()</a>. De esta manera la función <a
href="http://www.w3api.com/wiki/jQuery:Hide%28%29" title="Efecto hide en jQuery">.hide()</a> podrá recibir un efecto como parámetro.</p><p>El efecto se compondrá de dos partes:</p><ul><li>Nombre del efecto</li><li>Parámetros del efecto</li></ul><p>En el caso del <a
href="http://www.w3api.com/wiki/jQuery:Slide" title="Efecto Slide de jQuery UI">efecto slide</a> podemos indicar como parámetros la dirección (izquierda, arriba,...), distancia (punto del elemento en el que se aplicará el efecto) y el modo (si es show o hide).</p><p>De esa manera nuestro efecto saliendo hacía la izquierda sería algo así:</p><pre class="jquery" style="font-family:monospace;"><span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:hide%28%29"><span style="color: #000066;">hide</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;slide&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> direction<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;left&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>Dónde this será el elemento sobre el que aplicamos el efecto. Por ejemplo, si queremos desplazar una capa:</p><pre class="html4strict" style="font-family:monospace;">&nbsp;
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:DIV"><span style="color: #000000; font-weight: bold;">div</span></a> <a href="http://w3api.com/wiki/HTML:id"><span style="color: #000066;">id</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;micapa&quot;</span> <a href="http://w3api.com/wiki/HTML:style"><span style="color: #000066;">style</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background-color:red;width:100px;height:100px&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:DIV"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span>
&nbsp;</pre><p>El código <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a> nos quedará de la siguiente forma:</p><pre class="jquery" style="font-family:monospace;"><span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:ready%28%29"><span style="color: #000066;">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#micapa&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:click%28%29"><span style="color: #000066;">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:hide%28%29"><span style="color: #000066;">hide</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;slide&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> direction<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;left&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>Hemos utilizado el método <a
href="http://www.w3api.com/wiki/jQuery:Click()" title="Funcion Click de jQuery">.click()</a> para lanzar el efecto cuando se pulse sobre la capa.</p><p>Con esos sencillos pasos tenemos completado el efecto slide con <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a>.<strong>Similar Posts:</strong><ul
class="similar-posts"><li><a
href="http://lineadecodigo.com/jquery/usando-cookies-con-jquery/" rel="bookmark" title="Diciembre 28, 2009">Usando cookies con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/ocultar-y-mostrar-elementos-con-jquery/" rel="bookmark" title="Febrero 5, 2009">Ocultar y mostrar elementos con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/efecto-leer-mas-texto-simplificado-con-jquery/" rel="bookmark" title="Enero 6, 2012">Efecto leer más texto simplificado con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/efecto-leer-mas-texto-con-jquery/" rel="bookmark" title="Enero 4, 2012">Efecto leer más texto con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/filtrar-elementos-de-un-array-con-jquery/" rel="bookmark" title="Abril 4, 2011">Filtrar elementos de un array con jQuery</a></li></ul><p></p> <script type="text/javascript">jQuery(document).ready(function($) { window.setTimeout('loadGoogle1_3657()',1000); });</script><script type="text/javascript">function loadGoogle1_3657(){ jQuery(document).ready(function($) { $('.dd-google1-3657').remove();$.getScript('https://apis.google.com/js/plusone.js'); }); }</script>]]></content:encoded> <wfw:commentRss>http://lineadecodigo.com/jquery/efecto-slide-con-jquery/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Efecto leer más texto con jQuery</title><link>http://lineadecodigo.com/jquery/efecto-leer-mas-texto-con-jquery/</link> <comments>http://lineadecodigo.com/jquery/efecto-leer-mas-texto-con-jquery/#comments</comments> <pubDate>Wed, 04 Jan 2012 07:00:08 +0000</pubDate> <dc:creator>Víctor Cuervo</dc:creator> <category><![CDATA[jQuery]]></category> <category><![CDATA[DIV]]></category> <category><![CDATA[hide]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[id]]></category> <category><![CDATA[ready]]></category> <category><![CDATA[show]]></category> <category><![CDATA[siblings]]></category> <category><![CDATA[span]]></category> <category><![CDATA[substr]]></category> <category><![CDATA[text]]></category> <category><![CDATA[toggle]]></category> <guid
isPermaLink="false">http://lineadecodigo.com/?p=3638</guid> <description><![CDATA[En el ejemplo de hoy vamos a ver como podemos codificar el efecto leer más texto con jQuery. La idea es tener un texto, del cual solo mostraremos su contenido inicial y un enlace que indique "leer más". Al pulsar sobre el enlace "leer más" se producirá el efecto leer más texto que consiste en [...]]]></description> <content:encoded><![CDATA[<div
class='dd_post_share'><div
class='dd_buttons'><div
class='dd_button'><div
class='dd-google1-ajax-load dd-google1-3638'></div><g:plusone size='medium' href='http://lineadecodigo.com/jquery/efecto-leer-mas-texto-con-jquery/'></g:plusone></div><div
class='dd_button'><script src="http://connect.facebook.net/es_ES/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Flineadecodigo.com%2Fjquery%2Fefecto-leer-mas-texto-con-jquery%2F" send="true" show_faces="false"  layout="button_count" width="92"  ></fb:like></div><div
class='dd_button'><a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://lineadecodigo.com/jquery/efecto-leer-mas-texto-con-jquery/" data-count="horizontal" data-text="Efecto leer más texto con jQuery" data-via="lineadecodigo" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div></div><div
style='clear:both'></div></div><div
style='clear:both'></div><p>En el ejemplo de hoy vamos a ver como podemos codificar el efecto leer más texto con <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a>. La idea es tener un texto, del cual solo mostraremos su contenido inicial y un enlace que indique "leer más". Al pulsar sobre el enlace "leer más" se producirá el efecto leer más texto que consiste en mostrar el contenido completo del texto.</p><p>Seguro que habéis visto el efecto leer más texto en muchas web, como por ejemplo en los comentarios de <a
href="http://www.youtube.com" title="Youtube">YouTube</a>.</p><p>Lo primero será poner el texto en nuestra página. El texto será completo y se encontrará dentro de una capa <a
href="http://www.w3api.com/wiki/HTML:DIV" title="Elemento DIV de HTML">DIV</a>.</p><pre class="html4strict" style="font-family:monospace;">&nbsp;
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:DIV"><span style="color: #000000; font-weight: bold;">div</span></a> <a href="http://w3api.com/wiki/HTML:class"><span style="color: #000066;">class</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;comentario&quot;</span>&gt;</span>
	Lorem ipsum dolor sit amet, vis alterum feugiat sanctus ea. Affert facete eu cum, ut dicta vitae quo. Cum id aeque option epicuri, nec oblique suscipit ad. Et sea odio adhuc altera. Verterem nominati an mel, his diam consetetur ei. Eligendi principes sed no, ubique patrioque vix cu, soluta veritus percipit vim ex.<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:BR"><span style="color: #000000; font-weight: bold;">br</span></a>&gt;</span>
	Pro populo dolorem eu. Justo conclusionemque sea ne. At dicat putent eum. Reprimique delicatissimi vel et.<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:BR"><span style="color: #000000; font-weight: bold;">br</span></a>&gt;</span>
	Posse constituam pro eu. Sit graecis appetere at, id his quas possit platonem, ius at nobis decore ullamcorper. Vix ne latine iudicabit constituto, justo fastidii eleifend et pro, ei sit solet dicant mediocrem. Te sit cibo percipit persecuti. Debet convenire suavitate ei vis. Pri utinam labitur temporibus ex.
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:DIV"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span>
&nbsp;</pre><p>Siempre que utilicemos <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a> es bueno el utilizar clases o indicadores para acceder a los elementos. En este caso hemos utilizado el <a
href="http://www.w3api.com/wiki/HTML:Id" title="Atributo id de HTML">id</a> "comentario" para identificar claramente nuestro texto.</p><p>Ahora ya vamos con nuestro código <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a>, el cual colocaremos en la cabecera de nuestra página dentro del método <a
href="http://www.w3api.com/wiki/jQuery:Ready%28%29" title="Método ready de jQuery">.ready()</a>.</p><blockquote><p>Acordaros que el método <a
href="http://www.w3api.com/wiki/jQuery:Ready%28%29" title="Método ready de jQuery">.ready()</a> se ejecuta una vez que se ha cargado toda la página, asegurando así la consistencia de nuestro código a la hora de acceder a los elementos.</p></blockquote><p>Lo primero que hacemos es establecer el número de caracteres que se previsualizarán y extraeremos la subcadena que representan del contenido del texto. El contenido nos lo dará el método <a
href="http://www.w3api.com/wiki/jQuery:Html%28%29" title="Método html de jQuery">.html()</a> sobre la capa "comentario" y la subcadena la conseguimos mediante el método <a
href="http://www.manualweb.net/tutorial-javascript/" title="Manual de JavaScript">Javascript</a> <a
href="http://www.w3api.com/wiki/Javascript:String.substr%28%29" title="Método substr de Javascript">.substr()</a>.</p><pre class="jquery" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> caracteresAMostrar <span style="color: #339933;">=</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> contenido <span style="color: #339933;">=</span> <span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.comentario&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:html"><span style="color: #000066;">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>contenido.<a href="http://w3api.com/wiki/jQuery:length"><span style="color: #000066;">length</span></a> <span style="color: #339933;">&gt;</span> caracteresAMostrar<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> resumen <span style="color: #339933;">=</span> contenido.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> caracteresAMostrar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> todo <span style="color: #339933;">=</span> contenido.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span>caracteresAMostrar<span style="color: #339933;">,</span> contenido.<a href="http://w3api.com/wiki/jQuery:length"><span style="color: #000066;">length</span></a> <span style="color: #339933;">-</span> caracteresAMostrar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre><p>En la variable resumen dejamos el resumen de texto a mostrar, mientras que en la variable todo dejamos el resto del contenido.</p><p>Ahora, con estos textos vamos a modificar la estructura de la página (si nos hubiesen dado esta estructura previa nos ahorraríamos estos pasos, pero hemos preferido partir de un texto completo).</p><p>En la nueva estructuración crearemos dentro del <a
href="http://www.w3api.com/wiki/HTML:DIV" title="Elemento DIV de HTML">DIV</a> el texto, un <a
href="http://www.w3api.com/wiki/HTML:SPAN" title="Elemento span de HTML">SPAN</a> donde tendremos el resto del texto y otro <a
href="http://www.w3api.com/wiki/HTML:SPAN" title="Elemento span de HTML">SPAN</a> con la frase Leer Más. Luego veremos que este texto lo modificamos dependiendo de si hay que "Leer más o leer menos".</p><pre class="jquery" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> nuevocontenido <span style="color: #339933;">=</span> resumen <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;span class=&quot;complete&quot;&gt;'</span> <span style="color: #339933;">+</span> todo <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;more&quot;&gt;Leer mas...&lt;/span&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.comentario&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:html"><span style="color: #000066;">html</span></a><span style="color: #009900;">&#40;</span>nuevocontenido<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>El código <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a> de esta parte queda de la siguiente forma:</p><pre class="jquery" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> caracteresAMostrar <span style="color: #339933;">=</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> contenido <span style="color: #339933;">=</span> <span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.comentario&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:html"><span style="color: #000066;">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>contenido.<a href="http://w3api.com/wiki/jQuery:length"><span style="color: #000066;">length</span></a> <span style="color: #339933;">&gt;</span> caracteresAMostrar<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> resumen <span style="color: #339933;">=</span> contenido.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> caracteresAMostrar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> todo <span style="color: #339933;">=</span> contenido.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span>caracteresAMostrar<span style="color: #339933;">,</span> contenido.<a href="http://w3api.com/wiki/jQuery:length"><span style="color: #000066;">length</span></a> <span style="color: #339933;">-</span> caracteresAMostrar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> nuevocontenido <span style="color: #339933;">=</span> resumen <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;span class=&quot;complete&quot;&gt;'</span> <span style="color: #339933;">+</span> todo <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;more&quot;&gt;Leer mas...&lt;/span&gt;'</span><span style="color: #339933;">;</span>
   <span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.comentario&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:html"><span style="color: #000066;">html</span></a><span style="color: #009900;">&#40;</span>nuevocontenido<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre><p>Ahora tenemos que codificar el efecto leer más texto en <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a>. Y este paso es sencillo. Ya que si pinchamos sobre "leer más" habrá que hacer visible la parte definida como "more" y poner un texto "leer menos". Y si pulsamos sobre "leer menos" realizaremos el efecto contrario, que será esconder "more" y poner el texto "leer más".</p><p>Como estamos hablando de un cambio de estado de un elemento. De nuestro elemento "more". Lo que haremos será apoyarnos en el método <a
href="http://www.w3api.com/wiki/jQuery:Toggle%28%29" title="Función toggle de jQuery">.toggle()</a>, el cual efectua una acción u otra cada vez que pulsemos sobre el elemento.</p><p>El código <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a> quedará de la siguiente forma:</p><pre class="jquery" style="font-family:monospace;"><span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.more&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:toggle%28%29"><span style="color: #000066;">toggle</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:text"><span style="color: #000066;">text</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Leer menos...&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:siblings"><span style="color: #000066;">siblings</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.complete&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:show%28%29"><span style="color: #000066;">show</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:text"><span style="color: #000066;">text</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Leer mas...&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:siblings"><span style="color: #000066;">siblings</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.complete&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:hide%28%29"><span style="color: #000066;">hide</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>Vemos que utilizamos varios métodos de <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a>:</p><ul><li><strong><a
href="http://www.w3api.com/wiki/jQuery:Text%28%29" title="Función text de jQuery">.text()</a></strong>, nos permite cambiar el texto del elemento por "leer menos" o "leer más".</li><li><strong><a
href="http://www.w3api.com/wiki/jQuery:Siblings%28%29" title="Función siblings de jQuery">.siblings()</a></strong>, nos permite acceder a los elementos que está en nuestra misma estructura del DOM, el id se lo pasamos por parámetro.</li><li><strong><a
href="http://www.w3api.com/wiki/jQuery:Show%28%29" title="Función show de jQuery">.show()</a></strong>, muestra el contenido del elemento.</li><li><strong><a
href="http://www.w3api.com/wiki/jQuery:Hide%28%29" title="Función Hide de jQuery">.hide()</a></strong>, oculta el contenido del elemento.</li></ul><p>Con esto ya tenemos todo nuestro código <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a> y el efecto leer más texto conseguido.<strong>Similar Posts:</strong><ul
class="similar-posts"><li><a
href="http://lineadecodigo.com/jquery/efecto-leer-mas-texto-simplificado-con-jquery/" rel="bookmark" title="Enero 6, 2012">Efecto leer más texto simplificado con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/volcar-el-contenido-de-un-array-con-jquery/" rel="bookmark" title="Mayo 31, 2011">Volcar el contenido de un array con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/efecto-slide-con-jquery/" rel="bookmark" title="Enero 5, 2012">Efecto slide con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/modificar-el-contenido-de-una-capa-con-jquery/" rel="bookmark" title="Mayo 27, 2010">Modificar el contenido de una capa con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/acceder-a-un-campo-de-formulario-con-jquery/" rel="bookmark" title="Junio 7, 2011">Acceder a un campo de formulario con jQuery</a></li></ul><p></p> <script type="text/javascript">jQuery(document).ready(function($) { window.setTimeout('loadGoogle1_3638()',1000); });</script><script type="text/javascript">function loadGoogle1_3638(){ jQuery(document).ready(function($) { $('.dd-google1-3638').remove();$.getScript('https://apis.google.com/js/plusone.js'); }); }</script>]]></content:encoded> <wfw:commentRss>http://lineadecodigo.com/jquery/efecto-leer-mas-texto-con-jquery/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Seleccionar un texto solo si no ha cambiado</title><link>http://lineadecodigo.com/jquery/seleccionar-un-texto-solo-si-no-ha-cambiado/</link> <comments>http://lineadecodigo.com/jquery/seleccionar-un-texto-solo-si-no-ha-cambiado/#comments</comments> <pubDate>Tue, 11 Oct 2011 06:00:48 +0000</pubDate> <dc:creator>Víctor Cuervo</dc:creator> <category><![CDATA[jQuery]]></category> <category><![CDATA[defaultValue]]></category> <category><![CDATA[focus]]></category> <category><![CDATA[input]]></category> <category><![CDATA[SELECT]]></category> <category><![CDATA[textarea]]></category> <category><![CDATA[value]]></category> <guid
isPermaLink="false">http://lineadecodigo.com/?p=3559</guid> <description><![CDATA[Unos cuantos han sido los artículos que le hemos dedicado a la selección de texto con jQuery: Seleccionar el texto de un campo, Seleccionar el texto al posicionarse en un campo y Detectar el texto seleccionado con jQuery. Pero no quería dejar de escribir uno que nos va a dar un buen efecto visual y [...]]]></description> <content:encoded><![CDATA[<div
class='dd_post_share'><div
class='dd_buttons'><div
class='dd_button'><div
class='dd-google1-ajax-load dd-google1-3559'></div><g:plusone size='medium' href='http://lineadecodigo.com/jquery/seleccionar-un-texto-solo-si-no-ha-cambiado/'></g:plusone></div><div
class='dd_button'><script src="http://connect.facebook.net/es_ES/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Flineadecodigo.com%2Fjquery%2Fseleccionar-un-texto-solo-si-no-ha-cambiado%2F" send="true" show_faces="false"  layout="button_count" width="92"  ></fb:like></div><div
class='dd_button'><a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://lineadecodigo.com/jquery/seleccionar-un-texto-solo-si-no-ha-cambiado/" data-count="horizontal" data-text="Seleccionar un texto solo si no ha cambiado" data-via="lineadecodigo" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div></div><div
style='clear:both'></div></div><div
style='clear:both'></div><p>Unos cuantos han sido los artículos que le hemos dedicado a la selección de texto con jQuery: <a
href="http://lineadecodigo.com/jquery/seleccionar-el-texto-de-un-campo/" title="Seleccionar el texto de un campo con jQuery">Seleccionar el texto de un campo</a>, <a
href="http://lineadecodigo.com/jquery/seleccionar-texto-al-posicionarse-en-un-campo/" title="Seleccionar texto al posicionarse en un campo con jQuery">Seleccionar el texto al posicionarse en un campo</a> y <a
href="http://lineadecodigo.com/jquery/detectar-texto-seleccionado-con-jquery/" title="Detectar el texto seleccionado con jQuery">Detectar el texto seleccionado con jQuery</a>. Pero no quería dejar de escribir uno que nos va a dar un buen efecto visual y este consiste en seleccionar un texto solo si este no ha cambiado con <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a>.</p><p>Es decir, seleccionaremos el texto del campo si sigue siendo el mismo que al cargar la página. Si el usuario ha cambiado el contenido, ya no lo seleccionaremos.</p><p>Manos a la obra... Lo primero el formulario al uso (casi que ya lo imaginas).</p><pre class="html4strict" style="font-family:monospace;">&nbsp;
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:FORM"><span style="color: #000000; font-weight: bold;">form</span></a> <a href="http://w3api.com/wiki/HTML:id"><span style="color: #000066;">id</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;miform&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:LABEL"><span style="color: #000000; font-weight: bold;">label</span></a> <a href="http://w3api.com/wiki/HTML:for"><span style="color: #000066;">for</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;texto&quot;</span>&gt;</span>Inserta texto:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:LABEL"><span style="color: #000000; font-weight: bold;">label</span></a>&gt;</span>
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:INPUT"><span style="color: #000000; font-weight: bold;">input</span></a> <a href="http://w3api.com/wiki/HTML:id"><span style="color: #000066;">id</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;texto&quot;</span> <a href="http://w3api.com/wiki/HTML:type"><span style="color: #000066;">type</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <a href="http://w3api.com/wiki/HTML:value"><span style="color: #000066;">value</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Texto por defecto&quot;</span> <a href="http://w3api.com/wiki/HTML:size"><span style="color: #000066;">size</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100&quot;</span><span style="color: #66cc66;">/</span>&gt;&lt;<a href="http://w3api.com/wiki/HTML:BR"><span style="color: #000000; font-weight: bold;">br</span></a><span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:LABEL"><span style="color: #000000; font-weight: bold;">label</span></a> <a href="http://w3api.com/wiki/HTML:for"><span style="color: #000066;">for</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;areatexto&quot;</span>&gt;</span>Cada vez que pinches en el <span style="color: #ddbb00;">&amp;aacute;</span>rea de texto se selecionará. Esto sucederá mientras no cambies el contenido original del mismo.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:LABEL"><span style="color: #000000; font-weight: bold;">label</span></a>&gt;&lt;<a href="http://w3api.com/wiki/HTML:BR"><span style="color: #000000; font-weight: bold;">br</span></a><span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:TEXTAREA"><span style="color: #000000; font-weight: bold;">textarea</span></a> <a href="http://w3api.com/wiki/HTML:id"><span style="color: #000066;">id</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;areatexto&quot;</span> <a href="http://w3api.com/wiki/HTML:cols"><span style="color: #000066;">cols</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;25&quot;</span> <a href="http://w3api.com/wiki/HTML:rows"><span style="color: #000066;">rows</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;8&quot;</span>&gt;</span>Texto por defecto<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:TEXTAREA"><span style="color: #000000; font-weight: bold;">textarea</span></a>&gt;&lt;<a href="http://w3api.com/wiki/HTML:BR"><span style="color: #000000; font-weight: bold;">br</span></a><span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:FORM"><span style="color: #000000; font-weight: bold;">form</span></a>&gt;</span>
&nbsp;</pre><p>Hay que fijarse bien que hemos puesto los campos <a
href="http://www.w3api.com/wiki/HTML:INPUT" title="elemento input de HTML">input</a> y <a
href="http://www.w3api.com/wiki/HTML:TEXTAREA" title="elemento textarea de HTML">textarea</a> con textos por defecto.</p><p>Será cuándo el usuario acceda al campo cuando comprobaremos si ha cambiado el texto. Para ello utilizamos el método <a
href="http://www.w3api.com/wiki/jQuery:Focus%28%29" title="metodo focus de jQuery">.focus()</a> de <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a>. En este caso vamos a ver cómo sería para el <a
href="http://www.w3api.com/wiki/HTML:TEXTAREA" title="elemento textarea de HTML">textarea</a>.</p><pre class="jquery" style="font-family:monospace;"><span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:ready%28%29"><span style="color: #000066;">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;textarea&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:focus%28%29"><span style="color: #000066;">focus</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>...<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>Ahora tenemos que diferenciar como conseguir el texto que tiene actualmente y el texto que estaba por defecto. El texto actual lo conseguiremos con .value y el texto por defecto con .defaultValue.</p><p>Así que seleccionaremos el texto, es decir, lamaremos al método <a
href="http://www.w3api.com/wiki/jQuery:Select%28%29" title="método select de jQuery">.select()</a> si son iguales.</p><pre class="jquery" style="font-family:monospace;"><span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:ready%28%29"><span style="color: #000066;">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;textarea&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://w3api.com/wiki/jQuery:focus%28%29"><span style="color: #000066;">focus</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">defaultValue</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<a href="http://w3api.com/wiki/jQuery:select%28%29"><span style="color: #000066;">select</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>Así ya hemos seleccionado el texto solo si este no ha cambiado.<strong>Similar Posts:</strong><ul
class="similar-posts"><li><a
href="http://lineadecodigo.com/jquery/seleccionar-texto-al-posicionarse-en-un-campo/" rel="bookmark" title="Septiembre 23, 2011">Seleccionar texto al posicionarse en un campo</a></li><li><a
href="http://lineadecodigo.com/jquery/detectar-texto-seleccionado-con-jquery/" rel="bookmark" title="Septiembre 20, 2011">Detectar texto seleccionado con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/seleccionar-el-texto-de-un-campo/" rel="bookmark" title="Septiembre 21, 2011">Seleccionar el texto de un campo</a></li><li><a
href="http://lineadecodigo.com/jquery/cambios-en-un-combo-con-jquery/" rel="bookmark" title="Mayo 17, 2011">Cambios en un combo con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/acceder-a-un-campo-de-formulario-con-jquery/" rel="bookmark" title="Junio 7, 2011">Acceder a un campo de formulario con jQuery</a></li></ul><p></p> <script type="text/javascript">jQuery(document).ready(function($) { window.setTimeout('loadGoogle1_3559()',1000); });</script><script type="text/javascript">function loadGoogle1_3559(){ jQuery(document).ready(function($) { $('.dd-google1-3559').remove();$.getScript('https://apis.google.com/js/plusone.js'); }); }</script>]]></content:encoded> <wfw:commentRss>http://lineadecodigo.com/jquery/seleccionar-un-texto-solo-si-no-ha-cambiado/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Listar opciones seleccionadas de un combo con jQuery</title><link>http://lineadecodigo.com/jquery/listar-opciones-seleccionadas-de-un-combo-con-jquery/</link> <comments>http://lineadecodigo.com/jquery/listar-opciones-seleccionadas-de-un-combo-con-jquery/#comments</comments> <pubDate>Sat, 24 Sep 2011 06:00:09 +0000</pubDate> <dc:creator>Víctor Cuervo</dc:creator> <category><![CDATA[jQuery]]></category> <category><![CDATA[change]]></category> <category><![CDATA[Combo]]></category> <category><![CDATA[DIV]]></category> <category><![CDATA[formularios]]></category> <category><![CDATA[multiple]]></category> <category><![CDATA[OPTION]]></category> <category><![CDATA[SELECT]]></category> <category><![CDATA[selected]]></category> <category><![CDATA[text]]></category> <category><![CDATA[trigger]]></category> <guid
isPermaLink="false">http://lineadecodigo.com/?p=3491</guid> <description><![CDATA[jQuery nos permite manejar de forma muy sencilla los formularios con un amplio conjunto de métodos. En este caso vamos a ver cómo podemos acceder a la lista de opciones seleccionadas de un combo y mostrarlas por pantalla utilizando la librería jQuery. En primer lugar vamos a pintar el combo (esta vez va de fútbol): [...]]]></description> <content:encoded><![CDATA[<div
class='dd_post_share'><div
class='dd_buttons'><div
class='dd_button'><div
class='dd-google1-ajax-load dd-google1-3491'></div><g:plusone size='medium' href='http://lineadecodigo.com/jquery/listar-opciones-seleccionadas-de-un-combo-con-jquery/'></g:plusone></div><div
class='dd_button'><script src="http://connect.facebook.net/es_ES/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Flineadecodigo.com%2Fjquery%2Flistar-opciones-seleccionadas-de-un-combo-con-jquery%2F" send="true" show_faces="false"  layout="button_count" width="92"  ></fb:like></div><div
class='dd_button'><a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://lineadecodigo.com/jquery/listar-opciones-seleccionadas-de-un-combo-con-jquery/" data-count="horizontal" data-text="Listar opciones seleccionadas de un combo con jQuery" data-via="lineadecodigo" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div></div><div
style='clear:both'></div></div><div
style='clear:both'></div><p>jQuery nos permite <a
href="http://www.w3api.com/wiki/Categor%C3%ADa:JQuery_Forms" title="funciones jQuery para el manejo de Formularios">manejar de forma muy sencilla los formularios con un amplio conjunto de métodos</a>. En este caso vamos a ver cómo podemos acceder a la lista de opciones seleccionadas de un combo y mostrarlas por pantalla utilizando la librería <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual sobre la librería jQuery">jQuery</a>.</p><p>En primer lugar vamos a pintar el combo (esta vez va de fútbol):</p><pre class="html4strict" style="font-family:monospace;">&nbsp;
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:FORM"><span style="color: #000000; font-weight: bold;">form</span></a> <a href="http://w3api.com/wiki/HTML:id"><span style="color: #000066;">id</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myform&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:SELECT"><span style="color: #000000; font-weight: bold;">select</span></a> <a href="http://w3api.com/wiki/HTML:id"><span style="color: #000066;">id</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;equipos&quot;</span> <a href="http://w3api.com/wiki/HTML:multiple"><span style="color: #000066;">multiple</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;multiple&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:OPTION"><span style="color: #000000; font-weight: bold;">option</span></a> <a href="http://w3api.com/wiki/HTML:value"><span style="color: #000066;">value</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;barça&quot;</span> <a href="http://w3api.com/wiki/HTML:selected"><span style="color: #000066;">selected</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;selected&quot;</span>&gt;</span>Barça<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:OPTION"><span style="color: #000000; font-weight: bold;">option</span></a>&gt;</span>
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:OPTION"><span style="color: #000000; font-weight: bold;">option</span></a> <a href="http://w3api.com/wiki/HTML:value"><span style="color: #000066;">value</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;real madrid&quot;</span>&gt;</span>Real Madrid<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:OPTION"><span style="color: #000000; font-weight: bold;">option</span></a>&gt;</span>
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:OPTION"><span style="color: #000000; font-weight: bold;">option</span></a> <a href="http://w3api.com/wiki/HTML:value"><span style="color: #000066;">value</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;atletico madrid&quot;</span>&gt;</span>Atl<span style="color: #ddbb00;">&amp;eacute;</span>tico Madrid<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:OPTION"><span style="color: #000000; font-weight: bold;">option</span></a>&gt;</span>
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:OPTION"><span style="color: #000000; font-weight: bold;">option</span></a> <a href="http://w3api.com/wiki/HTML:value"><span style="color: #000066;">value</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;valencia&quot;</span>&gt;</span>Valencia CF<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:OPTION"><span style="color: #000000; font-weight: bold;">option</span></a>&gt;</span>
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:OPTION"><span style="color: #000000; font-weight: bold;">option</span></a> <a href="http://w3api.com/wiki/HTML:value"><span style="color: #000066;">value</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;betis&quot;</span> <a href="http://w3api.com/wiki/HTML:selected"><span style="color: #000066;">selected</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;selected&quot;</span>&gt;</span>Real Betis<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:OPTION"><span style="color: #000000; font-weight: bold;">option</span></a>&gt;</span>
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:OPTION"><span style="color: #000000; font-weight: bold;">option</span></a> <a href="http://w3api.com/wiki/HTML:value"><span style="color: #000066;">value</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;deportivo&quot;</span>&gt;</span>Deportivo de la Coruña<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:OPTION"><span style="color: #000000; font-weight: bold;">option</span></a>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:SELECT"><span style="color: #000000; font-weight: bold;">select</span></a>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:FORM"><span style="color: #000000; font-weight: bold;">form</span></a>&gt;</span>
&nbsp;</pre><p>Si queremos que puedan existir varias opciones seleccionadas dentro del combo deberemos de utilizar el atributo <a
href="http://www.w3api.com/wiki/HTML:Multiple" title="atributo multiple de HTML">multiple="multiple"</a> dentro del <a
href="http://www.w3api.com/wiki/HTML:SELECT" title="elemento HTML select">select</a>. De igual manera, si queremos prefijar una de las opciones deberemos de utilizar el atributo <a
href="http://www.w3api.com/wiki/HTML:Selected" title="atributo HTML selected">selected="selected"</a>.</p><p>Listaremos las opciones seleccionadas del combo cada vez que haya un cambio en dicho combo. Para ello vamos a manejar el método <a
href="http://www.w3api.com/wiki/jQuery:Change()" title="método change de jQuery">.change()</a> de <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual sobre la librería jQuery">jQuery</a>. En cada cambio ejecutaremos la función que le digamos al método <a
href="http://www.w3api.com/wiki/jQuery:Change()" title="método change de jQuery">.change()</a>.</p><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#equipos&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    ...
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>Y ¿qué va a hacer nuestra función? Pues recorrer el elemento <a
href="http://www.w3api.com/wiki/HTML:SELECT" title="elemento HTML select">select</a>. Pero no todo el elemento <a
href="http://www.w3api.com/wiki/HTML:SELECT" title="elemento HTML select">select</a>, si no aquellas opciones que estén seleccionadas. Así que vamos a utilizar un selector. El selector será:</p><pre>#equipos option:selected</pre><p>Es decir, del combo o <a
href="http://www.w3api.com/wiki/HTML:SELECT" title="elemento HTML select">select</a> 'equipos' elige aquellas opciones, u <a
href="http://www.w3api.com/wiki/HTML:OPTION" title="elemento option de HTML">option</a>, que estén seleccionadas, es decir <a
href="http://www.w3api.com/wiki/HTML:Selected" title="atributo selected de HTML">'selected'</a>.</p><p>Ahora, de cada opción obtendremos su valor mediante el método <a
href="http://www.w3api.com/wiki/jQuery:Text%28%29" title="método jQuery text">.text()</a></p><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#equipos option:selected&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  texto <span style="color: #339933;">+=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; - &quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>Ya solo nos quedará volcar el contenido en alguna capa de nuestra página. Para ello creamos un elemento <a
href="http://www.w3api.com/wiki/HTML:DIV" title="elemento DIV de HTML">DIV</a> al que llamaremos 'resultado' y al que volcaremos los datos mediante:</p><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#resultado&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>texto<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>Solo nos queda un pequeño detalle. Y es que si hemos puesto campos seleccionados de antemano, si queremos que se ejecute nuestro método <a
href="http://www.w3api.com/wiki/jQuery:Change()" title="método change de jQuery">.change()</a>, podemos simular el ejecutarlo mediante el método <a
href="http://www.w3api.com/wiki/jQuery:Trigger%28%29" title="Método jQuery trigger">.trigger()</a></p><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#equipos&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">trigger</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'change'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>De esta manera tendremos la primera ejecución del <a
href="http://www.w3api.com/wiki/jQuery:Change()" title="método change de jQuery">.change()</a>. El código entero nos quedará de la siguiente forma:</p><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#equipos&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> texto <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Opciones Seleccionadas: &quot;</span><span style="color: #339933;">;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#equipos option:selected&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      texto <span style="color: #339933;">+=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; - &quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#resultado&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>texto<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">trigger</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'change'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p><strong>Similar Posts:</strong><ul
class="similar-posts"><li><a
href="http://lineadecodigo.com/jquery/cambios-en-un-combo-con-jquery/" rel="bookmark" title="Mayo 17, 2011">Cambios en un combo con jQuery</a></li><li><a
href="http://lineadecodigo.com/html/predeterminar-valor-en-un-combo/" rel="bookmark" title="Noviembre 16, 2006">Predeterminar valor en un combo</a></li><li><a
href="http://lineadecodigo.com/html/combos-de-seleccion/" rel="bookmark" title="Noviembre 2, 2006">Combos de selección</a></li><li><a
href="http://lineadecodigo.com/javascript/activar-valores-de-un-combo-dinamicamente/" rel="bookmark" title="Septiembre 16, 2009">Activar valores de un combo dinámicamente</a></li><li><a
href="http://lineadecodigo.com/jquery/detectar-texto-seleccionado-con-jquery/" rel="bookmark" title="Septiembre 20, 2011">Detectar texto seleccionado con jQuery</a></li></ul><p></p> <script type="text/javascript">jQuery(document).ready(function($) { window.setTimeout('loadGoogle1_3491()',1000); });</script><script type="text/javascript">function loadGoogle1_3491(){ jQuery(document).ready(function($) { $('.dd-google1-3491').remove();$.getScript('https://apis.google.com/js/plusone.js'); }); }</script>]]></content:encoded> <wfw:commentRss>http://lineadecodigo.com/jquery/listar-opciones-seleccionadas-de-un-combo-con-jquery/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Seleccionar texto al posicionarse en un campo</title><link>http://lineadecodigo.com/jquery/seleccionar-texto-al-posicionarse-en-un-campo/</link> <comments>http://lineadecodigo.com/jquery/seleccionar-texto-al-posicionarse-en-un-campo/#comments</comments> <pubDate>Fri, 23 Sep 2011 06:00:09 +0000</pubDate> <dc:creator>Víctor Cuervo</dc:creator> <category><![CDATA[jQuery]]></category> <category><![CDATA[focus]]></category> <category><![CDATA[formulario]]></category> <category><![CDATA[input]]></category> <category><![CDATA[selección]]></category> <category><![CDATA[SELECT]]></category> <category><![CDATA[texto]]></category> <guid
isPermaLink="false">http://lineadecodigo.com/?p=3481</guid> <description><![CDATA[Si el otro día vimos como seleccionar el texto de un campo (por defecto o mediante un click del usuario), hoy vamos a ver como seleccionar texto al posicionarse en un campo con jQuery. Es decir, al ir moviéndonos por los campos y llegar al determinado campo seleccionaremos el texto. Controlar el posicionarse en un [...]]]></description> <content:encoded><![CDATA[<div
class='dd_post_share'><div
class='dd_buttons'><div
class='dd_button'><div
class='dd-google1-ajax-load dd-google1-3481'></div><g:plusone size='medium' href='http://lineadecodigo.com/jquery/seleccionar-texto-al-posicionarse-en-un-campo/'></g:plusone></div><div
class='dd_button'><script src="http://connect.facebook.net/es_ES/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Flineadecodigo.com%2Fjquery%2Fseleccionar-texto-al-posicionarse-en-un-campo%2F" send="true" show_faces="false"  layout="button_count" width="92"  ></fb:like></div><div
class='dd_button'><a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://lineadecodigo.com/jquery/seleccionar-texto-al-posicionarse-en-un-campo/" data-count="horizontal" data-text="Seleccionar texto al posicionarse en un campo" data-via="lineadecodigo" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div></div><div
style='clear:both'></div></div><div
style='clear:both'></div><p>Si <a
href="http://lineadecodigo.com/jquery/seleccionar-el-texto-de-un-campo/" title="seleccionar el texto de un campo con jQuery">el otro día vimos como seleccionar el texto de un campo (por defecto o mediante un click del usuario)</a>, hoy vamos a ver como seleccionar texto al posicionarse en un campo con <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a>. Es decir, al ir moviéndonos por los campos y llegar al determinado campo seleccionaremos el texto.</p><p>Controlar el posicionarse en un campo lo podremos hacer mediante el método <a
href="http://www.w3api.com/wiki/jQuery:Focus()" title="método focus de jQuery">.focus()</a> de jQuery. Pero lo primero, el formulario. Como siempre, algo simple:</p><pre class="html4strict" style="font-family:monospace;">&nbsp;
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:FORM"><span style="color: #000000; font-weight: bold;">form</span></a> <a href="http://w3api.com/wiki/HTML:id"><span style="color: #000066;">id</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;miform&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:LABEL"><span style="color: #000000; font-weight: bold;">label</span></a> <a href="http://w3api.com/wiki/HTML:for"><span style="color: #000066;">for</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;texto&quot;</span>&gt;</span>Inserta texto:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:LABEL"><span style="color: #000000; font-weight: bold;">label</span></a>&gt;</span>
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:INPUT"><span style="color: #000000; font-weight: bold;">input</span></a> <a href="http://w3api.com/wiki/HTML:id"><span style="color: #000066;">id</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;texto&quot;</span> <a href="http://w3api.com/wiki/HTML:type"><span style="color: #000066;">type</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <a href="http://w3api.com/wiki/HTML:value"><span style="color: #000066;">value</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Texto por defecto&quot;</span> <a href="http://w3api.com/wiki/HTML:size"><span style="color: #000066;">size</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100&quot;</span><span style="color: #66cc66;">/</span>&gt;&lt;<a href="http://w3api.com/wiki/HTML:BR"><span style="color: #000000; font-weight: bold;">br</span></a><span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:LABEL"><span style="color: #000000; font-weight: bold;">label</span></a> <a href="http://w3api.com/wiki/HTML:for"><span style="color: #000066;">for</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;texto2&quot;</span>&gt;</span>Inserta texto:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:LABEL"><span style="color: #000000; font-weight: bold;">label</span></a>&gt;</span>
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:INPUT"><span style="color: #000000; font-weight: bold;">input</span></a> <a href="http://w3api.com/wiki/HTML:id"><span style="color: #000066;">id</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;texto2&quot;</span> <a href="http://w3api.com/wiki/HTML:type"><span style="color: #000066;">type</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <a href="http://w3api.com/wiki/HTML:value"><span style="color: #000066;">value</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Texto por defecto 2&quot;</span> <a href="http://w3api.com/wiki/HTML:size"><span style="color: #000066;">size</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100&quot;</span><span style="color: #66cc66;">/</span>&gt;&lt;<a href="http://w3api.com/wiki/HTML:BR"><span style="color: #000000; font-weight: bold;">br</span></a><span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:FORM"><span style="color: #000000; font-weight: bold;">form</span></a>&gt;</span>
&nbsp;</pre><p>Como la selección del texto al posicionarse en un campo lo vamos a aplicar a todos los <a
href="http://www.w3api.com/wiki/HTML:INPUT" title="elemento input de HTML">campos input</a> (de tipo text) vamos a utilizar un selector.</p><pre>input[type=text]</pre><p>Así que el método <a
href="http://www.w3api.com/wiki/jQuery:Focus()" title="método focus de jQuery">.focus()</a> se quedará de la siguiente forma:</p><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input[type=text]&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>Ya solo nos quedará realizar la selección del campo de texto mediante el método <a
href="http://www.w3api.com/wiki/jQuery:Select()" title="metodo select de jQuery">.select()</a> de <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a>.</p><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input[type=text]&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p><strong>Similar Posts:</strong><ul
class="similar-posts"><li><a
href="http://lineadecodigo.com/jquery/seleccionar-un-texto-solo-si-no-ha-cambiado/" rel="bookmark" title="Octubre 11, 2011">Seleccionar un texto solo si no ha cambiado</a></li><li><a
href="http://lineadecodigo.com/jquery/seleccionar-el-texto-de-un-campo/" rel="bookmark" title="Septiembre 21, 2011">Seleccionar el texto de un campo</a></li><li><a
href="http://lineadecodigo.com/jquery/detectar-texto-seleccionado-con-jquery/" rel="bookmark" title="Septiembre 20, 2011">Detectar texto seleccionado con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/modificar-valores-por-defecto-de-un-formulario-con-jquery/" rel="bookmark" title="Marzo 15, 2011">Modificar valores por defecto de un formulario con jQuery</a></li><li><a
href="http://lineadecodigo.com/wforms/todos-los-campos-del-formulario-obligatorios/" rel="bookmark" title="Octubre 28, 2007">Todos los campos del formulario obligatorios</a></li></ul><p></p> <script type="text/javascript">jQuery(document).ready(function($) { window.setTimeout('loadGoogle1_3481()',1000); });</script><script type="text/javascript">function loadGoogle1_3481(){ jQuery(document).ready(function($) { $('.dd-google1-3481').remove();$.getScript('https://apis.google.com/js/plusone.js'); }); }</script>]]></content:encoded> <wfw:commentRss>http://lineadecodigo.com/jquery/seleccionar-texto-al-posicionarse-en-un-campo/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Seleccionar el texto de un campo</title><link>http://lineadecodigo.com/jquery/seleccionar-el-texto-de-un-campo/</link> <comments>http://lineadecodigo.com/jquery/seleccionar-el-texto-de-un-campo/#comments</comments> <pubDate>Wed, 21 Sep 2011 06:00:04 +0000</pubDate> <dc:creator>Víctor Cuervo</dc:creator> <category><![CDATA[jQuery]]></category> <category><![CDATA[button]]></category> <category><![CDATA[click]]></category> <category><![CDATA[formulario]]></category> <category><![CDATA[id]]></category> <category><![CDATA[selección]]></category> <category><![CDATA[SELECT]]></category> <category><![CDATA[texto]]></category> <guid
isPermaLink="false">http://lineadecodigo.com/?p=3477</guid> <description><![CDATA[Seleccionar el texto de un campo nos posibilita guiar al usuario de nuestra web en la edición de textos. La idea es dejarle un campo marcado dentro del formulario. Para ello nos vamos a apoyar en jQuery. Lo primero es poner el formulario. Nada complicado si sabes HTML: &#160; &#60;form id=&#34;miform&#34;&#62; &#60;label for=&#34;texto&#34;&#62;Inserta texto:&#60;/label&#62; &#60;input [...]]]></description> <content:encoded><![CDATA[<div
class='dd_post_share'><div
class='dd_buttons'><div
class='dd_button'><div
class='dd-google1-ajax-load dd-google1-3477'></div><g:plusone size='medium' href='http://lineadecodigo.com/jquery/seleccionar-el-texto-de-un-campo/'></g:plusone></div><div
class='dd_button'><script src="http://connect.facebook.net/es_ES/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Flineadecodigo.com%2Fjquery%2Fseleccionar-el-texto-de-un-campo%2F" send="true" show_faces="false"  layout="button_count" width="92"  ></fb:like></div><div
class='dd_button'><a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://lineadecodigo.com/jquery/seleccionar-el-texto-de-un-campo/" data-count="horizontal" data-text="Seleccionar el texto de un campo" data-via="lineadecodigo" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div></div><div
style='clear:both'></div></div><div
style='clear:both'></div><p>Seleccionar el texto de un campo nos posibilita guiar al usuario de nuestra web en la edición de textos. La idea es dejarle un campo marcado dentro del formulario. Para ello nos vamos a apoyar en <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a>.</p><p>Lo primero es poner el formulario. Nada complicado si sabes <a
href="http://www.manualweb.net/tutorial-html/" title="Tutorial sobre HTML">HTML</a>:</p><pre class="html4strict" style="font-family:monospace;">&nbsp;
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:FORM"><span style="color: #000000; font-weight: bold;">form</span></a> <a href="http://w3api.com/wiki/HTML:id"><span style="color: #000066;">id</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;miform&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:LABEL"><span style="color: #000000; font-weight: bold;">label</span></a> <a href="http://w3api.com/wiki/HTML:for"><span style="color: #000066;">for</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;texto&quot;</span>&gt;</span>Inserta texto:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:LABEL"><span style="color: #000000; font-weight: bold;">label</span></a>&gt;</span>
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:INPUT"><span style="color: #000000; font-weight: bold;">input</span></a> <a href="http://w3api.com/wiki/HTML:id"><span style="color: #000066;">id</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;texto&quot;</span> <a href="http://w3api.com/wiki/HTML:type"><span style="color: #000066;">type</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <a href="http://w3api.com/wiki/HTML:value"><span style="color: #000066;">value</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Texto por defecto&quot;</span> <a href="http://w3api.com/wiki/HTML:size"><span style="color: #000066;">size</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100&quot;</span><span style="color: #66cc66;">/</span>&gt;&lt;<a href="http://w3api.com/wiki/HTML:BR"><span style="color: #000000; font-weight: bold;">br</span></a><span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:TEXTAREA"><span style="color: #000000; font-weight: bold;">textarea</span></a> <a href="http://w3api.com/wiki/HTML:id"><span style="color: #000066;">id</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;areatexto&quot;</span>&gt;</span>Texto por defecto<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:TEXTAREA"><span style="color: #000000; font-weight: bold;">textarea</span></a>&gt;&lt;<a href="http://w3api.com/wiki/HTML:BR"><span style="color: #000000; font-weight: bold;">br</span></a><span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:BUTTON"><span style="color: #000000; font-weight: bold;">button</span></a> <a href="http://w3api.com/wiki/HTML:id"><span style="color: #000066;">id</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;selectall&quot;</span>&gt;</span>Seleccionar todo el texto<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:BUTTON"><span style="color: #000000; font-weight: bold;">button</span></a>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:FORM"><span style="color: #000000; font-weight: bold;">form</span></a>&gt;</span>
&nbsp;</pre><p>Ahora, y tras haber cargado nuestra librería <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a>...</p><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:SCRIPT"><span style="color: #000000; font-weight: bold;">script</span></a> <a href="http://w3api.com/wiki/HTML:type"><span style="color: #000066;">type</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <a href="http://w3api.com/wiki/HTML:src"><span style="color: #000066;">src</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:SCRIPT"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span></pre><p>... nos apoyamos en el método <a
href="http://www.w3api.com/wiki/jQuery:Select()" title="Método Select de jQuery">.select()</a> para aplicarselo a un campo. Por ejemplo si queremos seleccionar el texto del campo con <a
href="http://www.w3api.com/wiki/HTML:Id" title="atributo id de HTML">id</a> 'texto' tendremos que codificar lo siguiente:</p><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#texto&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>Otra opción es hacerlo bajo demanda. Lo que viene a ser el típico botón de "Seleccionar Todo". Que en nuestro caso hemos representado con un <a
href="http://www.w3api.com/wiki/HTML:BUTTON" title="Elemento button de HTML">elemento button</a>.</p><p>En <a
href="http://www.manualweb.net/tutorial-jquery/" title="Manual de jQuery">jQuery</a> conseguiremos capturar el click sobre el botón con el método <a
href="http://www.w3api.com/wiki/jQuery:Click()" title="Método click de jQuery">.click()</a>. Así el código quedará de la siguiente forma:</p><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#selectall&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#areatexto&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>Hay que tener en cuenta dos cosas. Que para acceder a los id de los elementos utilizamos una almohadilla delante del nombre. Y que al final del <a
href="http://www.w3api.com/wiki/jQuery:Click()" title="Método click de jQuery">.click()</a> hemos utilizado un return false para parar el evento generado por el botón.</p><p>Ya hemos visto dos formas muy sencillas de seleccionar el texto de un campo. ¿Se te ocurre otra forma?<strong>Similar Posts:</strong><ul
class="similar-posts"><li><a
href="http://lineadecodigo.com/jquery/seleccionar-texto-al-posicionarse-en-un-campo/" rel="bookmark" title="Septiembre 23, 2011">Seleccionar texto al posicionarse en un campo</a></li><li><a
href="http://lineadecodigo.com/jquery/seleccionar-un-texto-solo-si-no-ha-cambiado/" rel="bookmark" title="Octubre 11, 2011">Seleccionar un texto solo si no ha cambiado</a></li><li><a
href="http://lineadecodigo.com/jquery/acceder-a-un-campo-de-formulario-con-jquery/" rel="bookmark" title="Junio 7, 2011">Acceder a un campo de formulario con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/bloquear-el-envio-de-un-formulario-con-jquery/" rel="bookmark" title="Abril 3, 2011">Bloquear el envío de un formulario con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/detectar-texto-seleccionado-con-jquery/" rel="bookmark" title="Septiembre 20, 2011">Detectar texto seleccionado con jQuery</a></li></ul><p></p> <script type="text/javascript">jQuery(document).ready(function($) { window.setTimeout('loadGoogle1_3477()',1000); });</script><script type="text/javascript">function loadGoogle1_3477(){ jQuery(document).ready(function($) { $('.dd-google1-3477').remove();$.getScript('https://apis.google.com/js/plusone.js'); }); }</script>]]></content:encoded> <wfw:commentRss>http://lineadecodigo.com/jquery/seleccionar-el-texto-de-un-campo/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Detectar texto seleccionado con jQuery</title><link>http://lineadecodigo.com/jquery/detectar-texto-seleccionado-con-jquery/</link> <comments>http://lineadecodigo.com/jquery/detectar-texto-seleccionado-con-jquery/#comments</comments> <pubDate>Tue, 20 Sep 2011 06:00:17 +0000</pubDate> <dc:creator>Víctor Cuervo</dc:creator> <category><![CDATA[jQuery]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[SELECT]]></category> <category><![CDATA[textarea]]></category> <category><![CDATA[texto]]></category> <guid
isPermaLink="false">http://lineadecodigo.com/?p=3469</guid> <description><![CDATA[Cuando ponemos campos de entrada en nuestras páginas web, el usuario podrá seleccionar parte del texto que haya en dichos campos. Vamos a ver como detectar el texto seleccionado utilizando jQuery. Lo primero será poner un textarea con un texto de ejemplo: &#160; &#60;form id=&#34;miform&#34;&#62; &#60;textarea id=&#34;texto&#34; rows=&#34;20&#34; cols=&#34;100&#34;&#62;Lorem ipsum dolor sit amet...&#60;/textarea&#62; &#60;/form&#62; &#160; [...]]]></description> <content:encoded><![CDATA[<div
class='dd_post_share'><div
class='dd_buttons'><div
class='dd_button'><div
class='dd-google1-ajax-load dd-google1-3469'></div><g:plusone size='medium' href='http://lineadecodigo.com/jquery/detectar-texto-seleccionado-con-jquery/'></g:plusone></div><div
class='dd_button'><script src="http://connect.facebook.net/es_ES/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Flineadecodigo.com%2Fjquery%2Fdetectar-texto-seleccionado-con-jquery%2F" send="true" show_faces="false"  layout="button_count" width="92"  ></fb:like></div><div
class='dd_button'><a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://lineadecodigo.com/jquery/detectar-texto-seleccionado-con-jquery/" data-count="horizontal" data-text="Detectar texto seleccionado con jQuery" data-via="lineadecodigo" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div></div><div
style='clear:both'></div></div><div
style='clear:both'></div><p>Cuando ponemos campos de entrada en nuestras páginas web, el usuario podrá seleccionar parte del texto que haya en dichos campos. Vamos a ver como detectar el texto seleccionado utilizando <a
href="http://www.manualweb.net/tutorial-jquery/" title="Tutorial sobre jQuery">jQuery</a>.</p><p>Lo primero será poner un <a
href="http://www.w3api.com/wiki/HTML:TEXTAREA" title="elemento HTML textarea">textarea</a> con un texto de ejemplo:</p><pre class="html4strict" style="font-family:monospace;">&nbsp;
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:FORM"><span style="color: #000000; font-weight: bold;">form</span></a> <a href="http://w3api.com/wiki/HTML:id"><span style="color: #000066;">id</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;miform&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:TEXTAREA"><span style="color: #000000; font-weight: bold;">textarea</span></a> <a href="http://w3api.com/wiki/HTML:id"><span style="color: #000066;">id</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;texto&quot;</span> <a href="http://w3api.com/wiki/HTML:rows"><span style="color: #000066;">rows</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;20&quot;</span> <a href="http://w3api.com/wiki/HTML:cols"><span style="color: #000066;">cols</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100&quot;</span>&gt;</span>Lorem ipsum dolor sit amet...<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:TEXTAREA"><span style="color: #000000; font-weight: bold;">textarea</span></a>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:FORM"><span style="color: #000000; font-weight: bold;">form</span></a>&gt;</span>
&nbsp;</pre><p>Para utilizar <a
href="http://www.manualweb.net/tutorial-jquery/" title="Tutorial sobre jQuery">jQuery</a> tendremos que cargar la librería de <a
href="http://www.manualweb.net/tutorial-jquery/" title="Tutorial sobre jQuery">jQuery</a>:</p><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:SCRIPT"><span style="color: #000000; font-weight: bold;">script</span></a> <a href="http://w3api.com/wiki/HTML:type"><span style="color: #000066;">type</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <a href="http://w3api.com/wiki/HTML:src"><span style="color: #000066;">src</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:SCRIPT"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span></pre><p>El método de <a
href="http://www.manualweb.net/tutorial-jquery/" title="Tutorial sobre jQuery">jQuery</a> que nos ayudará a detectar el texto seleccionado es <a
href="http://www.w3api.com/wiki/jQuery:Select()" title="método jQuery select()">.select(</a>). Este método habrá que asignarlo al elemento de la página sobre el que queremos detectar el texto seleccionado. En nuestro código el <a
href="http://www.w3api.com/wiki/HTML:TEXTAREA" title="elemento HTML textarea">textarea</a> 'texto'.</p><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#texto&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">select</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>	... <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>El contenido que ha sido seleccionado se puede recuperar mediante el método <a
href="http://www.w3api.com/wiki/DOM:Document.getSelection()" title="método getSelection() de Javascript">.getSelection()</a> de <a
href="http://www.manualweb.net/tutorial-javascript/" title="Manual de Javascript">Javascript</a>. Así que solo tendremos que ejecutar este método y volcarlo sobre la pantalla.</p><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#texto&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">select</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  selectedText <span style="color: #339933;">=</span> document.<span style="color: #660066;">getSelection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#resultado&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Se ha seleccionado el texto &quot;</span> <span style="color: #339933;">+</span> selectedText<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>Para volcarlo en pantalla utilizamos el método <a
href="http://www.w3api.com/wiki/jQuery:Html()" title="método jquery html">.html()</a> sobre una capa.<strong>Similar Posts:</strong><ul
class="similar-posts"><li><a
href="http://lineadecodigo.com/jquery/seleccionar-un-texto-solo-si-no-ha-cambiado/" rel="bookmark" title="Octubre 11, 2011">Seleccionar un texto solo si no ha cambiado</a></li><li><a
href="http://lineadecodigo.com/jquery/seleccionar-texto-al-posicionarse-en-un-campo/" rel="bookmark" title="Septiembre 23, 2011">Seleccionar texto al posicionarse en un campo</a></li><li><a
href="http://lineadecodigo.com/jquery/listar-opciones-seleccionadas-de-un-combo-con-jquery/" rel="bookmark" title="Septiembre 24, 2011">Listar opciones seleccionadas de un combo con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/seleccionar-el-texto-de-un-campo/" rel="bookmark" title="Septiembre 21, 2011">Seleccionar el texto de un campo</a></li><li><a
href="http://lineadecodigo.com/wforms/todos-los-campos-del-formulario-obligatorios/" rel="bookmark" title="Octubre 28, 2007">Todos los campos del formulario obligatorios</a></li></ul><p></p> <script type="text/javascript">jQuery(document).ready(function($) { window.setTimeout('loadGoogle1_3469()',1000); });</script><script type="text/javascript">function loadGoogle1_3469(){ jQuery(document).ready(function($) { $('.dd-google1-3469').remove();$.getScript('https://apis.google.com/js/plusone.js'); }); }</script>]]></content:encoded> <wfw:commentRss>http://lineadecodigo.com/jquery/detectar-texto-seleccionado-con-jquery/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Acceder a un campo de formulario con jQuery</title><link>http://lineadecodigo.com/jquery/acceder-a-un-campo-de-formulario-con-jquery/</link> <comments>http://lineadecodigo.com/jquery/acceder-a-un-campo-de-formulario-con-jquery/#comments</comments> <pubDate>Tue, 07 Jun 2011 06:00:05 +0000</pubDate> <dc:creator>Víctor Cuervo</dc:creator> <category><![CDATA[jQuery]]></category> <category><![CDATA[click]]></category> <category><![CDATA[formulario]]></category> <category><![CDATA[val]]></category> <category><![CDATA[valor]]></category> <guid
isPermaLink="false">http://lineadecodigo.com/?p=3340</guid> <description><![CDATA[jQuery nos ayuda de una forma muy sencilla a realizar operaciones con el DOM de la página. De esta forma nos facilita el acceder a un campo de formulario. Lo primero será montar nuestro formulario en HTML. Algo muy sencillo si conoces el uso de la etiqueta FORM: &#160; &#60;form id=&#34;formulario&#34;&#62; &#60;input id=&#34;valor&#34; type=&#34;text&#34; size=&#34;50&#34;&#62; [...]]]></description> <content:encoded><![CDATA[<div
class='dd_post_share'><div
class='dd_buttons'><div
class='dd_button'><div
class='dd-google1-ajax-load dd-google1-3340'></div><g:plusone size='medium' href='http://lineadecodigo.com/jquery/acceder-a-un-campo-de-formulario-con-jquery/'></g:plusone></div><div
class='dd_button'><script src="http://connect.facebook.net/es_ES/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Flineadecodigo.com%2Fjquery%2Facceder-a-un-campo-de-formulario-con-jquery%2F" send="true" show_faces="false"  layout="button_count" width="92"  ></fb:like></div><div
class='dd_button'><a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://lineadecodigo.com/jquery/acceder-a-un-campo-de-formulario-con-jquery/" data-count="horizontal" data-text="Acceder a un campo de formulario con jQuery" data-via="lineadecodigo" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div></div><div
style='clear:both'></div></div><div
style='clear:both'></div><p><a
href="http://www.manualweb.net/tutorial-jquery/" title="jQuery">jQuery</a> nos ayuda de una forma muy sencilla a realizar operaciones con el DOM de la página. De esta forma nos facilita el acceder a un campo de formulario.</p><p>Lo primero será montar nuestro formulario en <a
href="http://www.manualweb.net/tutorial-html/" title="manual html">HTML</a>. Algo muy sencillo si conoces el uso de la etiqueta <a
href="http://w3api.com/wiki/HTML:FORM" title="etiqueta form">FORM</a>:</p><pre class="html4strict" style="font-family:monospace;">&nbsp;
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:FORM"><span style="color: #000000; font-weight: bold;">form</span></a> <a href="http://w3api.com/wiki/HTML:id"><span style="color: #000066;">id</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;formulario&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:INPUT"><span style="color: #000000; font-weight: bold;">input</span></a> <a href="http://w3api.com/wiki/HTML:id"><span style="color: #000066;">id</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;valor&quot;</span> <a href="http://w3api.com/wiki/HTML:type"><span style="color: #000066;">type</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <a href="http://w3api.com/wiki/HTML:size"><span style="color: #000066;">size</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;50&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:BUTTON"><span style="color: #000000; font-weight: bold;">button</span></a> <a href="http://w3api.com/wiki/HTML:id"><span style="color: #000066;">id</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;comprobar&quot;</span>&gt;</span>Comprobar valor<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:BUTTON"><span style="color: #000000; font-weight: bold;">button</span></a>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:FORM"><span style="color: #000000; font-weight: bold;">form</span></a>&gt;</span>
&nbsp;</pre><p>El siguiente paso será montar nuestro código <a
href="http://www.manualweb.net/tutorial-jquery/" title="jQuery">jQuery</a>. Siguiendo los principios básicos veremos que el acceso a un ID del documento se consigue mediante la función:</p><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#idelemento&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>Para el caso de acceder a un campo de formulario es exactamente igual. Es decir, en este caso, si queremos recuperar el id "valor", escribiremos:</p><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#valor&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>Si bien, esto nos da acceso al elemento, no a su contenido. Para poder acceder al contenido del campo de formulario deberemos apoyarnos en la función <a
href="http://w3api.com/wiki/jQuery:Val()" title="funcion val en jQuery">.val()</a>.</p><p>Así, el código para acceder a un campo de formulario con <a
href="http://www.manualweb.net/tutorial-jquery/" title="jQuery">jQuery</a> nos quedará de la siguiente forma:</p><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comprobar&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#valor&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>Si revisamos el código, veremos que para acceder al campo lo hacemos desde la función <a
href="http://w3api.com/wiki/jQuery:Click()" title="funcion jquery click">click()</a>, la cual se invoca cada vez que hacemos click sobre el botón "comprobar".</p><p>Como podemos ver, es muy sencillo acceder a un campo de formulario con <a
href="http://www.manualweb.net/tutorial-jquery/" title="jQuery">jQuery</a>.<strong>Similar Posts:</strong><ul
class="similar-posts"><li><a
href="http://lineadecodigo.com/jquery/modificar-el-contenido-de-una-capa-con-jquery/" rel="bookmark" title="Mayo 27, 2010">Modificar el contenido de una capa con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/seleccionar-el-texto-de-un-campo/" rel="bookmark" title="Septiembre 21, 2011">Seleccionar el texto de un campo</a></li><li><a
href="http://lineadecodigo.com/jquery/repaginar-con-jquery-y-ajax/" rel="bookmark" title="Febrero 1, 2010">Repaginar con jQuery y AJAX</a></li><li><a
href="http://lineadecodigo.com/jquery/seleccionar-texto-al-posicionarse-en-un-campo/" rel="bookmark" title="Septiembre 23, 2011">Seleccionar texto al posicionarse en un campo</a></li><li><a
href="http://lineadecodigo.com/jquery/usando-cookies-con-jquery/" rel="bookmark" title="Diciembre 28, 2009">Usando cookies con jQuery</a></li></ul><p></p> <script type="text/javascript">jQuery(document).ready(function($) { window.setTimeout('loadGoogle1_3340()',1000); });</script><script type="text/javascript">function loadGoogle1_3340(){ jQuery(document).ready(function($) { $('.dd-google1-3340').remove();$.getScript('https://apis.google.com/js/plusone.js'); }); }</script>]]></content:encoded> <wfw:commentRss>http://lineadecodigo.com/jquery/acceder-a-un-campo-de-formulario-con-jquery/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Volcar el contenido de un array con jQuery</title><link>http://lineadecodigo.com/jquery/volcar-el-contenido-de-un-array-con-jquery/</link> <comments>http://lineadecodigo.com/jquery/volcar-el-contenido-de-un-array-con-jquery/#comments</comments> <pubDate>Tue, 31 May 2011 21:36:38 +0000</pubDate> <dc:creator>Víctor Cuervo</dc:creator> <category><![CDATA[jQuery]]></category> <category><![CDATA[array]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[join]]></category> <category><![CDATA[span]]></category> <category><![CDATA[text]]></category> <guid
isPermaLink="false">http://lineadecodigo.com/?p=3338</guid> <description><![CDATA[Si tenemos una serie de elementos en un array es muy probable que queramos volcar el contenido en pantalla. Vamos a ver cómo podemos hacer esta sencilla operación y volcar el contenido de un array con jQuery. El punto de partida... nuestro array: // Un listado de números. Soy poco original var a=&#91;1,2,3,4,5,6,7,8,9,10&#93;; Lo primero [...]]]></description> <content:encoded><![CDATA[<div
class='dd_post_share'><div
class='dd_buttons'><div
class='dd_button'><div
class='dd-google1-ajax-load dd-google1-3338'></div><g:plusone size='medium' href='http://lineadecodigo.com/jquery/volcar-el-contenido-de-un-array-con-jquery/'></g:plusone></div><div
class='dd_button'><script src="http://connect.facebook.net/es_ES/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Flineadecodigo.com%2Fjquery%2Fvolcar-el-contenido-de-un-array-con-jquery%2F" send="true" show_faces="false"  layout="button_count" width="92"  ></fb:like></div><div
class='dd_button'><a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://lineadecodigo.com/jquery/volcar-el-contenido-de-un-array-con-jquery/" data-count="horizontal" data-text="Volcar el contenido de un array con jQuery" data-via="lineadecodigo" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div></div><div
style='clear:both'></div></div><div
style='clear:both'></div><p>Si tenemos una serie de elementos en un array es muy probable que queramos volcar el contenido en pantalla. Vamos a ver cómo podemos hacer esta sencilla operación y volcar el contenido de un array con <a
href="http://www.manualweb.net/tutorial-jquery/" title="jQuery">jQuery</a>.</p><p>El punto de partida... nuestro array:</p><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Un listado de números. Soy poco original</span>
<span style="color: #003366; font-weight: bold;">var</span> a<span style="color: #339933;">=</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #339933;">,</span><span style="color: #CC0000;">7</span><span style="color: #339933;">,</span><span style="color: #CC0000;">8</span><span style="color: #339933;">,</span><span style="color: #CC0000;">9</span><span style="color: #339933;">,</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre><p>Lo primero que tenemos que tener es una parte de nuestra página web (de nuestro <a
href="http://w3api.com/wiki/Categor%C3%ADa:DOM" title="API del DOM">DOM</a>) preparado para volcar el contenido. En nuestro caso hemos definido un elemento <a
href="http://w3api.com/wiki/HTML:SPAN" title="span">span</a>.</p><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<a href="http://w3api.com/wiki/HTML:SPAN"><span style="color: #000000; font-weight: bold;">span</span></a> <a href="http://w3api.com/wiki/HTML:id"><span style="color: #000066;">id</span></a><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;resultado&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://w3api.com/wiki/HTML:SPAN"><span style="color: #000000; font-weight: bold;">span</span></a>&gt;</span></pre><p>A este elemento le hemos dado el id "resultado". Y es que el id es importante, ya que vía el id volcaremos el contenido del array. Para acceder al contenido del elemento "resultado" nos vamos a servir del método <a
href="http://w3api.com/wiki/jQuery:Text%28%29" title="método text jQuery">.text()</a>. En <a
href="http://www.manualweb.net/tutorial-jquery/" title="jQuery">jQuery</a> el método <a
href="http://w3api.com/wiki/jQuery:Text%28%29" title="método text jQuery">.text()</a> nos permite obtener el texto del elemento y todos los elementos que contenga.</p><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#resultado&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>Ahora solo nos queda volcar el contenido sobre el texto. Para ello nos vamos a valer de un método <a
href="http://www.manualweb.net/tutorial-javascript/" title="javascript">Javascript</a>, del método <a
href="http://w3api.com/wiki/Javascript:Array.join()" title="join">.join()</a>, el cual nos permite volcar el contenido de un array indicando como parámetro el separador de los elementos a volcar.</p><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#resultado&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>a.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;, &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><p>Y con esto ya hemos conseguido volcar el contenido de un array con <a
href="http://www.manualweb.net/tutorial-jquery/" title="jQuery">jQuery</a> sobre un elemento de nuestra página.<strong>Similar Posts:</strong><ul
class="similar-posts"><li><a
href="http://lineadecodigo.com/jquery/filtrar-elementos-nulos-de-un-array-con-jquery/" rel="bookmark" title="Mayo 16, 2011">Filtrar elementos nulos de un array con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/filtrar-elementos-de-un-array-con-jquery/" rel="bookmark" title="Abril 4, 2011">Filtrar elementos de un array con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/efecto-leer-mas-texto-con-jquery/" rel="bookmark" title="Enero 4, 2012">Efecto leer más texto con jQuery</a></li><li><a
href="http://lineadecodigo.com/jquery/listar-opciones-seleccionadas-de-un-combo-con-jquery/" rel="bookmark" title="Septiembre 24, 2011">Listar opciones seleccionadas de un combo con jQuery</a></li><li><a
href="http://lineadecodigo.com/javascript/convertir-un-array-en-una-cadena-con-javascript/" rel="bookmark" title="Agosto 16, 2007">Convertir un array en una cadena con JavaScript</a></li></ul><p></p> <script type="text/javascript">jQuery(document).ready(function($) { window.setTimeout('loadGoogle1_3338()',1000); });</script><script type="text/javascript">function loadGoogle1_3338(){ jQuery(document).ready(function($) { $('.dd-google1-3338').remove();$.getScript('https://apis.google.com/js/plusone.js'); }); }</script>]]></content:encoded> <wfw:commentRss>http://lineadecodigo.com/jquery/volcar-el-contenido-de-un-array-con-jquery/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> </channel> </rss>
