<?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>Random Snippets &#187; div id</title>
	<atom:link href="http://www.randomsnippets.com/tag/div-id/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.randomsnippets.com</link>
	<description>Random Snippets of Code and Ideas for any Web Developer</description>
	<lastBuildDate>Wed, 05 Aug 2009 14:32:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to dynamically add content to a div and store the content to a cookie via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/04/14/how-to-dynamically-add-content-to-a-div-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/04/14/how-to-dynamically-add-content-to-a-div-via-javascript/#comments</comments>
		<pubDate>Tue, 15 Apr 2008 06:45:17 +0000</pubDate>
		<dc:creator>Knix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[add]]></category>
		<category><![CDATA[content]]></category>
		<category><![CDATA[cookieContent]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[demo content]]></category>
		<category><![CDATA[div id]]></category>
		<category><![CDATA[document cookie]]></category>
		<category><![CDATA[document getelementbyid]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[dynamic content]]></category>
		<category><![CDATA[Dynamically]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[getCookie]]></category>
		<category><![CDATA[getElementById]]></category>
		<category><![CDATA[input text]]></category>
		<category><![CDATA[javascript code]]></category>
		<category><![CDATA[javascript function]]></category>
		<category><![CDATA[javascript functions]]></category>
		<category><![CDATA[onClick]]></category>
		<category><![CDATA[setCookie]]></category>
		<category><![CDATA[store content]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=14</guid>
		<description><![CDATA[
function addContent(divName, content) {
     document.getElementById(divName).innerHTML = content;
}
function setCookie(c_name,value,expiredays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function getCookie(c_name) {
    if (document.cookie.length>0) {
        c_start=document.cookie.indexOf(c_name + "=");
     [...]]]></description>
			<content:encoded><![CDATA[<p><script lang="javascript">
function addContent(divName, content) {
     document.getElementById(divName).innerHTML = content;
}
function setCookie(c_name,value,expiredays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function getCookie(c_name) {
    if (document.cookie.length>0) {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1) { 
            c_start=c_start + c_name.length+1; 
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            var cookieContent = "Welcome back " + unescape(document.cookie.substring(c_start,c_end));
            document.getElementById('myDiv2').innerHTML = cookieContent;
        } 
    }
}
</script><br />
This is an example of adding dynamic content via JavaScript by allowing the user type in the actual content.</p>
<fieldset>
<legend>Demo</legend>
<form name="myForm">
Content to be added:<br />
<textarea name="myContent">Type here&#8230;<br />
<h1>tags work as well</h1>
<p><u>woohoo</u></textarea></p>
<input type="button" value="Add content" onClick="addContent('myDiv', document.myForm.myContent.value)">
</form>
<p>Your content will be added dynamically below:</p>
<div id="myDiv"></div>
</fieldset>
<p><span id="more-14"></span><br />
Here is the plain HTML for the demo:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;form name=&quot;myForm&quot;&gt;
Content to be added:
&lt;textarea name=&quot;myContent&quot;&gt;Type here...&lt;h1&gt;tags work as well&lt;/h1&gt;&lt;u&gt;woohoo&lt;/u&gt;&lt;/textarea&gt;
&lt;input type=&quot;button&quot; value=&quot;Add content&quot; onClick=&quot;addContent('myDiv', document.myForm.myContent.value); setCookie('content', document.myForm.myContent.value, 7);&quot;&gt;
&lt;/form&gt;
&lt;br&gt;&lt;br&gt;
Your content will be added dynamically below:
&lt;div id=&quot;myDiv&quot;&gt;&lt;/div&gt;</pre></td></tr></table></div>

<p>There are 3 important parts to the HTML code:</p>
<ol>
<li><b>textarea</b> &#8211; This is the content that the user enters.  HTML tags are naturally supported.</li>
<li><b>Add content button</b> &#8211;  This buttons calls our <b>addContent()</b> function which passes the div id of where the content is to be entered and the actual content itself from the <b>textarea</b>.</li>
<li><b>myDiv</b> &#8211; This is the id of blank div where the content will be inserted.</li>
</ol>
<p>Here is the JavaScript code for the <b>addContent()</b> function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> addContent<span style="color: #009900;">&#40;</span>divName<span style="color: #339933;">,</span> content<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>divName<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> content;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>That&#8217;s it!  All it does it retrieve the DOM element, which in this case, is just an empty div and sets the <b>innerHTML</b> attribute to the content that is passed over to this function from the form.  </p>
<p>This demo is in response to a request that saves the content that the user has entered.</p>
<fieldset>
<legend>Demo</legend>
<form name="myForm2">
Enter your name below and click the button.  Your name will be saved to a cookie that expires in 7 days.  When you refresh the page or come back to visit you will see a heartwarming message welcoming you back.</p>
<input text name="myContent2" value="Enter your name here...">
<input type="button" value="Save my name" onClick="addContent('myDiv2', document.myForm2.myContent2.value); setCookie('content', document.myForm2.myContent2.value, 7);">
</form>
<p>Your name will be saved below:</p>
<div id="myDiv2" style="font-weight: bold;"></div>
</fieldset>
<p>Here is the HTML code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;form name=&quot;myForm2&quot;&gt;
Enter your name below and click the button.  Your name will be saved to a cookie that expires in 7 days.  When you refresh the page or come back to visit you will see a heartwarming message welcoming you back.
&lt;br&gt;&lt;br&gt;
&lt;input text name=&quot;myContent2&quot; value=&quot;Enter your name here...&quot;&gt;
&lt;input type=&quot;button&quot; value=&quot;Save my name&quot; onClick=&quot;addContent('myDiv2', document.myForm2.myContent2.value); setCookie('content', document.myForm2.myContent2.value, 7);&quot;&gt;
&lt;/form&gt;
&lt;br&gt;&lt;br&gt;
Your name will be saved below:
&lt;div id=&quot;myDiv2&quot; style=&quot;font-weight: bold;&quot;&gt;&lt;/div&gt;</pre></td></tr></table></div>

<p>The <b>onClick</b> event listener will launch 2 scripts this time:</p>
<ol>
<li><b>addContent</b> &#8211; This will do exactly what it did before in the first demo and just insert the input text value inside the div.</li>
<li><b>setCookie</b> &#8211; This will save a cookie with the name <b>content</b> and assign whatever is inside the input text as the value.  In addition, the cookie is set to expire in 7 days.</li>
</ol>
<p>To avoid reinventing the wheel, I have borrowed the JavaScript get and set cookie functions from <a href="http://www.w3schools.com/JS/js_cookies.asp" target="_blank">W3 Schools</a> and edited it slightly for the needs of this demo.</p>
<p>Here are the new JavaScript functions for cookie management:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> setCookie<span style="color: #009900;">&#40;</span>c_name<span style="color: #339933;">,</span>value<span style="color: #339933;">,</span>expiredays<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> exdate<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    exdate.<span style="color: #660066;">setDate</span><span style="color: #009900;">&#40;</span>exdate.<span style="color: #660066;">getDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>expiredays<span style="color: #009900;">&#41;</span>;
    document.<span style="color: #660066;">cookie</span><span style="color: #339933;">=</span>c_name<span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span> <span style="color: #339933;">+</span>escape<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>expiredays<span style="color: #339933;">==</span><span style="color: #003366; font-weight: bold;">null</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: #3366CC;">&quot;;expires=&quot;</span><span style="color: #339933;">+</span>exdate.<span style="color: #660066;">toGMTString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> getCookie<span style="color: #009900;">&#40;</span>c_name<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>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">length</span><span style="color: #339933;">&gt;</span>0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        c_start<span style="color: #339933;">=</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>c_name <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c_start<span style="color: #339933;">!=-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
            c_start<span style="color: #339933;">=</span>c_start <span style="color: #339933;">+</span> c_name.<span style="color: #660066;">length</span><span style="color: #339933;">+</span><span style="color: #CC0000;">1</span>; 
            c_end<span style="color: #339933;">=</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;;&quot;</span><span style="color: #339933;">,</span>c_start<span style="color: #009900;">&#41;</span>;
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c_end<span style="color: #339933;">==-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> c_end<span style="color: #339933;">=</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">length</span>;
            <span style="color: #003366; font-weight: bold;">var</span> cookieContent <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Welcome back &quot;</span> <span style="color: #339933;">+</span> unescape<span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>c_start<span style="color: #339933;">,</span>c_end<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
            document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myDiv2'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> cookieContent;
        <span style="color: #009900;">&#125;</span> 
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
getCookie<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'content'</span><span style="color: #009900;">&#41;</span>;</pre></td></tr></table></div>

<p>Please refer to <a href="http://www.w3schools.com/JS/js_cookies.asp" target="_blank">W3 Schools</a> for a detailed description of these JavaScript functions.  The only edit I have made is with the <b>getCookie</b> function in lines 13-14 where it retrieves the cookie value and inserts the content into a div.  Also, line 18 is very important as it calls the <b>getCookie</b> function and looks for the <b>content</b> cookie.  If the cookie exists, the content will be displayed in the <b>myDiv2</b> div.  </p>
<p>When using these JavaScripts, <b> make sure you load them after all your HTML is loaded</b>.  Otherwise, the function would have no place to insert the cookie content.</p>
<p>Although using cookies is a great way to store information, there are a couple of caveats:</p>
<ol>
<li><b>Size limitation</b> &#8211; This depends on the browser but keeping your cookies to a maximum size of 4000 bytes will keep you in the safe zone.</li>
<li><b>Cookie limit per domain</b> &#8211; This also depends on the browser but the general rule is 20 cookies per domain.</li>
</ol>
<p><script lang="javascript">getCookie('content');</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/04/14/how-to-dynamically-add-content-to-a-div-via-javascript/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Dynamically edit font styling of HTML content via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/02/24/dynamically-edit-font-styling-of-html-content-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/02/24/dynamically-edit-font-styling-of-html-content-via-javascript/#comments</comments>
		<pubDate>Mon, 25 Feb 2008 05:12:38 +0000</pubDate>
		<dc:creator>Knix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[color background]]></category>
		<category><![CDATA[div id]]></category>
		<category><![CDATA[document object model]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[Dynamically]]></category>
		<category><![CDATA[dynamicContent]]></category>
		<category><![CDATA[html javascript]]></category>
		<category><![CDATA[javascript onclick event]]></category>
		<category><![CDATA[size font]]></category>
		<category><![CDATA[styling]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/2008/02/24/dynamically-edit-font-styling-of-html-content-via-javascript/</guid>
		<description><![CDATA[The Document Object Model (DOM) allows for dynamic styling because it makes all HTML elements and attributes readily accessible using JavaScript.  Here is a simple demo of how powerful this technology can be:

Demo
Hello world!









Here is the JavaScript code that makes all this possible:


&#60;div id=&#34;dynamicContent&#34;&#62;Hello world!&#60;/div&#62;
&#60;input type=&#34;button&#34; onClick=&#34;document.getElementById('dynamicContent').style.fontWeight = 'bolder';&#34; value=&#34;Bold&#34;&#62;
&#60;input type=&#34;button&#34; onClick=&#34;document.getElementById('dynamicContent').style.fontWeight = 'lighter';&#34; [...]]]></description>
			<content:encoded><![CDATA[<p>The Document Object Model (DOM) allows for dynamic styling because it makes all HTML elements and attributes readily accessible using JavaScript.  Here is a simple demo of how powerful this technology can be:</p>
<fieldset>
<legend>Demo</legend>
<div id="dynamicContent">Hello world!</div>
<p></p>
<input type="button" onClick="document.getElementById('dynamicContent').style.fontWeight = 'bolder';" value="Bold">
<input type="button" onClick="document.getElementById('dynamicContent').style.fontWeight = 'lighter';" value="Lighter">
<input type="button" onClick="document.getElementById('dynamicContent').style.fontStyle = 'normal';" value="Normal">
<input type="button" onClick="document.getElementById('dynamicContent').style.fontStyle = 'italic';" value="Italic">
<input type="button" onClick="document.getElementById('dynamicContent').style.fontFamily = 'courier';" value="Courier">
<input type="button" onClick="document.getElementById('dynamicContent').style.fontFamily = 'roman';" value="Times Roman">
</fieldset>
<p><span id="more-7"></span><br />
Here is the JavaScript code that makes all this possible:<br />
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;dynamicContent&quot;</span><span style="color: #339933;">&gt;</span>Hello world<span style="color: #339933;">!&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;button&quot;</span> onClick<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;document.getElementById('dynamicContent').style.fontWeight = 'bolder';&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Bold&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;button&quot;</span> onClick<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;document.getElementById('dynamicContent').style.fontWeight = 'lighter';&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Lighter&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;button&quot;</span> onClick<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;document.getElementById('dynamicContent').style.fontStyle = 'normal';&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Normal&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;button&quot;</span> onClick<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;document.getElementById('dynamicContent').style.fontStyle = 'italic';&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Italic&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;button&quot;</span> onClick<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;document.getElementById('dynamicContent').style.fontFamily = 'courier';&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Courier&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;button&quot;</span> onClick<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;document.getElementById('dynamicContent').style.fontFamily = 'roman';&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Times Roman&quot;</span><span style="color: #339933;">&gt;</span></pre></div></div>

<p>
In this example, we use the JavaScript <b>onClick</b> event handler to dynamically change the content styling of the text within the div tags.  To access the div object, we use the <b>getElementById()</b> function and pass over the div id which is, <b>dynamicContent</b> in this case, as the argument.  Once we have access to the div element, we have access to all the styles that are associated with it. </p>
<p>There are a myriad of things that you can change such as font size, font weight, font face, font color, background color, borders and much much more.<br /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/02/24/dynamically-edit-font-styling-of-html-content-via-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
