<?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>¿Más código? &#187; xhtml</title>
	<atom:link href="http://www.mascodigo.com/tag/xhtml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mascodigo.com</link>
	<description>Mantenlo simple, estúpido</description>
	<lastBuildDate>Wed, 14 Sep 2011 14:40:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Borrar y rellenar el contenido de un div con DOM</title>
		<link>http://www.mascodigo.com/2009/08/06/borrar-y-rellenar-el-contenido-de-un-div-con-dom/</link>
		<comments>http://www.mascodigo.com/2009/08/06/borrar-y-rellenar-el-contenido-de-un-div-con-dom/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 16:53:27 +0000</pubDate>
		<dc:creator>Webmaster</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[div]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://www.mascodigo.com/?p=164</guid>
		<description><![CDATA[Cuando utilizamos javascript para modificar contenido en una página, tenemos varias opciones. Una de ellas es innerHTML. Esta opción seria maravillosa, porque permite añadir contenido a un div de forma muy simple, ademas permite añadir código html que sera interpretado como tal. El inconveniente de innerHTML es que no es una función estándar, es decir, [...]]]></description>
			<content:encoded><![CDATA[<p>Cuando utilizamos javascript para modificar contenido en una página, tenemos varias opciones. Una de ellas es innerHTML.</p>
<p>Esta opción seria maravillosa, porque permite añadir contenido a un div de forma muy simple, ademas permite añadir código html que sera interpretado como tal.</p>
<p>El inconveniente de innerHTML es que no es una función estándar, es decir, esta no se especifica en el estándar de javascript (<a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">ECMA-262</a>).</p>
<p>La otra opción es utilizar DOM, de esta forma respetamos el estándar. Por desgracia utilizar DOM es algo mas complicado que innerHTML.</p>
<p>Para solucionar el problema mencionado al principio, manejar contenido con javascript, he creado varias funciones que nos facilitan este trabajo.</p>
<p>Una de ellas se encarga de eliminar todo el contenido de un div y la otra de escribir un texto ( sin código HTML ).</p>
<p>Podéis ver un ejemplo real <a href="http://archivos.mascodigo.com/ejemplos/div/demoDiv.html">aquí</a>.</p>
<p>Funciones:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">        <span style="color: #006600; font-style: italic;">//Eliminar el contenido de un div</span>
        <span style="color: #003366; font-weight: bold;">function</span> limpiarDiv<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> div<span style="color: #339933;">;</span>
&nbsp;
                div <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>div.<span style="color: #660066;">hasChildNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    div.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span>div.<span style="color: #660066;">lastChild</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">//Escribir en un div</span>
        <span style="color: #003366; font-weight: bold;">function</span> rellenarDiv<span style="color: #009900;">&#40;</span>id<span style="color: #339933;">,</span> texto<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> div<span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> nodoTexto<span style="color: #339933;">;</span>
&nbsp;
              div <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
              nodoTexto <span style="color: #339933;">=</span> document.<span style="color: #660066;">createTextNode</span><span style="color: #009900;">&#40;</span>texto<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
              <span style="color: #006600; font-style: italic;">//Llamamos a la función limpiarDiv para eliminar el contenido</span>
              <span style="color: #006600; font-style: italic;">//actual del div.</span>
              limpiarDiv<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
              div.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>nodoTexto<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.mascodigo.com/2009/08/06/borrar-y-rellenar-el-contenido-de-un-div-con-dom/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Contar caracteres de un campo con JavaScript</title>
		<link>http://www.mascodigo.com/2009/03/25/contar-caracteres-de-un-campo-con-javascript/</link>
		<comments>http://www.mascodigo.com/2009/03/25/contar-caracteres-de-un-campo-con-javascript/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 18:46:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[caracteres]]></category>
		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://www.mascodigo.com/?p=64</guid>
		<description><![CDATA[Con el fin de hacer las cosas mas fáciles y entendibles al usuario he trabajado un poco la interactividad que tienen estos con los formularios. Al principio no le prestaba mucha atención a este tema. Simplemente creaba un campo con su correspondiente etiqueta descriptiva y ya esta. Ahora para ayudar al usuario a rellenar los [...]]]></description>
			<content:encoded><![CDATA[<p style="margin-bottom: 0cm;">Con el fin de hacer las cosas  mas fáciles y entendibles al usuario he trabajado un poco la interactividad que tienen estos con los formularios.</p>
<p></ br></p>
<p style="margin-bottom: 0cm;">Al principio no le prestaba mucha atención a este tema. Simplemente creaba un campo con su correspondiente etiqueta descriptiva y ya esta.</p>
<p></ br></p>
<p style="margin-bottom: 0cm;">Ahora para ayudar al usuario a rellenar los campos adecuadamente y evitar posibles errores, utilizo una función que indica cuando se paso el número máximo de caracteres, cuantos lleva escritos y añadir un máximo de caracteres permitidos.</p>
<p></ br></p>
<p style="margin-bottom: 0cm;">Esto también da un mayor atractivo al formulario.</p>
<p></ br></p>
<p><a href="http://www.mascodigo.com/ejemplos/contarCaracteresJavaScript/ejemplo.html" target="_blank"><br />
Ejemplo real<br />
</a></p>
<p style="margin-bottom: 0cm;">Ejemplo de uso:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;!--</span> incluir fichero con la funcion en JavaScript <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;./contarCaracteres.js&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/JavaScript&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;!--</span>
Campo al que contaremos los caracteres 
&nbsp;
<span style="color: #339933;">*</span> Llamamos a la función con el evento <span style="color: #3366CC;">&quot;onKeyDown&quot;</span>.
<span style="color: #339933;">*</span> Le pasamos como parametros<span style="color: #339933;">:</span>
	<span style="color: #339933;">-</span> el id del campo <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">-</span> el id del div donde se mostrara el nº de caracteres actuales
	<span style="color: #339933;">-</span> el color que tendra el campo en caso de sobrepasar el máximo de caracteres
	<span style="color: #339933;">-</span> el máximo de caracteres permitidos.
&nbsp;
<span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;ejemplo&quot;</span> onkeydown<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;contarCaracteres(this.id, 'nCaracteres','#FF0000',10)&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text&quot;</span> <span style="color: #339933;">/&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;!--</span> div donde mostraremos el numero de caracteres <span style="color: #339933;">--&gt;</span>
Caracteres<span style="color: #339933;">:&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #3366CC;">'nCaracteres'</span><span style="color: #339933;">&gt;&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Función:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> contarCaracteres<span style="color: #009900;">&#40;</span>campo<span style="color: #339933;">,</span> id<span style="color: #339933;">,</span>color<span style="color: #339933;">,</span>maximo<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">/**
	 * variable que contiene el div donde vamos a
	 * escribir y borrar.
	 */</span>
	<span style="color: #003366; font-weight: bold;">var</span> div<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">/**
	 * variable que guardara el numero total de
	 * caracteres que tiene el campo que estamos
	 * contando.
	 */</span>
	<span style="color: #003366; font-weight: bold;">var</span> cTotal<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">/**
	 * variable que creara el nodo de texto y el valor
	 * que mostraremos.
	 */</span>
	<span style="color: #003366; font-weight: bold;">var</span> tmp<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">//Asignar div al div con el que trabajaremos</span>
	div <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">//Borrar el contenido del div</span>
	<span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>div.<span style="color: #660066;">hasChildNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		div.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span>div.<span style="color: #660066;">lastChild</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">//Obtener el número de caracteres del campo en cTotal.</span>
	campo <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>campo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	cTotal <span style="color: #339933;">=</span> campo.<span style="color: #660066;">value</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">/**
	 * Añadirle el atributo maxlenght para evitar que se escriban
	 * mas caracteres de los indicados.
	 */</span>
	campo.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;maxlength&quot;</span><span style="color: #339933;">,</span>maximo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">/**
	 * Si se supera el máximo de caracteres permitidos ponemos el fondo
	 * del campo en rojo ó el color indicado.
	 */</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>cTotal <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> maximo  <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		campo.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> color<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>  <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		campo.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;#FFFFFF&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">/**
	 * Escribimos el total de caracteres en el div indicado.
	 * Para hacer esto creamos un nodo de texto con el valor de
	 * cTotal. El nodo lo asignamos a tmp
	 */</span>
	tmp <span style="color: #339933;">=</span> document.<span style="color: #660066;">createTextNode</span><span style="color: #009900;">&#40;</span>cTotal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">//Añadimos el contenido de tmp al div indicado.</span>
	div.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.mascodigo.com/2009/03/25/contar-caracteres-de-un-campo-con-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

