<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ jQuery - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ jQuery - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Wed, 13 May 2026 22:45:09 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/jquery/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ JavaScript document.ready() – Document Ready JS and jQuery Example ]]>
                </title>
                <description>
                    <![CDATA[ When working with JavaScript and the Document Object Model (DOM), you might want your script to run only when the DOM has loaded.  You can do this using the $(document).ready() method in jQuery, or the DOMContentLoaded event in vanilla JavaScript. In... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/javascript-document-ready-jquery-example/</link>
                <guid isPermaLink="false">66b0a3152cf7184612a5a9ac</guid>
                
                    <category>
                        <![CDATA[ DOM ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ jQuery ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Ihechikara Abba ]]>
                </dc:creator>
                <pubDate>Wed, 27 Jul 2022 21:03:19 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/07/joan-gamell-ZS67i1HLllo-unsplash.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>When working with JavaScript and the Document Object Model (DOM), you might want your script to run only when the DOM has loaded. </p>
<p>You can do this using the <code>$(document).ready()</code> method in jQuery, or the <code>DOMContentLoaded</code> event in vanilla JavaScript.</p>
<p>In this article, you'll learn how to make your JavaScript code run only when the DOM has loaded using jQuery and vanilla JavaScript.</p>
<h2 id="heading-how-to-use-the-documentready-method-in-jquery">How to Use the <code>$(document).ready()</code> Method in jQuery</h2>
<p>Before JavaScript runs in the browser, it waits for the contents of the document to load. This includes stylesheets, images, and so on.</p>
<p>As a convention, placing the script element just before the closing body tag makes the script wait for the rest of the document to load before running. </p>
<p>We also can make this process faster in jQuery by using the <code>$(document).ready()</code> method. The <code>$(document).ready()</code> method only waits for the DOM to load, it doesn't wait for stylesheets, images, and iframes. </p>
<p>Here's an example:</p>
<pre><code class="lang-javascript">$(<span class="hljs-built_in">document</span>).ready(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello World!"</span>);
});
</code></pre>
<p>In the code above, the <code>$(document).ready()</code> method will only run after the DOM has loaded. So you'll only see "Hello World!" in the console after the <code>$(document).ready()</code> method has started running. </p>
<p>In summary, you can write all your jQuery code inside the <code>$(document).ready()</code> method. This way, your code will wait for the DOM to be loaded before running. </p>
<h2 id="heading-how-to-use-the-domcontentloaded-event-in-javascript">How to Use the <code>DOMContentLoaded</code> Event in JavaScript</h2>
<p>Just like jQuery's <code>$(document).ready()</code> method, the <code>DOMContentLoaded</code> event fires once the DOM has loaded – it doesn't wait for stylesheets and images. </p>
<p>Here's how to use the <code>DOMContentLoaded</code> event:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">document</span>.addEventListener(<span class="hljs-string">"DOMContentLoaded"</span>, <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello World!"</span>);
});
</code></pre>
<p>Once the DOM has loaded, the <code>DOMContentLoaded</code> event will detect it and run.</p>
<p>You should use the <code>DOMContentLoaded</code>  event when:</p>
<ul>
<li>You have certain functionalities in your webpage that should fire immediately without waiting for the rest of the page content.</li>
<li>You have a script tag placed within the head element.</li>
</ul>
<h2 id="heading-summary">Summary</h2>
<p>In this article, we talked about the<code>$(document).ready()</code> method in jQuery, and the <code>DOMContentLoaded</code> event in vanilla JavaScript.</p>
<p>We use them to execute JavaScript code when the DOM has loaded. </p>
<p>The interesting part of these functionalities is that they let JavaScript code run without waiting for images and stylesheets to load completely in a web page. </p>
<p>Happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Switch from jQuery to Vanilla JavaScript with Bootstrap 5 ]]>
                </title>
                <description>
                    <![CDATA[ By Zoltán Szőgyényi Bootstrap 5 is a free and open-source CSS framework directed at responsive, mobile-first front-end web development. In case you didn't know, Bootstrap 5 alpha has been officially launched. It has removed jQuery as a dependency, ha... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/bootstrap-5-vanilla-js-tutorial/</link>
                <guid isPermaLink="false">66d461c438f2dc3808b79128</guid>
                
                    <category>
                        <![CDATA[ Bootstrap ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Bootstrap 5 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ jQuery ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Tutorial ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Fri, 31 Jul 2020 18:09:09 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2020/07/bootstrap-5-vanilla-js.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Zoltán Szőgyényi</p>
<p><a target="_blank" href="http://v5.getbootstrap.com/">Bootstrap 5</a> is a free and open-source CSS framework directed at responsive, mobile-first front-end web development.</p>
<p>In case you didn't know, <a target="_blank" href="https://themesberg.com/blog/bootstrap/bootstrap-version-5-alpha-whats-new">Bootstrap 5 alpha has been officially launched</a>. It has removed jQuery as a dependency, has dropped support for Internet Explorer 9 and 10, and brings some awesome updates for the Sass files, markup, and a new Utility API.</p>
<p>This tutorial will show you how to start using VanillaJS instead of jQuery when building websites using the newest version of Bootstrap 5.</p>
<h2 id="heading-getting-started">Getting started</h2>
<p>You will need to include Bootstrap 5 in your project. There are several ways to do this, but to keep it simple we will include the framework via CDN.</p>
<p>First of all, let's create a blank <code>index.html</code> page inside a project folder:</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Bootstrap 5 Vanilla JS tutorial by Themesberg<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>Include the <code>bootstrap.min.css</code> stylesheet before the end of your <code>&lt;head&gt;</code> tag:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/css/bootstrap.min.css"</span> <span class="hljs-attr">integrity</span>=<span class="hljs-string">"sha384-r4NyP46KrjDleawBgD5tp8Y7UzmLA05oM1iAEQ17CSuDqnUK2+k9luXQOfXJCJ4I"</span> <span class="hljs-attr">crossorigin</span>=<span class="hljs-string">"anonymous"</span>&gt;</span>
</code></pre>
<p>Afterwards include the Popper.js library and the main Bootstrap JavaScript file before the end of your <code>&lt;body&gt;</code> tag:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"</span> <span class="hljs-attr">integrity</span>=<span class="hljs-string">"sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"</span> <span class="hljs-attr">crossorigin</span>=<span class="hljs-string">"anonymous"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/js/bootstrap.min.js"</span> <span class="hljs-attr">integrity</span>=<span class="hljs-string">"sha384-oesi62hOLfzrys4LxRF63OJCXdXDipiYWBnvTl9Y9/TRlw5xlKIEHpNyvvDShgf/"</span> <span class="hljs-attr">crossorigin</span>=<span class="hljs-string">"anonymous"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p>Curious what the <code>integrity</code> and <code>crossorigin</code> attributes mean? Here's the explanation:</p>
<p><strong>Integrity attribute</strong>: allows the browser to check the file source to ensure that the code is never loaded if the source has been manipulated.</p>
<p><strong>Crossorigin attribute</strong>: is present when a request is loaded using 'CORS' which is now a requirement of SRI checking when not loaded from the 'same-origin'.</p>
<p>Great! Now we have successfully included the newest version of Bootstrap in our project. One of the obvious differences is that we no longer had to require jQuery as a dependency, <strong>saving about 82.54 KB</strong> in bandwidth if in a minified state.</p>
<h2 id="heading-switching-from-jquery-to-vanilla-js">Switching from jQuery to Vanilla JS</h2>
<p>Before we get started with this section, you should know that using Bootstrap 5 with jQuery is still possible according to the <a target="_blank" href="https://v5.getbootstrap.com/docs/5.0/getting-started/javascript/#still-want-to-use-jquery-its-possible">official documentation</a>.</p>
<p>We recommend using Vanilla JavaScript if the only reason you've been using jQuery was for the query selector, because you can do the same thing with the <code>document.querySelector('#element')</code> as if it was <code>$('#element')</code>.</p>
<p>The first step is to create a JavaScript file and include it before the end of the body tag but after the other two includes:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"</span> <span class="hljs-attr">integrity</span>=<span class="hljs-string">"sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"</span> <span class="hljs-attr">crossorigin</span>=<span class="hljs-string">"anonymous"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/js/bootstrap.min.js"</span> <span class="hljs-attr">integrity</span>=<span class="hljs-string">"sha384-oesi62hOLfzrys4LxRF63OJCXdXDipiYWBnvTl9Y9/TRlw5xlKIEHpNyvvDShgf/"</span> <span class="hljs-attr">crossorigin</span>=<span class="hljs-string">"anonymous"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"js/app.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p>So when do you need to actually use Javascript with Bootstrap 5? There are certain components in the framework that work only if they are initialized via Javascript, such as tooltips, popovers and toasts.</p>
<p>Furthermore, with components such as modals, dropdowns, and carousels you may want to be able to programmatically change them based on a user action or application logic.</p>
<h3 id="heading-initializing-tooltips-via-vanilla-javascript">Initializing tooltips via Vanilla JavaScript</h3>
<p>We all love tooltips, but they don't work unless they are initialized via JavaScript. Let's first start by creating a tooltip element inside our <code>index.html</code> file:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"tooltip"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"button"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn btn-secondary"</span> <span class="hljs-attr">data-toggle</span>=<span class="hljs-string">"tooltip"</span> <span class="hljs-attr">data-placement</span>=<span class="hljs-string">"top"</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"Tooltip on top"</span>&gt;</span>
    Tooltip on top
<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
</code></pre>
<p>Hovering over the button will not show the tooltip, because by default it is an opt-in feature of Bootstrap and it needs to be initialized manually using JavaScript. If you want to do it with jQuery, here's how it would look:</p>
<pre><code class="lang-js">$(<span class="hljs-string">'#tooltip'</span>).tooltip();
</code></pre>
<p>Using Vanilla JS you would need to use the following code to enable the tooltip:</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> tooltipElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'tooltip'</span>);
<span class="hljs-keyword">var</span> tooltip = <span class="hljs-keyword">new</span> bootstrap.Tooltip(tooltipElement);
</code></pre>
<p>What the code above does it that it selects the element with the unique id of "tooltip" and then creates a Bootstrap tooltip object. You can then use that to manipulate the state of the tooltip, such as showing or hiding the tooltip programmatically.</p>
<p>Here's an example on how you could show/hide it via methods:</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> showTooltip = <span class="hljs-literal">true</span>;
<span class="hljs-keyword">if</span> (showTooltip) {
    tooltip.show();
} <span class="hljs-keyword">else</span> {
    tooltip.hide();
}
</code></pre>
<p>If you would like to enable all of the tooltips you could also use the following code:</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> tooltipTriggerList = [].slice.call(<span class="hljs-built_in">document</span>.querySelectorAll(<span class="hljs-string">'[data-toggle="tooltip"]'</span>));
<span class="hljs-keyword">var</span> tooltipList = tooltipTriggerList.map(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">tooltipTriggerEl</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> bootstrap.Tooltip(tooltipTriggerEl)
});
</code></pre>
<p>What happens here is that we select all of the elements that have the <code>data-toggle="tooltip"</code> attribute and value and initialize a tooltip object for each one. It also saves them to a tooltipList variable.</p>
<h3 id="heading-toggle-the-visibility-of-your-elements-using-the-collapse-javascript-methods">Toggle the visibility of your elements using the Collapse JavaScript methods</h3>
<p>The collapse feature on Bootstrap is another very handy way to show and hide elements via data attributes or JavaScript.</p>
<p>Here's an example of how we can show or hide a card when a certain button is being clicked. Let's first create the HTML markup:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"toggleCardButton"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"button"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn btn-primary mb-2"</span>&gt;</span>Toggle Card<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"card"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card collapse show"</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"width: 18rem;"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://dev-to-uploads.s3.amazonaws.com/i/rphqzfoh2cbz3zj8m8t1.png"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card-img-top"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"..."</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card-body"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h5</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card-title"</span>&gt;</span>Freecodecamp.org<span class="hljs-tag">&lt;/<span class="hljs-name">h5</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card-text"</span>&gt;</span>Awesome resource to learn programming from.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn btn-primary"</span>&gt;</span>Learn coding for free<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>So we created a button with the id <code>toggleCardButton</code> and a card with the id <code>card</code>. Let's start by selecting the two elements:</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> toggleButton = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'toggleCardButton'</span>);
<span class="hljs-keyword">var</span> card = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'card'</span>);
</code></pre>
<p>Then we need to create a collapsable object using the newly selected card element:</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> collapsableCard = <span class="hljs-keyword">new</span> bootstrap.Collapse(card, {<span class="hljs-attr">toggle</span>: <span class="hljs-literal">false</span>});
</code></pre>
<p>What the <code>toggle:false</code> flag does is that it keeps the element visible after creating the object. If not present, it would hide the card by default.</p>
<p>Now we need to add an event listener for the button for the click action:</p>
<pre><code class="lang-js">toggleButton.addEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-comment">// do something when the button is being clicked</span>
});
</code></pre>
<p>And lastly we need to toggle the card using the collapsable object that we initialized like this:</p>
<pre><code class="lang-js">toggleButton.addEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    collapsableCard.toggle();
});
</code></pre>
<p>That's it! Now the card will be shown/hidden whenever the button is clicked. Of course all of this could've been done using the <a target="_blank" href="https://v5.getbootstrap.com/docs/5.0/components/collapse/#via-data-attributes">data attributes feature</a> from Bootstrap, but you may want to toggle certain elements based on an API call or a logic in your application.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>If you have followed along this tutorial you should now be able to use the most popular CSS framework without the need of requiring jQuery in your project. This lets you <strong>save up to 85 KB of data</strong> and makes your website faster! Congratulations ?</p>
<p>If you appreciate this tutorial and like using Bootstrap as a CSS framework for your projects, I invite you to check out some of the free and premium <a target="_blank" href="https://themesberg.com/templates/bootstrap">Bootstrap themes, templates, and UI Kits</a> that we build at Themesberg ❤️</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ A Simple Introduction to the jQuery Mobile Framework ]]>
                </title>
                <description>
                    <![CDATA[ By Alfrick Opidi When the world discovered the web, things were unexciting and lifeless. For example, building a simple image mouseover application required several lines of code, and couldn't work on some platforms.  But things got better when jQuer... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/introduction-to-jquery-mobile-framework/</link>
                <guid isPermaLink="false">66d45d9b680e33282da25e14</guid>
                
                    <category>
                        <![CDATA[ jQuery ]]>
                    </category>
                
                    <category>
                        <![CDATA[ mobile app development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Thu, 02 Jul 2020 16:49:43 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c99e8740569d1a4ca225a.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Alfrick Opidi</p>
<p>When the world discovered the web, things were unexciting and lifeless. For example, building a simple image mouseover application required several lines of code, and couldn't work on some platforms. </p>
<p>But things got better when jQuery was introduced, since it allowed developers to create stunning JavaScript applications that could run comfortably in various places.</p>
<p>After that, the jQuery team took things a notch higher by developing jQuery UI, which made it possible for developers to create nice-looking web applications on the existing jQuery core. </p>
<p>Better still, in 2010 jQuery Mobile was introduced which has made development much better and more efficient.</p>
<p>Built with a bias to mobile phones, <a target="_blank" href="http://jquerymobile.com/">jQuery Mobile</a> is an effective, unified framework that offers UI components, data transitions, and other exciting features. </p>
<p>jQuery Mobile leverages the functionalities of HTML5, CSS3, jQuery, as well as jQuery UI into a single framework that allows developers to achieve consistency across different platforms and devices.</p>
<h2 id="heading-basic-features-of-jquery-mobile">Basic Features of jQuery Mobile</h2>
<h3 id="heading-1-great-simplicity-and-usability">1. Great simplicity and usability</h3>
<p>The jQuery Mobile framework is uncomplicated and flexible. Since the framework's configuration interface is mark-up driven, developers can easily build their complete basic application interfaces in HTML, with minimal or no JavaScript code. </p>
<p>Complex tasks requiring several lines of JavaScript code, such as Ajax calls and DOM manipulation, can easily be realized with few <a target="_blank" href="https://www.freecodecamp.org/news/long-code-vs-short-code/">lines of code</a> in mobile jQuery.</p>
<p>For example, if we want a user to click and hide some text after a page has been created in the DOM, but before enhancement is complete, we can simply use the <strong>pagecreate</strong> event handler. This is something that would require several lines code to accomplish without jQuery Mobile. </p>
<pre><code class="lang-javascript">$(<span class="hljs-built_in">document</span>).on(<span class="hljs-string">"pagecreate"</span>,<span class="hljs-string">"#mypagetest"</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
  $(<span class="hljs-string">"span"</span>).on(<span class="hljs-string">"click"</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    $(<span class="hljs-built_in">this</span>).hide();
  });                       
});
</code></pre>
<p>In the above code, the <strong>#mypagetest</strong> parameter refers to the id of the page that specifies the page event. Also, the <strong>on()</strong> method is used to attach the event handlers.</p>
<p>Furthermore, its simplicity allows developers to break their applications into multiple pages. With the framework, developers can "write less, and do more."</p>
<h3 id="heading-2-progressive-enhancement-and-graceful-degradation">2. Progressive enhancement and graceful degradation</h3>
<p><a target="_blank" href="https://www.gov.uk/service-manual/technology/using-progressive-enhancement">Progressive enhancement</a> and graceful degradation are key features that propel the agility of jQuery Mobile. They enable it to support both high-end and less capable devices (for example, those lacking JavaScript support).</p>
<p>The framework allows developers to build applications that can be accessed by the widest number of browsers and devices, whether it is Internet Explorer 6 or the newest Android or iPhone.</p>
<p>Mobile jQuery also gives developers the ability to render basic content (as built) on basic devices. And the more sophisticated platforms and browsers will be increasingly enriched using additional, externally linked JavaScript and CSS.</p>
<h3 id="heading-3-support-for-user-friendly-inputs">3. Support for user-friendly inputs</h3>
<p>During jQuery mobile development, developers can include an uncomplicated <a target="_blank" href="https://blog.api.rakuten.net/what-is-an-api/">API</a> to support touch, mouse, and cursor focus-based user input functionalities. Several types of easily styled and touch-friendly form elements are also included in the framework.</p>
<p>Examples include checkbox and radio sets, slider, search filtering, and menu selection elements. Also, every one of the form elements includes an alternate 'mini' version, which can be easily incorporated into mobile web pages.</p>
<p>For example, here's how to create a checkbox button using jQuery Mobile. Notice that the <strong>data-mini="true"</strong> attribute is added to create a mini version of the button. </p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">form</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"checkbox"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"checkbox-mini-0"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"my-checkbox"</span> <span class="hljs-attr">data-mini</span>=<span class="hljs-string">"true"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">"checkbox-mini-0"</span>&gt;</span>Click here to agree<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
</code></pre>
<p>Beyond all this, to ensure the user experience is optimized on mobile devices, the framework has a rich Ajax-driven navigation system that allows animated page transitions to take place seamlessly.</p>
<p>With jQuery Mobile transition events, you can animate the transition from the current active page to the new page. </p>
<p>For example, you can use the <strong>pagebeforeshow</strong> event (triggered on the "to" page) and the <strong>pagebeforehide</strong> event (triggered on the "from" page) when transitioning from one page to the next. Both events are triggered before the transition animation begins.</p>
<p>Let's see how they can be applied: </p>
<pre><code class="lang-javascript">$(<span class="hljs-built_in">document</span>).on(<span class="hljs-string">"pagebeforeshow"</span>,<span class="hljs-string">"#myfirstpage"</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{ 

    <span class="hljs-comment">// When entering myfirstpage</span>

  alert(<span class="hljs-string">"myfirstpage is about to appear"</span>);

});

$(<span class="hljs-built_in">document</span>).on(<span class="hljs-string">"pagebeforehide"</span>,<span class="hljs-string">"#myfirstpage"</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{ 

    <span class="hljs-comment">// When leaving myfirstpage</span>

  alert(<span class="hljs-string">"myfirstpage is about to disappear"</span>);
});
</code></pre>
<h3 id="heading-4-accessibility">4. Accessibility</h3>
<p>Besides its cross-platform capabilities, jQuery for mobile was created with a strong consideration for accessibility. </p>
<p>The framework comes with support for Accessible Rich Internet Applications (WAI-ARIA) to assist disabled persons using screen readers and other assistive technologies easily access web pages.</p>
<h3 id="heading-5-lightweight-size">5. Lightweight size</h3>
<p>Mobile jQuery's lightweight size (about 40KB when minified) adds to its swiftness. Additionally, the fact that it employs minimal image dependencies also vastly accelerates its capabilities.</p>
<h3 id="heading-6-theming-and-ui-widgets">6. Theming and UI widgets</h3>
<p>jQuery Mobile has an in-built theme system that enables developers to determine their own application styling. With the jQuery Mobile Themeroller, developers can effectively customize their applications to fit their color, tastes, and preferences.</p>
<p>The framework also comes with various innovative, cross-platform widgets that enable developers to create applications that are better customized. </p>
<p>Some of the available widgets are persistent toolbars, buttons, dialogs, and the commonly used popup widget.</p>
<h3 id="heading-7-responsiveness">7. Responsiveness</h3>
<p>The framework's full responsiveness enables the same underlying codebases to fit comfortably in different types of screens, from mobile devices to desktop-sized screens.</p>
<h2 id="heading-basic-page-structure-of-jquery-mobile">Basic Page Structure of jQuery Mobile</h2>
<p>jQuery Mobile's structure has all the UI components and attributes required for creating user-friendly and feature-rich mobile web applications and websites of all kinds—whether basic or advanced.</p>
<p>You can use jQuery mobile to build web pages, various types of list views, toolbars, a wide range of form elements and buttons, dialogs, as well as other functionalities.</p>
<p>Importantly, since jQuery Mobile is created on top of jQuery core, it lets developers leverage jQuery UI code and access key facilities. These include robust animation and image effects for web pages, DOM manipulation, event handling, and Ajax for server communication.</p>
<p>Let's get a feel for how jQuery mobile development code looks.</p>
<p>For example, in this time of the COVID-19 pandemic when most people are working from home or from <a target="_blank" href="https://novelcoworking.com/locations/ohio/cincinnati/hooper-building/">co-working spaces</a>, let's create a simple web page that demonstrates some team management mistakes that people make.</p>
<p>Here is the code:</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>jQuery Mobile Example<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"http://code.jquery.com/jquery-1.11.1.min.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">data-role</span>=<span class="hljs-string">"page"</span> <span class="hljs-attr">date-theme</span>=<span class="hljs-string">"c"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">data-role</span>=<span class="hljs-string">"header"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>fCC jQuery Mobile Sample<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">data-role</span>=<span class="hljs-string">"content"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>COVID-19 Work-From-Home Team Management Mistakes To Avoid<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">data-role</span>=<span class="hljs-string">"listview"</span> <span class="hljs-attr">data-inset</span>=<span class="hljs-string">"true"</span> <span class="hljs-attr">data-filter</span>=<span class="hljs-string">"true"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span>Using Unnecessary Tools<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span>Foregoing Team Evaluations<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span>Micromanaging<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span>Hiring Too Quickly<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span>Not Having Contingencies<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">data-role</span>=<span class="hljs-string">"footer"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h4</span>&gt;</span>alfrickopidi.com, 2020 - Copyright<span class="hljs-tag">&lt;/<span class="hljs-name">h4</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>Here is the output when the above mobile jQuery lines of code are opened on a browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/06/image1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Notably, when the browser is decreased or increased, the size of the items in the list also adjusts appropriately. Therefore, the web page can be easily accessed in various devices with different screen resolutions without worrying about lack of consistency. The size of the items will change accordingly to suit the type of device.</p>
<p>As you can see in the above code sample, the document is a simple HTML5 that includes the following three things:</p>
<ul>
<li>Files from the jQuery Mobile CSS (jquery.mobile-1.4.5.min.css)</li>
<li>Files from the jQuery repository (jquery-1.11.1.min.js)</li>
<li>Files from the jQuery Mobile repository (jquery.mobile-1.4.5.min.js)</li>
</ul>
<p>These files are directly linked to the jQuery CDN. Another alternative is to head over to the <a target="_blank" href="http://jquerymobile.com/download/">download page</a> to get these files and host them on a private server.</p>
<p>Importantly, including the "viewport" metatag during jQuery mobile development instructs devices that the page width and the device screen width are equivalent (width=device-width).</p>
<p>The tag also instructs the browser to zoom in to 100 per cent (scale=1). If the scale is changed to 2, for instance, the browser will zoom the web page by 50 per cent.</p>
<p>A closer examination of the code reveals some strange "_data-"_attributes scattered throughout it. This is an improved feature of HTML5 that enables developers to pass organized data across an application – for example, the <em>data-role="header"</em> attribute defines the head section of the web page.</p>
<p>The above example just scratches the surface of the things developers can achieve using jQuery Mobile. The framework's <a target="_blank" href="https://demos.jquerymobile.com/1.4.5/">documentation</a> is easy to follow and describes its many features, including linking pages, incorporating animated page transitions, and designing buttons.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>jQuery for mobile is a resource-rich framework built with jQuery, HTML5, and CSS capabilities to handle certain cross-platform, cross-device and cross-browser compatibility issues effectively.</p>
<p>The framework offers great opportunities for creating mobile and web applications that are powerful, fully responsive, and future-ready.</p>
<p>Will you give it a try?</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Targeting Click of “Clear” Button (X) on Input Field ]]>
                </title>
                <description>
                    <![CDATA[ jQuery makes it easy to get your project up and running. Though it's fallen out of favor in recent years, it's still worth learning the basics, especially if you want quick access to its powerful methods. But while jQuery is a powerful library, it ca... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/targeting-click-of-clear-button-x-on-input-field/</link>
                <guid isPermaLink="false">66c36042ef766eb77cd787c7</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ jQuery ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 02 Jun 2020 02:19:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9a98740569d1a4ca2689.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>jQuery makes it easy to get your project up and running. Though it's fallen out of favor in recent years, it's still worth learning the basics, especially if you want quick access to its powerful methods.</p>
<p>But while jQuery is a powerful library, it can't do everything. That's where having solid understanding of vanilla JavaScript comes in handy.</p>
<p>Say you have a <a target="_blank" href="https://www.freecodecamp.org/learn/coding-interview-prep/take-home-projects/build-a-wikipedia-viewer">Wikipedia Viewer</a> project like this:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"search"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"text"</span>&gt;</span>Search on Wikipedia<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"searchbox"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"search"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">input</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"searchbutton"</span>&gt;</span>Search<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://en.wikipedia.org/wiki/Special:Random"</span> <span class="hljs-attr">target</span>=<span class="hljs-string">"_blank"</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"searchbutton"</span>&gt;</span>Random Article<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"resultingarticles"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<pre><code class="lang-js">$(<span class="hljs-string">"#searchbox"</span>).keyup(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">event</span>) </span>{
  <span class="hljs-keyword">if</span>(event.keyCode === <span class="hljs-number">13</span>) {
    $(<span class="hljs-string">"#searchbutton"</span>).click();
  };
});

$(<span class="hljs-string">"#searchbutton"</span>).click(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{

  <span class="hljs-keyword">var</span> searchInput = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"searchbox"</span>).value;
  searchInput = searchInput.toLowerCase();

  <span class="hljs-keyword">if</span>(searchInput !== <span class="hljs-string">""</span>) {

    <span class="hljs-keyword">var</span> myRequest = <span class="hljs-keyword">new</span> XMLHttpRequest();
    myRequest.open(<span class="hljs-string">'GET'</span>,<span class="hljs-string">'https://en.wikipedia.org/w/api.php?action=query&amp;list=search&amp;srsearch='</span>+ searchInput + <span class="hljs-string">'&amp;utf8=&amp;format=json&amp;origin=*'</span>);

      myRequest.onload = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
      <span class="hljs-keyword">var</span> searchResults = <span class="hljs-built_in">JSON</span>.parse(myRequest.responseText);

      $(<span class="hljs-string">".resultingarticles"</span>).empty();  

      <span class="hljs-keyword">for</span>(i=<span class="hljs-number">0</span>; i&lt;<span class="hljs-number">10</span>; i++) {
        <span class="hljs-keyword">var</span> articleTitle = searchResults.query.search[i].title;
        <span class="hljs-keyword">var</span> articleSnippet = searchResults.query.search[i].snippet;
        <span class="hljs-keyword">var</span> articleId = searchResults.query.search[i].pageid;
        <span class="hljs-keyword">var</span> articleLink = <span class="hljs-string">"https://en.wikipedia.org/?curid="</span> + articleId;
        $(<span class="hljs-string">".resultingarticles"</span>).append(<span class="hljs-string">"&lt;a href='"</span> + articleLink + <span class="hljs-string">"' target='_blank'&gt;"</span> + <span class="hljs-string">"&lt;div class='article'&gt;"</span> + <span class="hljs-string">"&lt;p&gt;"</span>+articleTitle+<span class="hljs-string">"&lt;/p&gt;"</span> + <span class="hljs-string">"&lt;p&gt;"</span> + articleSnippet + <span class="hljs-string">"&lt;/p&gt;"</span> + <span class="hljs-string">"&lt;/div&gt;"</span> + <span class="hljs-string">"&lt;/a&gt;"</span>);
      };

      };

    myRequest.send();

  };
});
</code></pre>
<p>Everything is working as you expect – you can enter text into the search box, hit enter or the "Search" button, and see a list of Wikipedia articles.</p>
<p>Because you're using <code>type="search"</code> on your <code>input</code> element, the Chrome browser will automatically add an "X" to the end of the input if there's text and you hover over the input. Note that other browsers might handle <code>type="search"</code> differently.</p>
<p>When you click on the "X", the text disappears.</p>
<p>But say you already have a list of articles, and when you clear the text, you also want to clear the populated articles:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/06/Peek-2020-06-13-19-24.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>It turns out that clicking the "X" in the search box fires a "search" event. jQuery doesn't support the "search" event, so you'll have to write an event listener in vanilla JavaScript:</p>
<pre><code class="lang-js"><span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"searchbox"</span>).addEventListener(<span class="hljs-string">"search"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">event</span>) </span>{
  $(<span class="hljs-string">".resultingarticles"</span>).empty();  
});
</code></pre>
<p>Now when a search event is fired, you can use jQuery to clear the <code>div</code> element with the articles:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/06/Peek-2020-06-13-19-29.gif" alt="Image" width="600" height="400" loading="lazy"></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Translate jQuery Code Into Vanilla JS ]]>
                </title>
                <description>
                    <![CDATA[ jQuery was once one of the most popular JS libraries available. It solved a lot of problems, like making DOM manipulation and Ajax calls standard across all the different browsers, which all handled JavaScript slightly differently. A lot of jQuery's ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-translate-jquery-code-into-vanilla-js/</link>
                <guid isPermaLink="false">66c35536c7095d76345eaf6a</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ jQuery ]]>
                    </category>
                
                    <category>
                        <![CDATA[ js ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 02 Jun 2020 02:15:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9aa7740569d1a4ca26e2.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>jQuery was once one of the most popular JS libraries available. It solved a lot of problems, like making DOM manipulation and Ajax calls standard across all the different browsers, which all handled JavaScript slightly differently.</p>
<p>A lot of jQuery's once cutting edge features have made it into vanilla JavaScript, so there's no need to load an entire library for just a few functionalities. Given this, it's not uncommon that one of your tasks on the job will be to rewrite jQuery into vanilla JavaScript.</p>
<h3 id="heading-how-to-rewrite-jquery-into-vanilla-js">How to rewrite jQuery into vanilla JS</h3>
<p>Imagine you have the following code:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"m1 menu"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"menu-center"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"active"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#home"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>

            <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#portfolio"</span>&gt;</span>Portfolio<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>

            <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#about"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>

            <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#contact"</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>

            <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"home"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"portfolio"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"about"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"contact"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span>, <span class="hljs-selector-tag">html</span> {
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
}
<span class="hljs-selector-class">.menu</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">75px</span>;
    <span class="hljs-attribute">background-color</span>: <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>);
    <span class="hljs-attribute">position</span>: fixed;
    <span class="hljs-attribute">background-color</span>:<span class="hljs-built_in">rgba</span>(<span class="hljs-number">4</span>, <span class="hljs-number">180</span>, <span class="hljs-number">49</span>, <span class="hljs-number">0.6</span>);
    <span class="hljs-attribute">-webkit-transition</span>: all <span class="hljs-number">0.3s</span> ease;
    <span class="hljs-attribute">-moz-transition</span>: all <span class="hljs-number">0.3s</span> ease;
    <span class="hljs-attribute">-o-transition</span>: all <span class="hljs-number">0.3s</span> ease;
    <span class="hljs-attribute">transition</span>: all <span class="hljs-number">0.3s</span> ease;
}
<span class="hljs-selector-class">.light-menu</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">75px</span>;
    <span class="hljs-attribute">background-color</span>: <span class="hljs-built_in">rgba</span>(<span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">1</span>);
    <span class="hljs-attribute">position</span>: fixed;
    <span class="hljs-attribute">background-color</span>:<span class="hljs-built_in">rgba</span>(<span class="hljs-number">4</span>, <span class="hljs-number">180</span>, <span class="hljs-number">49</span>, <span class="hljs-number">0.6</span>);
    <span class="hljs-attribute">-webkit-transition</span>: all <span class="hljs-number">0.3s</span> ease;
    <span class="hljs-attribute">-moz-transition</span>: all <span class="hljs-number">0.3s</span> ease;
    <span class="hljs-attribute">-o-transition</span>: all <span class="hljs-number">0.3s</span> ease;
    <span class="hljs-attribute">transition</span>: all <span class="hljs-number">0.3s</span> ease;
}
<span class="hljs-selector-id">#menu-center</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">980px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">75px</span>;
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> auto;
}
<span class="hljs-selector-id">#menu-center</span> <span class="hljs-selector-tag">ul</span> {
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">15px</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span>;
}
<span class="hljs-selector-id">#menu-center</span> <span class="hljs-selector-tag">ul</span> <span class="hljs-selector-tag">li</span> {
    <span class="hljs-attribute">list-style</span>: none;
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> <span class="hljs-number">30px</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span>;
    <span class="hljs-attribute">display</span>: inline;
}
<span class="hljs-selector-class">.active</span> {
    <span class="hljs-attribute">font-family</span>:<span class="hljs-string">'Droid Sans'</span>, serif;
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">14px</span>;
    <span class="hljs-attribute">color</span>: <span class="hljs-number">#fff</span>;
    <span class="hljs-attribute">text-decoration</span>: none;
    <span class="hljs-attribute">line-height</span>: <span class="hljs-number">50px</span>;
}
<span class="hljs-selector-tag">a</span> {
    <span class="hljs-attribute">font-family</span>:<span class="hljs-string">'Droid Sans'</span>, serif;
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">14px</span>;
    <span class="hljs-attribute">color</span>: black;
    <span class="hljs-attribute">text-decoration</span>: none;
    <span class="hljs-attribute">line-height</span>: <span class="hljs-number">50px</span>;
}
<span class="hljs-selector-id">#home</span> {
    <span class="hljs-attribute">background-color</span>: grey;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">overflow</span>: hidden;
    <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">url</span>(images/home-bg2.png);
}
<span class="hljs-selector-id">#portfolio</span> {
    <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">url</span>(images/portfolio-bg.png);
    <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
}
<span class="hljs-selector-id">#about</span> {
    <span class="hljs-attribute">background-color</span>: blue;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
}
<span class="hljs-selector-id">#contact</span> {
    <span class="hljs-attribute">background-color</span>: red;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
}
</code></pre>
<pre><code class="lang-js">$(<span class="hljs-built_in">document</span>).ready(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    $(<span class="hljs-built_in">document</span>).on(<span class="hljs-string">"scroll"</span>, onScroll);

    <span class="hljs-comment">//smoothscroll</span>
    $(<span class="hljs-string">'a[href^="#"]'</span>).on(<span class="hljs-string">'click'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">e</span>) </span>{
        e.preventDefault();
        $(<span class="hljs-built_in">document</span>).off(<span class="hljs-string">"scroll"</span>);

        $(<span class="hljs-string">'a'</span>).each(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            $(<span class="hljs-built_in">this</span>).removeClass(<span class="hljs-string">'active'</span>);
        })
        $(<span class="hljs-built_in">this</span>).addClass(<span class="hljs-string">'active'</span>);

        <span class="hljs-keyword">var</span> target = <span class="hljs-built_in">this</span>.hash,
            menu = target;
        $target = $(target);
        $(<span class="hljs-string">'html, body'</span>).stop().animate({
            <span class="hljs-string">'scrollTop'</span>: $target.offset().top+<span class="hljs-number">2</span>
        }, <span class="hljs-number">500</span>, <span class="hljs-string">'swing'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            <span class="hljs-built_in">window</span>.location.hash = target;
            $(<span class="hljs-built_in">document</span>).on(<span class="hljs-string">"scroll"</span>, onScroll);
        });
    });
});

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">onScroll</span>(<span class="hljs-params">event</span>)</span>{
    <span class="hljs-keyword">var</span> scrollPos = $(<span class="hljs-built_in">document</span>).scrollTop();
    $(<span class="hljs-string">'#menu-center a'</span>).each(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">var</span> currLink = $(<span class="hljs-built_in">this</span>);
        <span class="hljs-keyword">var</span> refElement = $(currLink.attr(<span class="hljs-string">"href"</span>));
        <span class="hljs-keyword">if</span> (refElement.position().top &lt;= scrollPos &amp;&amp; refElement.position().top + refElement.height() &gt; scrollPos) {
            $(<span class="hljs-string">'#menu-center ul li a'</span>).removeClass(<span class="hljs-string">"active"</span>);
            currLink.addClass(<span class="hljs-string">"active"</span>);
        }
        <span class="hljs-keyword">else</span>{
            currLink.removeClass(<span class="hljs-string">"active"</span>);
        }
    });
}
</code></pre>
<p>When you scroll down the page, the navbar should change colors as you get to different sections:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/05/Peek-2020-05-25-18-44.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>The function that handles this is <code>onScroll</code>:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">onScroll</span>(<span class="hljs-params">event</span>)</span>{
    <span class="hljs-keyword">var</span> scrollPos = $(<span class="hljs-built_in">document</span>).scrollTop();
    <span class="hljs-comment">/* console.log(scrollPos); */</span>
    $(<span class="hljs-string">'#menu-center a'</span>).each(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">var</span> currLink = $(<span class="hljs-built_in">this</span>);
        <span class="hljs-keyword">var</span> refElement = $(currLink.attr(<span class="hljs-string">"href"</span>));
        <span class="hljs-keyword">if</span> (refElement.position().top &lt;= scrollPos &amp;&amp; refElement.position().top + refElement.height() &gt; scrollPos) {
            $(<span class="hljs-string">'#menu-center ul li a'</span>).removeClass(<span class="hljs-string">"active"</span>);
            currLink.addClass(<span class="hljs-string">"active"</span>);
        }
        <span class="hljs-keyword">else</span>{
            currLink.removeClass(<span class="hljs-string">"active"</span>);
        }
    });
}
</code></pre>
<p>To translate <code>onScroll</code> into vanilla JS, you have a few options.</p>
<h3 id="heading-option-1-use-an-online-compiler">Option #1: Use an online compiler</h3>
<p>You can use an online tool like Google's <a target="_blank" href="https://closure-compiler.appspot.com/">Closure Compiler</a> to compress the code and strip out any unnecessary jQuery.</p>
<p>The problem is that you're still essentially left with jQuery code, so the browser would still need to load the library.</p>
<h3 id="heading-option-2-manually-translate-the-code">Option #2: Manually translate the code</h3>
<p>The best option is to check out resources like <a target="_blank" href="https://github.com/nefe/You-Dont-Need-jQuery">You Don't Need jQuery</a> and <a target="_blank" href="http://youmightnotneedjquery.com/">You Might Not Need jQuery</a> before rewriting your jQuery code. You'll be able to find the native JS versions of the most popular jQuery methods, some of which you can use in your own code.</p>
<p>Here's the <code>onScroll</code> function in vanilla JS:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">onScroll</span>(<span class="hljs-params">event</span>) </span>{
  <span class="hljs-keyword">var</span> sections = [...document.querySelectorAll(<span class="hljs-string">'#menu-center a'</span>)];
  <span class="hljs-keyword">var</span> scrollPos = <span class="hljs-built_in">window</span>.pageYOffset || <span class="hljs-built_in">document</span>.documentElement.scrollTop || <span class="hljs-built_in">document</span>.body.scrollTop;
  sections.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">currLink</span>) </span>{
    <span class="hljs-keyword">var</span> val = currLink.getAttribute(<span class="hljs-string">'href'</span>);
    <span class="hljs-keyword">var</span> refElement = <span class="hljs-built_in">document</span>.querySelector(val);
    <span class="hljs-keyword">if</span> (refElement.offsetTop &lt;= scrollPos &amp;&amp; (refElement.offsetTop + refElement.offsetHeight &gt; scrollPos)) {
      <span class="hljs-comment">//document.querySelector('#menu-center ul li a').classList.remove('active');</span>
      currLink.classList.add(<span class="hljs-string">'active'</span>);
    } <span class="hljs-keyword">else</span> {
      currLink.classList.remove(<span class="hljs-string">'active'</span>);
    }
  });
}

<span class="hljs-built_in">document</span>.addEventListener(<span class="hljs-string">'scroll'</span>, onScroll);
</code></pre>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build an Image Slider with jQuery ]]>
                </title>
                <description>
                    <![CDATA[ This tutorial will walk you through building an image slider using the jQuery library. This tutorial will have four parts: HTML SCSS JS References HTML We will be using Bootstrap for this tutorial to keep things looking good, without spending a lo... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-build-an-image-slider-with-jquery/</link>
                <guid isPermaLink="false">66c3502ebbe01f981047867a</guid>
                
                    <category>
                        <![CDATA[ HTML ]]>
                    </category>
                
                    <category>
                        <![CDATA[ image ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ jQuery ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sat, 08 Feb 2020 01:57:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9cae740569d1a4ca3395.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>This tutorial will walk you through building an image slider using the <a target="_blank" href="https://jquery.com/">jQuery</a> library.</p>
<p><img src="https://discourse-user-assets.s3.amazonaws.com/original/2X/0/08d83a22c28da836a06853b1f1ea669b398326b9.gif" alt="GIF showing Slider in action" width="600" height="400" loading="lazy"></p>
<p>This tutorial will have four parts:</p>
<ul>
<li><a target="_blank" href="https://guide.freecodecamp.org/miscellaneous/guide-to-build-a-sliding-image-gallery/#html">HTML</a></li>
<li><a target="_blank" href="https://guide.freecodecamp.org/miscellaneous/guide-to-build-a-sliding-image-gallery/#scss">SCSS</a></li>
<li><a target="_blank" href="https://guide.freecodecamp.org/miscellaneous/guide-to-build-a-sliding-image-gallery/#js">JS</a></li>
<li><a target="_blank" href="https://guide.freecodecamp.org/miscellaneous/guide-to-build-a-sliding-image-gallery/#references">References</a></li>
</ul>
<h2 id="heading-html"><strong>HTML</strong></h2>
<p>We will be using <a target="_blank" href="http://getbootstrap.com/">Bootstrap</a> for this tutorial to keep things looking good, without spending a lot of time.</p>
<p>Our structure will be as follows:</p>
<pre><code class="lang-text">&lt;div class="container"&gt;

  &lt;!-- The wrapper for our slider --&gt;
  &lt;div class="slider"&gt;
    &lt;ul class="slides"&gt;&lt;!-- Each image will be inside this unordered list --&gt;&lt;/ul&gt;
  &lt;/div&gt;

  &lt;div class="buttons"&gt;&lt;!-- Pause and play buttons will go in here --&gt;&lt;/div&gt;

&lt;/div&gt;
</code></pre>
<p>Inside our <code>ul</code> with the class of <code>slides</code> we will have the following:</p>
<pre><code class="lang-text">&lt;li class="slide"&gt;&lt;img src="#" /&gt;&lt;/li&gt;
&lt;li class="slide"&gt;&lt;img src="#" /&gt;&lt;/li&gt;
&lt;li class="slide"&gt;&lt;img src="#" /&gt;&lt;/li&gt;
&lt;li class="slide"&gt;&lt;img src="#" /&gt;&lt;/li&gt;
&lt;li class="slide"&gt;&lt;img src="#" /&gt;&lt;/li&gt;
</code></pre>
<p>Inside our <code>.buttons</code> class you should have the following:</p>
<pre><code class="lang-text">&lt;button type="button" class="btn btn-default pause"&gt;
    &lt;span class="glyphicon glyphicon-pause"&gt;&lt;/span&gt;
&lt;/button&gt;
&lt;button type="button" class="btn btn-default play"&gt;
    &lt;span class="glyphicon glyphicon-play"&gt;&lt;/span&gt;
&lt;/button&gt;
</code></pre>
<p>Here is an example of what your <code>html</code> should look like:</p>
<p>Note: You should replace the image <code>src</code> attribute with your own content.</p>
<pre><code class="lang-text">&lt;div class="container"&gt;

  &lt;div class="slider"&gt;
    &lt;ul class="slides"&gt;
      &lt;li class="slide"&gt;&lt;img src="https://unsplash.it/1280/720/?image=120" /&gt;&lt;/li&gt;
      &lt;li class="slide"&gt;&lt;img src="https://unsplash.it/1280/720/?image=70" /&gt;&lt;/li&gt;
      &lt;li class="slide"&gt;&lt;img src="https://unsplash.it/1280/720/?image=50" /&gt;&lt;/li&gt;
      &lt;li class="slide"&gt;&lt;img src="https://unsplash.it/1280/720/?image=170" /&gt;&lt;/li&gt;
      &lt;li class="slide"&gt;&lt;img src="https://unsplash.it/1280/720/?image=190" /&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;

  &lt;div class="buttons"&gt;
    &lt;button type="button" class="btn btn-default pause"&gt;
      &lt;span class="glyphicon glyphicon-pause"&gt;&lt;/span&gt;
    &lt;/button&gt;
    &lt;button type="button" class="btn btn-default play"&gt;
      &lt;span class="glyphicon glyphicon-play"&gt;&lt;/span&gt;
    &lt;/button&gt;
  &lt;/div&gt;

&lt;/div&gt;
</code></pre>
<h2 id="heading-scss"><strong>SCSS</strong></h2>
<p>We are using <a target="_blank" href="http://sass-lang.com/">Sass</a> and the SCSS syntax so we can nest and use variables</p>
<p><img src="https://forum.freecodecamp.com/images/emoji/emoji_one/heart_decoration.png?v=2" alt=":heart_decoration:" width="600" height="400" loading="lazy"></p>
<p>We can use the following SCSS to define our styling:</p>
<pre><code class="lang-text">// Variables
$width: 720px;

.slider {
  width: $width;
  height: 400px;
  overflow: hidden;
  margin: 0 auto;
  text-align: center;

  .slides {
    display: block;
    width: 6000px;
    height: 400px;
    margin: 0;
    padding: 0;
  }

  .slide {
    float: left;
    list-style-type: none;
    width: $width;
    height: 400px;

    img {
      width: 100%;
      height: 100%;
    }
  }
}

.buttons {
  margin: 0;
  width: $width;
  position: relative;
  top: -40px;
  margin: 0 auto;

  .play {
    display: none;
  }

  .btn {
    display: flex;
    margin: 0 auto;
    text-align: center;
  }
}
</code></pre>
<h2 id="heading-js"><strong>JS</strong></h2>
<h4 id="heading-variables"><strong>Variables</strong></h4>
<p><em>In the following code snippet, we define variables used later in our code.</em></p>
<pre><code class="lang-text">var animationSpeed = 1000; // How quickly the next slide animates.
var pause = 3000; // The pause between each slide.
</code></pre>
<p>We will use a blank variable where we will call the <code>setInterval</code> method:</p>
<pre><code class="lang-text">var interval;
</code></pre>
<h4 id="heading-animation-we-will-wrap-our-slider-animations-inside-a-function"><strong>Animation We will wrap our slider animations inside a function:</strong></h4>
<pre><code class="lang-text">function startSlider() {}
</code></pre>
<p>We are using the <code>setInterval()</code> native JavaScript method to automate the contents of the function on a time based trigger.</p>
<pre><code class="lang-text">interval = setInterval(function() {}, pause);
</code></pre>
<p>We use the <code>pause</code> variable to see how many milliseconds to wait before calling the function again. Read more on the native <code>setInterval</code> method here: <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval">https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval</a>. Inside our function we will use jQuery to fade between slides at the speed of the animationSpeed variable:</p>
<pre><code class="lang-text">$('.slides &gt; li:first')
  .fadeOut(animationSpeed)
  .next()
  .fadeIn(animationSpeed)
  .end()
  .appendTo('.slides');
</code></pre>
<p>We are targeting the first slide using <code>$('.slides &gt; li:first')</code>. - <code>.fadeOut(animationSpeed)</code> will fade the first slide out and then using <code>.next()</code>, we move to the next slide. - Once we have moved to the next slide, we will fade it in: <code>.fadeIn(animationSpeed)</code>. - This sequence will continue until the last slide (<code>.end()</code>), then we stop the animation. We will now call the <code>startSlider</code> function to start the animation:</p>
<p>startSlider();</p>
<h4 id="heading-play-and-pause-this-feature-is-optional-but-quite-easy-to-implement-we-will-hide-the-play-button-so-we-dont-see-both-the-play-and-pause-buttons"><strong>Play and Pause <em>This feature is optional, but quite easy to implement.</em> We will hide the play button, so we don’t see both the play and pause buttons:</strong></h4>
<pre><code class="lang-text">$('.play').hide(); // Hiding the play button.
</code></pre>
<p>We will now create our pause button (automatically shown on page load):</p>
<pre><code class="lang-text">$('.pause').click(function() {
    clearInterval(interval);
    $(this).hide();
    $('.play').show();
});
</code></pre>
<p>We will call our function every time the pause button is clicked using jQuery. - We will remove the interval using the <code>clearInterval</code> method and using our <code>interval</code> variable as the parameter, indicating which interval to stop. - Because our slider is paused, we will hide the pause button using <code>$(this).hide();</code>. Note: we are using <code>this</code>, which will refer to what our parent is calling i.e. <code>.pause</code>. - We will then show our play button so the user can resume the animation: <code>$('.play').show();</code>. The following code sets up our play button (automatically hidden on page load):</p>
<p>$(‘.play’).click(function() { startSlider(); $(this).hide(); $(‘.pause’).show(); });</p>
<p>We will call our function every time the play button is clicked using jQuery.</p>
<ul>
<li>We will start or restart the interval using the <code>startSlider</code> function.</li>
<li>Because our slider is currently playing, we will hide the play button using <code>$(this).hide();</code>. Note: we are using <code>this</code>, which will refer to what our parent is calling i.e. <code>.play</code>.</li>
<li>We will then show our pause button so the user can stop the animation at will: <code>$('.pause').show();</code>.</li>
</ul>
<h2 id="heading-references"><strong>References</strong></h2>
<ul>
<li>Checkout the source code and example on <a target="_blank" href="https://codepen.io/atjonathan/pen/BKMxxq">CodePen</a> for this tutorial.</li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ jQuery Selectors Explained: Class Selectors, ID Selectors, and More ]]>
                </title>
                <description>
                    <![CDATA[ jQuery Selectors jQuery uses CSS-style selectors to select parts, or elements, of an HTML page. It then lets you do something with the elements using jQuery methods, or functions. To use one of these selectors, type a dollar sign and parentheses afte... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/jquery-selectors/</link>
                <guid isPermaLink="false">66c35945c5e11f7a9c406847</guid>
                
                    <category>
                        <![CDATA[ jQuery ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 28 Jan 2020 01:35:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9d58740569d1a4ca3745.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <h2 id="heading-jquery-selectors"><strong>jQuery Selectors</strong></h2>
<p>jQuery uses CSS-style selectors to select parts, or elements, of an HTML page. It then lets you do something with the elements using jQuery methods, or functions.</p>
<p>To use one of these selectors, type a dollar sign and parentheses after it: <code>$()</code>. This is shorthand for the <code>jQuery()</code> function. Inside the parentheses, add the element you want to select. You can use either single- or double-quotes. After this, add a dot after the parentheses and the method you want to use.</p>
<p>In jQuery, the class and ID selectors are like those in CSS. Let's have a quick review of that before we go on.</p>
<h2 id="heading-id-selector-in-css"><strong>ID Selector in CSS</strong></h2>
<p>The CSS ID selector applies styles to a specific html element. The CSS ID selector must match the ID attribute of an HTML element. Unlike classes, which can be applied to multiple elements throughout a site, a specific ID may only be applied to one single element on a site. CSS ID will override CSS Class properties. To select an element with a specific id, write a hash (#) character, followed by the id of the element.</p>
<h3 id="heading-syntax"><strong>Syntax</strong></h3>
<pre><code class="lang-css"><span class="hljs-selector-id">#specified_id</span> { <span class="hljs-comment">/* styles */</span> }
</code></pre>
<p>You can combine the ID selector with other types of selectors to style a very specific element.</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">section</span><span class="hljs-selector-id">#about</span><span class="hljs-selector-pseudo">:hover</span> { <span class="hljs-attribute">color</span>: blue; }

<span class="hljs-selector-tag">div</span><span class="hljs-selector-class">.classname</span><span class="hljs-selector-id">#specified_id</span> { <span class="hljs-attribute">color</span>: green; }
</code></pre>
<h3 id="heading-note-about-ids"><strong>Note about IDs</strong></h3>
<p>ID should be avoided when styling if possible. As it has high specificity and it can be overriden only if you inline styles, or add styles into <code>&lt;style&gt;</code>. The weight of ID override class selectors and type selectors.</p>
<p>Remember, the ID selector must match an HTML element’s ID attribute.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"specified_id"</span>&gt;</span><span class="hljs-comment">&lt;!-- content --&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-specificity"><strong>Specificity</strong></h3>
<p>ID selectors have a high specificity making them difficult to override. Classes have a much lower specificity and are generally the preferred way to style elements in order to avoid specificity issues.</p>
<p>Here’s an example of a jQuery method that selects all paragraph elements, and adds a class of “selected” to them:</p>
<pre><code class="lang-javascript">&lt;p&gt;This is a paragraph selected by a jQuery method.&lt;/p&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is also a paragraph selected by a jQuery method.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>

$(<span class="hljs-string">"p"</span>).addClass(<span class="hljs-string">"selected"</span>);
</code></pre>
<h2 id="heading-ok-back-to-jquery">Ok, back to jQuery</h2>
<p>In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot (<code>.</code>) and the class name. If you want to select elements with a certain ID, use the hash symbol (<code>#</code>) and the ID name. Note that HTML is not case-sensitive, therefore it is best practice to keep HTML markup and CSS selectors lowercase.</p>
<p>Selecting by class:</p>
<pre><code class="lang-javascript">&lt;p <span class="hljs-class"><span class="hljs-keyword">class</span></span>=<span class="hljs-string">"p-with-class"</span>&gt;Paragraph <span class="hljs-keyword">with</span> a <span class="hljs-keyword">class</span>.&lt;/p&gt;

$(<span class="hljs-string">".p-with-class"</span>).css(<span class="hljs-string">"color"</span>, <span class="hljs-string">"blue"</span>); <span class="hljs-comment">// colors the text blue</span>
</code></pre>
<p>Selecting by ID:</p>
<pre><code class="lang-javascript">&lt;li id=<span class="hljs-string">"li-with-id"</span>&gt;List item <span class="hljs-keyword">with</span> an ID.&lt;/li&gt;

$(<span class="hljs-string">"#li-with-id"</span>).replaceWith(<span class="hljs-string">"&lt;p&gt;Socks&lt;/p&gt;"</span>);
</code></pre>
<p>You can also select certain elements along with their classes and IDs:</p>
<h3 id="heading-selecting-by-class"><strong>Selecting by class</strong></h3>
<p>If you want to select elements with a certain class, use a dot (.) and the class name.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"pWithClass"</span>&gt;</span>Paragraph with a class.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<pre><code class="lang-javascript">$(<span class="hljs-string">".pWithClass"</span>).css(<span class="hljs-string">"color"</span>, <span class="hljs-string">"blue"</span>); <span class="hljs-comment">// colors the text blue</span>
</code></pre>
<p>You can also use the class selector in combination with a tag name to be more specific.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"wishList"</span>&gt;</span>My Wish List<span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>`<span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>
</code></pre>
<pre><code class="lang-javascript">$(<span class="hljs-string">"ul.wishList"</span>).append(<span class="hljs-string">"&lt;li&gt;New blender&lt;/li&gt;"</span>);
</code></pre>
<h3 id="heading-selecting-by-id"><strong>Selecting by ID</strong></h3>
<p>If you want to select elements with a certain ID value, use the hash symbol (#) and the ID name.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"liWithID"</span>&gt;</span>List item with an ID.<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
</code></pre>
<pre><code class="lang-javascript">$(<span class="hljs-string">"#liWithID"</span>).replaceWith(<span class="hljs-string">"&lt;p&gt;Socks&lt;/p&gt;"</span>);
</code></pre>
<p>As with the class selector, this can also be used in combination with a tag name.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"headline"</span>&gt;</span>News Headline<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
</code></pre>
<pre><code class="lang-javascript">$(<span class="hljs-string">"h1#headline"</span>).css(<span class="hljs-string">"font-size"</span>, <span class="hljs-string">"2em"</span>);
</code></pre>
<h3 id="heading-selectors-that-act-as-filters"><strong>Selectors that act as filters</strong></h3>
<p>There are also selectors that act as filters - they will usually start with colons. For example, the <code>:first</code> selector selects the element that is the first child of its parent. Here’s an example of an unordered list with some list items. The jQuery selector below the list selects the first <code>&lt;li&gt;</code> element in the list—the “One” list item—and then uses the <code>.css</code> method to turn the text green.</p>
<pre><code class="lang-html">   <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>One<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Two<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Three<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
</code></pre>
<pre><code class="lang-javascript">$(<span class="hljs-string">"li:first"</span>).css(<span class="hljs-string">"color"</span>, <span class="hljs-string">"green"</span>);
</code></pre>
<p><strong>Note:</strong> Don’t forget that applying css in JavaScript is not a good practice. You should always give your styles in css files.</p>
<p>Another filtering selector, <code>:contains(text)</code>, selects elements that have a certain text. Place the text you want to match in the parentheses. Here’s an example with two paragraphs. The jQuery selector takes the word “Moto” and changes its color to yellow.</p>
<pre><code class="lang-html">    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Hello<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>World<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<pre><code class="lang-javascript">$(<span class="hljs-string">"p:contains('World')"</span>).css(<span class="hljs-string">"color"</span>, <span class="hljs-string">"yellow"</span>);
</code></pre>
<p>Similarly, the <code>:last</code> selector selects the element that is the last child of its parent. The JQuery selector below selects the last <code>&lt;li&gt;</code> element in the list—the “Three” list item—and then uses the <code>.css</code> method to turn the text yellow.</p>
<p><code>$("li:last").css("color", "yellow");</code></p>
<p><strong>Note:</strong> In the jQuery selector, <code>World</code> is in single-quotes because it is already inside a pair of double-quotes. Always use single-quotes inside double-quotes to avoid unintentionally ending a string.</p>
<p><strong>Multiple Selectors</strong> In jQuery, you can use multiple selectors to apply the same changes to more than one element, using a single line of code. You do this by separating the different ids with a comma. For example, if you want to set the background color of three elements with ids cat,dog,and rat respectively to red, simply do:</p>
<pre><code class="lang-text">$("#cat,#dog,#rat").css("background-color","red");
</code></pre>
<p>These are just a few of the selectors available for use in jQuery. See the More Information section for a link to the complete list on the jQuery website.</p>
<h4 id="heading-more-information"><strong>More Information:</strong></h4>
<ul>
<li><a target="_blank" href="http://api.jquery.com/category/selectors/">Full list of jQuery selectors</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ JQuery Ajax POST Method ]]>
                </title>
                <description>
                    <![CDATA[ Sends an asynchronous http POST request to load data from the server. Its general form is: jQuery.post( url [, data ] [, success ] [, dataType ] ) url : is the only mandatory parameter. This string contains the adress to which to send the request. ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/jquery-ajax-post-method/</link>
                <guid isPermaLink="false">66c35942cf1314a450f0d701</guid>
                
                    <category>
                        <![CDATA[ Ajax ]]>
                    </category>
                
                    <category>
                        <![CDATA[ jQuery ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 10 Dec 2019 23:43:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9ec8740569d1a4ca3f16.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Sends an asynchronous http POST request to load data from the server. Its general form is:</p>
<pre><code class="lang-javascript">jQuery.post( url [, data ] [, success ] [, dataType ] )
</code></pre>
<ul>
<li>url : is the only mandatory parameter. This string contains the adress to which to send the request. The returned data will be ignored if no other parameter is specified</li>
<li>data : A plain object or string that is sent to the server with the request.</li>
<li>success : A callback function that is executed if the request succeeds.it takes as an argument the returned data. It is also passed the text status of the response.</li>
<li>dataType : The type of data expected from the server. The default is Intelligent Guess (xml, json, script, text, html). if this parameter is provided, then the success callback must be provided as well.</li>
</ul>
<h4 id="heading-examples"><strong>Examples</strong></h4>
<pre><code class="lang-javascript">$.post(<span class="hljs-string">'http://example.com/form.php'</span>, {<span class="hljs-attr">category</span>:<span class="hljs-string">'client'</span>, <span class="hljs-attr">type</span>:<span class="hljs-string">'premium'</span>});
</code></pre>
<p>requests <code>form.php</code> from the server, sending additional data and ignoring the returned result</p>
<pre><code class="lang-javascript">$.post(<span class="hljs-string">'http://example.com/form.php'</span>, {<span class="hljs-attr">category</span>:<span class="hljs-string">'client'</span>, <span class="hljs-attr">type</span>:<span class="hljs-string">'premium'</span>}, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">response</span>)</span>{ 
      alert(<span class="hljs-string">"success"</span>);
      $(<span class="hljs-string">"#mypar"</span>).html(response.amount);
});
</code></pre>
<p>requests <code>form.php</code> from the server, sending additional data and handling the returned response (json format). This example can be written in this format:</p>
<pre><code class="lang-javascript">$.post(<span class="hljs-string">'http://example.com/form.php'</span>, {<span class="hljs-attr">category</span>:<span class="hljs-string">'client'</span>, <span class="hljs-attr">type</span>:<span class="hljs-string">'premium'</span>}).done(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">response</span>)</span>{
      alert(<span class="hljs-string">"success"</span>);
      $(<span class="hljs-string">"#mypar"</span>).html(response.amount);
});
</code></pre>
<p>The following example posts a form using Ajax and put results in a div</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!doctype <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"utf-8"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>jQuery.post demo<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://code.jquery.com/jquery-1.10.2.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">action</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"searchForm"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"s"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Search..."</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"Search"</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
<span class="hljs-comment">&lt;!-- the result of the search will be rendered inside this div --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"result"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
<span class="hljs-comment">// Attach a submit handler to the form</span>
$( <span class="hljs-string">"#searchForm"</span> ).submit(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"> event </span>) </span>{

  <span class="hljs-comment">// Stop form from submitting normally</span>
  event.preventDefault();

  <span class="hljs-comment">// Get some values from elements on the page:</span>
  <span class="hljs-keyword">var</span> $form = $( <span class="hljs-built_in">this</span> ),
    term = $form.find( <span class="hljs-string">"input[name='s']"</span> ).val(),
    url = $form.attr( <span class="hljs-string">"action"</span> );

  <span class="hljs-comment">// Send the data using post</span>
  <span class="hljs-keyword">var</span> posting = $.post( url, { <span class="hljs-attr">s</span>: term } );

  <span class="hljs-comment">// Put the results in a div</span>
  posting.done(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"> data </span>) </span>{
    <span class="hljs-keyword">var</span> content = $( data ).find( <span class="hljs-string">"#content"</span> );
    $( <span class="hljs-string">"#result"</span> ).empty().append( content );
  });
});
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>The following example use the github api to fetch the list of repositories of a user using jQuery.ajax() and put results in a div</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!doctype <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"utf-8"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>jQuery Get demo<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://code.jquery.com/jquery-1.10.2.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"userForm"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"username"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Enter gitHub User name"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"Search"</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
<span class="hljs-comment">&lt;!-- the result of the search will be rendered inside this div --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"result"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
<span class="hljs-comment">// Attach a submit handler to the form</span>
$( <span class="hljs-string">"#userForm"</span> ).submit(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"> event </span>) </span>{

  <span class="hljs-comment">// Stop form from submitting normally</span>
  event.preventDefault();

  <span class="hljs-comment">// Get some values from elements on the page:</span>
  <span class="hljs-keyword">var</span> $form = $( <span class="hljs-built_in">this</span> ),
    username = $form.find( <span class="hljs-string">"input[name='username']"</span> ).val(),
    url = <span class="hljs-string">"https://api.github.com/users/"</span>+username+<span class="hljs-string">"/repos"</span>;

  <span class="hljs-comment">// Send the data using post</span>
  <span class="hljs-keyword">var</span> posting = $.post( url, { <span class="hljs-attr">s</span>: term } );

  <span class="hljs-comment">//Ajax Function to send a get request</span>
  $.ajax({
    <span class="hljs-attr">type</span>: <span class="hljs-string">"GET"</span>,
    <span class="hljs-attr">url</span>: url,
    <span class="hljs-attr">dataType</span>:<span class="hljs-string">"jsonp"</span>
    <span class="hljs-attr">success</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">response</span>)</span>{
        <span class="hljs-comment">//if request if made successfully then the response represent the data</span>

        $( <span class="hljs-string">"#result"</span> ).empty().append( response );
    }
  });

});
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h3 id="heading-jqueryajax"><strong>jQuery.ajax()</strong></h3>
<p><code>$.post( url [, data ] [, success ] [, dataType ] )</code> is a shorthand Ajax function, equivalent to:</p>
<pre><code class="lang-javascript">$.ajax({
  <span class="hljs-attr">type</span>: <span class="hljs-string">"POST"</span>,
  <span class="hljs-attr">url</span>: url,
  <span class="hljs-attr">data</span>: data,
  <span class="hljs-attr">success</span>: success,
  <span class="hljs-attr">dataType</span>: dataType
});
</code></pre>
<p><code>$.ajax()</code> provides way more options that can be found <a target="_blank" href="http://api.jquery.com/jquery.ajax/">here</a></p>
<h4 id="heading-more-information"><strong>More Information:</strong></h4>
<p>For more information, please visit the <a target="_blank" href="https://api.jquery.com/jquery.post/">official website</a></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ The Best jQuery Examples ]]>
                </title>
                <description>
                    <![CDATA[ jQuery is the most widely-used JavaScript library, and is used in more than half of all major websites. It's motto is "write less, do more...!" jQuery makes web development easier to use by providing a number of 'helper' functions. These help develop... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/the-best-jquery-examples/</link>
                <guid isPermaLink="false">66c360d3c7095d76345eb031</guid>
                
                    <category>
                        <![CDATA[ jQuery ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sat, 23 Nov 2019 00:23:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9f1b740569d1a4ca40dc.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>jQuery is the most widely-used JavaScript library, and is used in more than half of all major websites. It's motto is "write less, do more...!"</p>
<p>jQuery makes web development easier to use by providing a number of 'helper' functions. These help developers to quickly write DOM (Document Object Model) interactions without needing to manually write as much JavaScript themselves.</p>
<p>jQuery adds a global variable with all of the libraries methods attached. The naming convention is to have this global variable as <code>$</code>. by typing in <code>$.</code> you have all the jQuery methods at your disposal.</p>
<h2 id="heading-getting-started">Getting Started</h2>
<p>There are two main ways to start using jQuery:</p>
<ul>
<li><strong>Include jQuery locally</strong>: Download the jQuery library from <a target="_blank" href="https://jquery.com/">jquery.com</a> and include it in your HTML code.</li>
<li><strong>Use a CDN</strong>: Link to the jQuery library using a CDN (Content Delivery Network).</li>
</ul>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"/jquery/jquery-3.4.1.min.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"js/scripts.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
</code></pre>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span> = <span class="hljs-string">"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"js/scripts.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
</code></pre>
<h2 id="heading-selectors">Selectors</h2>
<p>jQuery uses CSS-style selectors to select parts, or elements, of an HTML page. It then lets you do something with the elements using jQuery methods, or functions.</p>
<p>To use one of these selectors, type a dollar sign and parentheses after it: <code>$()</code>. This is shorthand for the <code>jQuery()</code> function. Inside the parentheses, add the element you want to select. You can use either single- or double-quotes. After this, add a dot after the parentheses and the method you want to use.</p>
<p>In jQuery, the class and ID selectors are like those in CSS.</p>
<p>Here's an example of a jQuery method that selects all paragraph elements, and adds a class of "selected" to them:</p>
<pre><code class="lang-javascript">&lt;p&gt;This is a paragraph selected by a jQuery method.&lt;/p&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is also a paragraph selected by a jQuery method.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>

$(<span class="hljs-string">"p"</span>).addClass(<span class="hljs-string">"selected"</span>);
</code></pre>
<p>In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot (<code>.</code>) and the class name. If you want to select elements with a certain ID, use the hash symbol (<code>#</code>) and the ID name. Note that HTML is not case-sensitive, therefore it is best practice to keep HTML markup and CSS selectors lowercase.</p>
<h3 id="heading-selecting-by-class">Selecting by class</h3>
<p>If you want to select elements with a certain class, use a dot (.) and the class name.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"pWithClass"</span>&gt;</span>Paragraph with a class.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<pre><code class="lang-javascript">$(<span class="hljs-string">".pWithClass"</span>).css(<span class="hljs-string">"color"</span>, <span class="hljs-string">"blue"</span>); <span class="hljs-comment">// colors the text blue</span>
</code></pre>
<p>You can also use the class selector in combination with a tag name to be more specific.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"wishList"</span>&gt;</span>My Wish List<span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>`<span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>
</code></pre>
<pre><code class="lang-javascript">$(<span class="hljs-string">"ul.wishList"</span>).append(<span class="hljs-string">"&lt;li&gt;New blender&lt;/li&gt;"</span>);
</code></pre>
<h3 id="heading-selecting-by-id">Selecting by ID</h3>
<p>If you want to select elements with a certain ID value, use the hash symbol (#) and the ID name.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"liWithID"</span>&gt;</span>List item with an ID.<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
</code></pre>
<pre><code class="lang-javascript">$(<span class="hljs-string">"#liWithID"</span>).replaceWith(<span class="hljs-string">"&lt;p&gt;Socks&lt;/p&gt;"</span>);
</code></pre>
<p>As with the class selector, this can also be used in combination with a tag name.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"headline"</span>&gt;</span>News Headline<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
</code></pre>
<pre><code class="lang-javascript">$(<span class="hljs-string">"h1#headline"</span>).css(<span class="hljs-string">"font-size"</span>, <span class="hljs-string">"2em"</span>);
</code></pre>
<h3 id="heading-selecting-by-attribute-value">Selecting by attribute value</h3>
<p>If you want to select elements with a certain attribute, use <code>([attributeName="value"])</code>.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"myInput"</span> /&gt;</span>
</code></pre>
<pre><code class="lang-javascript">$(<span class="hljs-string">"[name='myInput']"</span>).value(<span class="hljs-string">"Test"</span>); <span class="hljs-comment">// sets input value to "Test"</span>
</code></pre>
<p>You can also use the attribute selector in combination with a tag name to be more specific.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"myElement"</span> /&gt;</span>`<span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"myElement"</span>&gt;</span>Button<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
</code></pre>
<pre><code class="lang-javascript">$(<span class="hljs-string">"input[name='myElement']"</span>).remove(); <span class="hljs-comment">// removes the input field not the button</span>
</code></pre>
<h3 id="heading-selectors-that-act-as-filters">Selectors that act as filters</h3>
<p>There are also selectors that act as filters - they will usually start with colons. For example, the <code>:first</code> selector selects the element that is the first child of its parent. Here's an example of an unordered list with some list items. The jQuery selector below the list selects the first <code>&lt;li&gt;</code> element in the list--the "One" list item--and then uses the <code>.css</code> method to turn the text green.</p>
<pre><code class="lang-html">   <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>One<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Two<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Three<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
</code></pre>
<pre><code class="lang-javascript">$(<span class="hljs-string">"li:first"</span>).css(<span class="hljs-string">"color"</span>, <span class="hljs-string">"green"</span>);
</code></pre>
<h3 id="heading-attribute-selector">Attribute Selector</h3>
<p>There are selectors that return elements which matches certain combinations of Attributes like <em>Attribute contains</em>, <em>Attribute ends with</em>, <em>Attribute starts with</em> etc. Here's an example of an unordered list with some list items. The jQuery selector below the list selects the <code>&lt;li&gt;</code> element in the list--the "One" list item as it has <code>data*</code> attribute as <code>"India"</code> as its value--and then uses the <code>.css</code> method to turn the text green.</p>
<pre><code class="lang-html">   <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">data-country</span>=<span class="hljs-string">"India"</span>&gt;</span>Mumbai<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">data-country</span>=<span class="hljs-string">"China"</span>&gt;</span>Beijing<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">data-country</span>=<span class="hljs-string">"United States"</span>&gt;</span>New York<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
</code></pre>
<pre><code class="lang-javascript">$(<span class="hljs-string">"li[data-country='India']"</span>).css(<span class="hljs-string">"color"</span>, <span class="hljs-string">"green"</span>);
</code></pre>
<p>Another filtering selector, <code>:contains(text)</code>, selects elements that have a certain text. Place the text you want to match in the parentheses. Here's an example with two paragraphs. The jQuery selector takes the word "Moto" and changes its color to yellow.</p>
<pre><code class="lang-html">    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Hello<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>World<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<pre><code class="lang-javascript">$(<span class="hljs-string">"p:contains('World')"</span>).css(<span class="hljs-string">"color"</span>, <span class="hljs-string">"yellow"</span>);
</code></pre>
<p>Similarly, the <code>:last</code> selector selects the element that is the last child of its parent. The jQuery selector below selects the last <code>&lt;li&gt;</code> element in the list--the "Three" list item--and then uses the <code>.css</code> method to turn the text yellow.</p>
<p><code>$("li:last").css("color", "yellow");</code></p>
<p><strong>Note:</strong> In the jQuery selector, <code>World</code> is in single-quotes because it is already inside a pair of double-quotes. Always use single-quotes inside double-quotes to avoid unintentionally ending a string.</p>
<h3 id="heading-multiple-selectors">Multiple Selectors</h3>
<p>In jQuery, you can use multiple selectors to apply the same changes to more than one element, using a single line of code. You do this by separating the different ids with a comma. For example, if you want to set the background color of three elements with ids cat,dog,and rat respectively to red, simply do:</p>
<pre><code class="lang-text">$("#cat,#dog,#rat").css("background-color","red");
</code></pre>
<h2 id="heading-html-method">HTML Method</h2>
<p>The jQuery <code>.html()</code> method gets the content of a HTML element or sets the content of an HTML element.</p>
<h3 id="heading-getting">Getting</h3>
<p>To return the content of a HTML element, use this syntax:</p>
<pre><code class="lang-javascript">$(<span class="hljs-string">'selector'</span>).html();
</code></pre>
<p>For example:</p>
<pre><code class="lang-javascript">$(<span class="hljs-string">'#example'</span>).html();
</code></pre>
<h3 id="heading-setting">Setting</h3>
<p>To set the content of a HTML element, use this syntax:</p>
<pre><code class="lang-javascript">$(<span class="hljs-string">'selector'</span>).html(content);
</code></pre>
<p>For example:</p>
<pre><code class="lang-javascript">$(<span class="hljs-string">'p'</span>).html(<span class="hljs-string">'Hello World!'</span>);
</code></pre>
<p>That will set the content of all of the <code>&lt;p&gt;</code> elements to Hello World!</p>
<h3 id="heading-warning">Warning</h3>
<p><code>.html()</code> method is used to set the element's content in <strong>HTML</strong> format. This may be dangerous if the content is provided by user. Consider using <code>.text()</code> method instead if you need to set non-HTML strings as content.</p>
<h2 id="heading-css-method">CSS Method</h2>
<p>The jQuery <code>.css()</code> method gets the value of a computed style property for the first element in the set of matched elements or set one or more CSS properties for every matched element.</p>
<h3 id="heading-getting-1">Getting</h3>
<p>To return the value of a specified CSS property, use the following syntax:</p>
<pre><code class="lang-js">    $(selector).css(propertyName);
</code></pre>
<p>Example:</p>
<pre><code class="lang-js">    $(<span class="hljs-string">'#element'</span>).css(<span class="hljs-string">'background'</span>);
</code></pre>
<p>Note: Here we can use any css selector eg: element(HTML Tag selector), .element(Class Selector), #element(ID selector).</p>
<h3 id="heading-setting-1">Setting</h3>
<p>To set a specified CSS property, use the following syntax:</p>
<pre><code class="lang-js">    $(selector).css(propertyName,value);
</code></pre>
<p>Example:</p>
<pre><code class="lang-js">    $(<span class="hljs-string">'#element'</span>).css(<span class="hljs-string">'background'</span>,<span class="hljs-string">'red'</span>);
</code></pre>
<p>To set multiple CSS properties, you'll have to use the object literal syntax like this:</p>
<pre><code class="lang-js">    $(<span class="hljs-string">'#element'</span>).css({
        <span class="hljs-string">'background'</span>: <span class="hljs-string">'gray'</span>,
        <span class="hljs-string">'color'</span>: <span class="hljs-string">'white'</span>
    });
</code></pre>
<p>If you want to change a property labeled with more than one word, refer to this example:</p>
<p>To change <code>background-color</code> of an element</p>
<pre><code class="lang-js">    $(<span class="hljs-string">'#element'</span>).css(<span class="hljs-string">'background-color'</span>, <span class="hljs-string">'gray'</span>);
</code></pre>
<h2 id="heading-click-method">Click Method</h2>
<p>The jQuery Click method triggers a function when an element is clicked. The function is known as a "handler" because it handles the click event. Functions can impact the HTML element that is bound to the click using the jQuery Click method, or they can change something else entirely. The most-used form is:</p>
<pre><code class="lang-javascript">$(<span class="hljs-string">"#clickMe"</span>).click(handler)
</code></pre>
<p>The click method takes the handler function as an argument and executes it every time the element <code>#clickMe</code> is clicked. The handler function receives a parameter known as an <a target="_blank" href="http://api.jquery.com/Types/#Event">eventObject</a> that can be useful for controlling the action.</p>
<h3 id="heading-examples">Examples</h3>
<p>This code shows an alert when a user clicks a button:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"alert"</span>&gt;</span>Click Here<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
</code></pre>
<pre><code class="lang-javascript">$(<span class="hljs-string">"#alert"</span>).click(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  alert(<span class="hljs-string">"Hi! I'm an alert"</span>);
});
</code></pre>
<p>The <a target="_blank" href="http://api.jquery.com/Types/#Event">eventObject</a> has some built in methods, including <code>preventDefault()</code>, which does exactly what it says - stops the default event of an element. Here we pevent the anchor tag from acting as a link:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"myLink"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"www.google.com"</span>&gt;</span>Link to Google<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
</code></pre>
<pre><code class="lang-javascript">$(<span class="hljs-string">"#myLink"</span>).click(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">event</span>) </span>{
  event.preventDefault();
});
</code></pre>
<h3 id="heading-more-ways-to-play-with-the-click-method">More ways to play with the click method</h3>
<p>The handler function can also accept additional data in the form of an object:</p>
<pre><code class="lang-javascript">jqueryElement.click(usefulInfo, handler)
</code></pre>
<p>The data can be of any type.</p>
<pre><code class="lang-javascript">$(<span class="hljs-string">"element"</span>).click({<span class="hljs-attr">firstWord</span>: <span class="hljs-string">"Hello"</span>, <span class="hljs-attr">secondWord</span>: <span class="hljs-string">"World"</span>}, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">event</span>)</span>{
    alert(event.data.firstWord);
    alert(event.data.secondWord);
});
</code></pre>
<p>Invoking the click method without a handler function triggers a click event:</p>
<pre><code class="lang-javascript">$(<span class="hljs-string">"#alert"</span>).click(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  alert(<span class="hljs-string">"Hi! I'm an alert"</span>);
});

$(<span class="hljs-string">"#alert"</span>).click();
</code></pre>
<p>Now, whenever the page loads, the click event will be triggered when we enter or reload the page, and show the assigned alert.</p>
<p>Also you should prefer to use <code>.on("click",...)</code> over <code>.click(...)</code> because the former can use less memory and work for dynamically added elements.</p>
<h3 id="heading-common-mistakes">Common Mistakes</h3>
<p>The click event is only bound to elements currently in the DOM at the time of binding, so any elements added afterwards will not be bound. To bind all elements in the DOM, even if they will be created at a later time, use the <code>.on()</code> method.</p>
<p>For example, this click method example:</p>
<pre><code class="lang-javascript">$(<span class="hljs-string">"element"</span>).click(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  alert(<span class="hljs-string">"I've been clicked!"</span>);
});
</code></pre>
<p>Can be changed to this on method example:</p>
<pre><code class="lang-javascript">$(<span class="hljs-built_in">document</span>).on(<span class="hljs-string">"click"</span>, <span class="hljs-string">"element"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  alert(<span class="hljs-string">"I've been clicked!"</span>);
});
</code></pre>
<h3 id="heading-getting-the-element-from-a-click-event">Getting The Element From A Click event</h3>
<p>This applies to both jQuery and plain JavaScript, but if you set up your event trigger to target a class, you can grab the specific element that triggered the element by using the <code>this</code> keyword.</p>
<p>jQuery happens to make it very easy (and multi browser friendly) to traverse the DOM to find that element's parents, siblings, and children, as well.</p>
<p>Let's say I have a table full of buttons and I want to target the row that button is in, I can simply wrap <code>this</code> in a jQuery selector and then get its <code>parent</code> and its parent's <code>parent</code> like so:</p>
<pre><code class="lang-javascript">$( <span class="hljs-built_in">document</span> ).on(<span class="hljs-string">"click"</span>, <span class="hljs-string">".myCustomBtnClassInATable"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> myTableCell = $(<span class="hljs-built_in">this</span>).parent();
    <span class="hljs-keyword">var</span> myTableRow = myTableCell.parent();
    <span class="hljs-keyword">var</span> myTableBody = myTableRow.parent();
    <span class="hljs-keyword">var</span> myTable = myTableBody.parent();

    <span class="hljs-comment">//you can also chain these all together to get what you want in one line</span>
    <span class="hljs-keyword">var</span> myTableBody = $(<span class="hljs-built_in">this</span>).parent().parent().parent();
});
</code></pre>
<p>It is also interesting to check out the event data for the click event, which you can grab by passing in any variable name to the function in the click event. You'll most likely see an <code>e</code> or <code>event</code> in most cases:</p>
<pre><code class="lang-javascript">$( <span class="hljs-built_in">document</span> ).on(<span class="hljs-string">"click"</span>, <span class="hljs-string">".myCustomBtnClassInATable"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">e</span>) </span>{ 
    <span class="hljs-comment">//find out more information about the event variable in the console</span>
    <span class="hljs-built_in">console</span>.log(e);
});
</code></pre>
<h2 id="heading-mousedown-method">Mousedown Method</h2>
<p>The mousedown event occurs when the left mouse button is pressed. To trigger the mousedown event for the selected element, use this syntax: <code>$(selector).mousedown();</code></p>
<p>Most of the time, however, the mousedown method is used with a function attached to the mousedown event. Here's the syntax: <code>$(selector).mousedown(function);</code> For example:</p>
<pre><code class="lang-text">$(#example).mousedown(function(){
   alert("Example was clicked");
});
</code></pre>
<p>That code will make the page alert "Example was clicked" when #example is clicked.</p>
<h2 id="heading-hover-method">Hover Method</h2>
<p>The jquery hover method is a combination of the <code>mouseenter</code> and <code>mouseleave</code> events. The syntax is this:</p>
<pre><code class="lang-text">$(selector).hover(inFunction, outFunction);
</code></pre>
<p>The first function, inFunction, will run when the <code>mouseenter</code> event occurs. The second function is optional, but will run when the <code>mouseleave</code> event occurs. If only one function is specified, the other function will run for both the <code>mouseenter</code> and <code>mouseleave</code> events. Below is a more specific example.</p>
<pre><code class="lang-text">$("p").hover(function(){
    $(this).css("background-color", "yellow");
}, function(){
    $(this).css("background-color", "pink");
});
</code></pre>
<p>So this means that hover on paragraph will change its background color to yellow and the opposite will change back to pink.</p>
<h2 id="heading-animate-method">Animate Method</h2>
<p>jQuery's animate method makes it easy to create simple animations using only a few lines of code. The basic structure is as following:</p>
<pre><code class="lang-javascript">$(<span class="hljs-string">".selector"</span>).animate(properties, duration, callbackFunction());
</code></pre>
<p>For the <code>properties</code> argument, you need to pass a javascript object with the CSS properties you want to animate as keys and the values you want to animate to as values. For the <code>duration</code>, you need to input the amount of time in milliseconds the animation should take. The <code>callbackFunction()</code> is executed once the animation has finished.</p>
<h3 id="heading-example">Example</h3>
<p>A simple example would look like this:</p>
<pre><code class="lang-javascript">$(<span class="hljs-string">".awesome-animation"</span>).animate({
    <span class="hljs-attr">opacity</span>: <span class="hljs-number">1</span>,
    <span class="hljs-attr">bottom</span>: += <span class="hljs-number">15</span>
}, <span class="hljs-number">1000</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    $(<span class="hljs-string">".different-element"</span>).hide();
});
</code></pre>
<h2 id="heading-hide-method">Hide Method</h2>
<p>In its simplest form, <strong>.hide()</strong> hides the matched element immediately, with no animation. For example:</p>
<pre><code class="lang-javascript">$(<span class="hljs-string">".myclass"</span>).hide()
</code></pre>
<p>will hide all the elements whose class is <em>myclass</em>. Any jQuery selector can be used.</p>
<h3 id="heading-hide-as-an-animation-method">.hide() as an animation method</h3>
<p>Thanks to its options, <strong>.hide()</strong> can animate the width, height, and opacity of the matched elements simultaneously.</p>
<ul>
<li>Duration can be provided in milliseconds, or using the literals slow (600 ms) and fast(200ms). for example:</li>
<li>A function can be specified to be called once the animation is complete, once per every matched element. This callback is mainly useful for chaining together different animations. For example</li>
</ul>
<pre><code class="lang-javascript">$(<span class="hljs-string">"#myobject"</span>).hide(<span class="hljs-number">800</span>)
</code></pre>
<pre><code class="lang-javascript">$(<span class="hljs-string">"p"</span>).hide( <span class="hljs-string">"slow"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  $(<span class="hljs-string">".titles"</span>).hide(<span class="hljs-string">"fast"</span>);
  alert(<span class="hljs-string">"No more text!"</span>);
});
</code></pre>
<h2 id="heading-show-method">Show Method</h2>
<p>In its simplest form, <strong>.show()</strong> displays the matched element immediately, with no animation. For example:</p>
<pre><code class="lang-javascript">$(<span class="hljs-string">".myclass"</span>).show();
</code></pre>
<p>will show all the elements whose class is <em>myclass</em>. Any jQuery selector can be used.</p>
<p>However, this method does not override <code>!important</code> in the CSS style, such as <code>display: none !important</code>.</p>
<h3 id="heading-show-as-an-animation-method">.show() as an animation method</h3>
<p>Thanks to its options, <strong>.show()</strong> can animate the width, height, and opacity of the matched elements simultaneously.</p>
<ul>
<li>Duration can be provided in milliseconds, or using the literals slow (600 ms) and fast(200ms). for example:</li>
<li>A function can be specified to be called once the animation is complete, once per every matched element. for example</li>
</ul>
<pre><code class="lang-javascript">$(<span class="hljs-string">"#myobject"</span>).show(<span class="hljs-string">"slow"</span>);
</code></pre>
<pre><code class="lang-javascript">$(<span class="hljs-string">"#title"</span>).show( <span class="hljs-string">"slow"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  $(<span class="hljs-string">"p"</span>).show(<span class="hljs-string">"fast"</span>);
});
</code></pre>
<h2 id="heading-jquery-toggle-method">jQuery Toggle method</h2>
<p>To show / hide elements you can use <code>toggle()</code> method. If element is hidden <code>toggle()</code> will show it and vice versa. Usage:</p>
<pre><code class="lang-javascript">$(<span class="hljs-string">".myclass"</span>).toggle()
</code></pre>
<h2 id="heading-slide-down-method">Slide Down method</h2>
<p>This method animates the height of the matched elements. This causes lower parts of the page to slide down, making way for the revealed items. Usage:</p>
<pre><code class="lang-javascript">$(<span class="hljs-string">".myclass"</span>).slideDown(); <span class="hljs-comment">//will expand the element with the identifier myclass for 400 ms.</span>
$(<span class="hljs-string">".myclass"</span>).slideDown(<span class="hljs-number">1000</span>); <span class="hljs-comment">//will expand the element with the identifier myclass for 1000 ms.</span>
$(<span class="hljs-string">".myclass"</span>).slideDown(<span class="hljs-string">"slow"</span>); <span class="hljs-comment">//will expand the element with the identifier myclass for 600 ms.</span>
$(<span class="hljs-string">".myclass"</span>).slideDown(<span class="hljs-string">"fast"</span>); <span class="hljs-comment">//will expand the element with the identifier myclass for 200 ms.</span>
</code></pre>
<h2 id="heading-load-method">Load Method</h2>
<p>The load() method loads data from a server and puts the returned data into the selected element.</p>
<p><strong>Note:</strong> There is also a jQuery Event method called load. Which one is called, depends on the parameters.</p>
<h3 id="heading-example-1">Example</h3>
<pre><code class="lang-javascript">$(<span class="hljs-string">"button"</span>).click(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    $(<span class="hljs-string">"#div1"</span>).load(<span class="hljs-string">"demo_test.txt"</span>);
});
</code></pre>
<h2 id="heading-chaining">Chaining</h2>
<p>jQuery chaining allows you to execute multiple methods on the same jQuery selection, all on a single line.</p>
<p>Chaining allows us to turn multi-line statements:</p>
<pre><code class="lang-javascript">$(<span class="hljs-string">'#someElement'</span>).removeClass(<span class="hljs-string">'classA'</span>);
$(<span class="hljs-string">'#someElement'</span>).addClass(<span class="hljs-string">'classB'</span>);
</code></pre>
<p>Into a single statement:</p>
<pre><code class="lang-javascript">$(<span class="hljs-string">'#someElement'</span>).removeClass(<span class="hljs-string">'classA'</span>).addClass(<span class="hljs-string">'classB'</span>);
</code></pre>
<h2 id="heading-ajax-get-method">Ajax Get Method</h2>
<p>Sends an asynchronous http GET request to load data from the server. Its general form is:</p>
<pre><code class="lang-javascript">jQuery.get( url [, data ] [, success ] [, dataType ] )
</code></pre>
<ul>
<li><code>url</code>: The only mandatory parameter. This string contains the address to which to send the request. The returned data will be ignored if no other parameter is specified.</li>
<li><code>data</code>: A plain object or string sent to the server with the request.</li>
<li><code>success</code>: A callback function executed if the request succeeds. It takes as an argument the returned data. It is also passed the text status of the response.</li>
<li><code>dataType</code>: The type of data expected from the server. The default is Intelligent Guess (xml, json, script, text, html). If this parameter is provided, the success callback also must be provided.</li>
</ul>
<h3 id="heading-examples-1">Examples</h3>
<p>Request <code>resource.json</code> from the server, send additional data, and ignore the returned result:</p>
<pre><code class="lang-javascript">$.get(<span class="hljs-string">'http://example.com/resource.json'</span>, {<span class="hljs-attr">category</span>:<span class="hljs-string">'client'</span>, <span class="hljs-attr">type</span>:<span class="hljs-string">'premium'</span>});
</code></pre>
<p>Request <code>resource.json</code> from the server, send additional data, and handle the returned response (json format):</p>
<pre><code class="lang-javascript">$.get(<span class="hljs-string">'http://example.com/resource.json'</span>, {<span class="hljs-attr">category</span>:<span class="hljs-string">'client'</span>, <span class="hljs-attr">type</span>:<span class="hljs-string">'premium'</span>}, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">response</span>) </span>{
     alert(<span class="hljs-string">"success"</span>);
     $(<span class="hljs-string">"#mypar"</span>).html(response.amount);
});
</code></pre>
<p>However, <code>$.get</code> doesn't provide any way to handle error.</p>
<p>The above example (with error handling) can also be written as:</p>
<pre><code class="lang-javascript">$.get(<span class="hljs-string">'http://example.com/resource.json'</span>, {<span class="hljs-attr">category</span>:<span class="hljs-string">'client'</span>, <span class="hljs-attr">type</span>:<span class="hljs-string">'premium'</span>})
     .done(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">response</span>) </span>{
           alert(<span class="hljs-string">"success"</span>);
           $(<span class="hljs-string">"#mypar"</span>).html(response.amount);
     })
     .fail(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{
           alert(<span class="hljs-string">"error"</span>);
           $(<span class="hljs-string">"#mypar"</span>).html(error.statusText);
     });
</code></pre>
<h3 id="heading-ajax-get-equivalent">Ajax GET Equivalent</h3>
<p><code>$.get( url [, data ] [, success ] [, dataType ] )</code> is a shorthand Ajax function, equivalent to:</p>
<pre><code class="lang-javascript">$.ajax({
     <span class="hljs-attr">url</span>: url,
     <span class="hljs-attr">data</span>: data,
     <span class="hljs-attr">success</span>: success,
     <span class="hljs-attr">dataType</span>: dataType
});
</code></pre>
<h2 id="heading-ajax-post-method">Ajax Post Method</h2>
<p>Sends an asynchronous http POST request to load data from the server. Its general form is:</p>
<pre><code class="lang-javascript">jQuery.post( url [, data ] [, success ] [, dataType ] )
</code></pre>
<ul>
<li>url : This is the only mandatory parameter. This string contains the adress to which to send the request. The returned data will be ignored if no other parameter is specified</li>
<li>data : A plain object or string that is sent to the server with the request.</li>
<li>success : A callback function that is executed if the request succeeds. It takes as an argument the returned data. It is also passed the text status of the response.</li>
<li>dataType : The type of data expected from the server. The default is Intelligent Guess (xml, json, script, text, html). if this parameter is provided, then the success callback must be provided as well.</li>
</ul>
<h4 id="heading-examples-2">Examples</h4>
<pre><code class="lang-javascript">$.post(<span class="hljs-string">'http://example.com/form.php'</span>, {<span class="hljs-attr">category</span>:<span class="hljs-string">'client'</span>, <span class="hljs-attr">type</span>:<span class="hljs-string">'premium'</span>});
</code></pre>
<p>requests <code>form.php</code> from the server, sending additional data and ignoring the returned result</p>
<pre><code class="lang-javascript">$.post(<span class="hljs-string">'http://example.com/form.php'</span>, {<span class="hljs-attr">category</span>:<span class="hljs-string">'client'</span>, <span class="hljs-attr">type</span>:<span class="hljs-string">'premium'</span>}, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">response</span>)</span>{ 
      alert(<span class="hljs-string">"success"</span>);
      $(<span class="hljs-string">"#mypar"</span>).html(response.amount);
});
</code></pre>
<p>requests <code>form.php</code> from the server, sending additional data and handling the returned response (json format). This example can be written in this format:</p>
<pre><code class="lang-javascript">$.post(<span class="hljs-string">'http://example.com/form.php'</span>, {<span class="hljs-attr">category</span>:<span class="hljs-string">'client'</span>, <span class="hljs-attr">type</span>:<span class="hljs-string">'premium'</span>}).done(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">response</span>)</span>{
      alert(<span class="hljs-string">"success"</span>);
      $(<span class="hljs-string">"#mypar"</span>).html(response.amount);
});
</code></pre>
<p>The following example posts a form using Ajax and put results in a div</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!doctype <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"utf-8"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>jQuery.post demo<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://code.jquery.com/jquery-1.10.2.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">action</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"searchForm"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"s"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Search..."</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"Search"</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
<span class="hljs-comment">&lt;!-- the result of the search will be rendered inside this div --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"result"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
<span class="hljs-comment">// Attach a submit handler to the form</span>
$( <span class="hljs-string">"#searchForm"</span> ).submit(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"> event </span>) </span>{

  <span class="hljs-comment">// Stop form from submitting normally</span>
  event.preventDefault();

  <span class="hljs-comment">// Get some values from elements on the page:</span>
  <span class="hljs-keyword">var</span> $form = $( <span class="hljs-built_in">this</span> ),
    term = $form.find( <span class="hljs-string">"input[name='s']"</span> ).val(),
    url = $form.attr( <span class="hljs-string">"action"</span> );

  <span class="hljs-comment">// Send the data using post</span>
  <span class="hljs-keyword">var</span> posting = $.post( url, { <span class="hljs-attr">s</span>: term } );

  <span class="hljs-comment">// Put the results in a div</span>
  posting.done(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"> data </span>) </span>{
    <span class="hljs-keyword">var</span> content = $( data ).find( <span class="hljs-string">"#content"</span> );
    $( <span class="hljs-string">"#result"</span> ).empty().append( content );
  });
});
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>The following example use the github api to fetch the list of repositories of a user using jQuery.ajax() and put results in a div</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!doctype <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"utf-8"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>jQuery Get demo<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://code.jquery.com/jquery-1.10.2.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"userForm"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"username"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Enter gitHub User name"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"Search"</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
<span class="hljs-comment">&lt;!-- the result of the search will be rendered inside this div --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"result"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
<span class="hljs-comment">// Attach a submit handler to the form</span>
$( <span class="hljs-string">"#userForm"</span> ).submit(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"> event </span>) </span>{

  <span class="hljs-comment">// Stop form from submitting normally</span>
  event.preventDefault();

  <span class="hljs-comment">// Get some values from elements on the page:</span>
  <span class="hljs-keyword">var</span> $form = $( <span class="hljs-built_in">this</span> ),
    username = $form.find( <span class="hljs-string">"input[name='username']"</span> ).val(),
    url = <span class="hljs-string">"https://api.github.com/users/"</span>+username+<span class="hljs-string">"/repos"</span>;

  <span class="hljs-comment">// Send the data using post</span>
  <span class="hljs-keyword">var</span> posting = $.post( url, { <span class="hljs-attr">s</span>: term } );

  <span class="hljs-comment">//Ajax Function to send a get request</span>
  $.ajax({
    <span class="hljs-attr">type</span>: <span class="hljs-string">"GET"</span>,
    <span class="hljs-attr">url</span>: url,
    <span class="hljs-attr">dataType</span>:<span class="hljs-string">"jsonp"</span>
    <span class="hljs-attr">success</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">response</span>)</span>{
        <span class="hljs-comment">//if request if made successfully then the response represent the data</span>

        $( <span class="hljs-string">"#result"</span> ).empty().append( response );
    }
  });

});
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h3 id="heading-ajax-post-equivalent">Ajax POST Equivalent</h3>
<p><code>$.post( url [, data ] [, success ] [, dataType ] )</code> is a shorthand Ajax function, equivalent to:</p>
<pre><code class="lang-javascript">$.ajax({
  <span class="hljs-attr">type</span>: <span class="hljs-string">"POST"</span>,
  <span class="hljs-attr">url</span>: url,
  <span class="hljs-attr">data</span>: data,
  <span class="hljs-attr">success</span>: success,
  <span class="hljs-attr">dataType</span>: dataType
});
</code></pre>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ The Difference Between a Framework and a Library ]]>
                </title>
                <description>
                    <![CDATA[ Developers often use the terms “library” and “framework” interchangeably. But there is a difference. Both frameworks and libraries are code written by someone else that is used to help solve common pr ]]>
                </description>
                <link>https://www.freecodecamp.org/news/the-difference-between-a-framework-and-a-library-bd133054023f/</link>
                <guid isPermaLink="false">66d4617d57503cc72873dee2</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ jQuery ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Vue.js ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Brandon Wozniewicz ]]>
                </dc:creator>
                <pubDate>Fri, 01 Feb 2019 17:28:23 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*tO6yh-odg-YDLazUQ6FWVQ.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Developers often use the terms “library” and “framework” interchangeably. But there is a difference.</p>
<p>Both frameworks and libraries are code written by someone else that is used to help solve common problems.</p>
<p>For example, let’s say you have a program where you plan on working with strings. You decide to keep your code DRY (don’t repeat yourself) and write some reusable functions like these:</p>
<pre><code class="language-js">function getWords(str) {
   const words = str.split(' ');
   return words;
}
function createSentence(words) {
   const sentence = words.join(' ');
   return sentence;
}
</code></pre>
<p>Congratulations. You’ve created a library.</p>
<p>There isn’t anything magic about frameworks or library. Both libraries and frameworks are reusable code written by someone else. Their purpose is to help you solve common problems in easier ways.</p>
<p>I often use a house as a metaphor for web development concepts.</p>
<p>A library is like going to Ikea. You already have a home, but you need a bit of help with furniture. You don’t feel like making your own table from scratch. Ikea allows you to pick and choose different things to go in your home. You are in control.</p>
<p>A framework, on the other hand, is like building a model home. You have a set of blueprints and a few <em>limited</em> choices when it comes to architecture and design. Ultimately, the contractor and blueprint are in control. And they will let you know when and where you can provide your input.</p>
<h4 id="heading-the-technical-difference">The Technical Difference</h4>
<p>The technical difference between a framework and library lies in a term called inversion of control.</p>
<p>When you use a library, you are in charge of the flow of the application. You are choosing when and where to call the library. When you use a framework, the framework is in charge of the flow. It provides some places for you to plug in your code, but it calls the code you plugged in as needed.</p>
<p>Let’s look at an example using jQuery (a library) and Vue.js (a framework).</p>
<p>Imagine we want to display an error message when an error is present. In our example, we will click a button, and pretend an error occurs.</p>
<h4 id="heading-with-jquery">With jQuery:</h4>
<pre><code class="language-html">// index.html
&lt;html&gt;
   &lt;head&gt;
      &lt;script src="https://code.jquery.com/jquery-3.3.1.min.js"
      &lt;/script&gt;
      &lt;script src="./app.js"&gt;&lt;/script&gt;
   &lt;/head&gt;
   &lt;body&gt;
      &lt;div id="app"&gt;
         &lt;button id="myButton"&gt;Submit&lt;/button&gt;
       &lt;/div&gt;
   &lt;/body&gt;
&lt;/html&gt;
// app.js
// A bunch of our own code, 
// followed by calling the jQuery library
let error = false;
const errorMessage = 'An Error Occurred';
$('#myButton').on('click', () =&gt; {
  error = true; // pretend some error occurs and set error = true
  if (error) {
    $('#app')
       .append(`&lt;p id="error"&gt;${errorMessage}&lt;/p&gt;`);
  } else {
    $('#error').remove();
  }
});
</code></pre>
<p>Notice how we use jQuery. <em>We</em> tell our program where we want to call it. This is much like going to a physical library and pulling certain books off the shelf as we want them.</p>
<p>That’s not to say jQuery functions don’t require certain inputs <em>once</em> we call them, but jQuery itself is a library of those functions. We are in charge.</p>
<h4 id="heading-with-vuejs">With Vue.js</h4>
<pre><code class="language-html">//index.html
&lt;html&gt;
   &lt;head&gt;
      &lt;script src="https://cdn.jsdelivr.net/npm/vue"&gt;&lt;/script&gt;
      &lt;script src="./app.js"&gt;&lt;/script&gt;
   &lt;/head&gt;
   &lt;body&gt;
      &lt;div id="app"&gt;&lt;/div&gt;
   &lt;/body&gt;
&lt;/html&gt;
const vm = new Vue({
  template: `&lt;div id="vue-example"&gt;
               &lt;button @click="checkForErrors"&gt;Submit&lt;/button&gt;
               &lt;p v-if="error"&gt;{{ errorMessage }}&lt;/p&gt;
             &lt;/div&gt;`,
  el: '#vue-example',
  data: {
    error: null,
    errorMessage: 'An Error Occurred',
  },
  methods: {
    checkForErrors()  {
      this.error = !this.error;
    },
  },
});
</code></pre>
<p>With Vue, we have to fill in the blanks. The Vue constructor is an object with certain properties. It tells us what it needs, and then behind the scenes, Vue decides when it needs it. Vue inverts the control of the program. We plug our code into Vue. Vue is in charge.</p>
<p>The difference whether it is a library or framework is whether or not there is an inversion of control.</p>
<h4 id="heading-a-note-on-being-opinionated">A note on being “opinionated”</h4>
<p>You’ll often hear frameworks and libraries described as “opinionated” or “un-opinionated.” These terms are subjective. They attempt to define the level of freedom a developer has when structuring their code.</p>
<p>Frameworks are more opinionated than not since — by definition — the inversion of control requires a concession of application-design freedom.</p>
<p>Again, the degree to which something is opinionated is subjective. For example, I personally would consider Angular a highly opinionated framework, and Vue.js a less-opinionated framework.</p>
<h3 id="heading-in-summary">In summary</h3>
<ul>
<li><p>Frameworks and libraries are both code written by someone else that helps you perform some common tasks in a less verbose way.</p>
</li>
<li><p>A framework inverts the control of the program. It tells the developer what they need. A library doesn’t. The programmer calls the library where and when <em>they</em> need it.</p>
</li>
<li><p>The degree of freedom a library or framework gives the developer will dictate how “opinionated” it is.</p>
</li>
</ul>
<p>Thanks for reading!</p>
<p>Found this helpful? I write about practical automation, productivity systems, and building smarter workflows — without the jargon. Visit me at <a href="http://brandonwoz.com">brandonwoz.com</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ An Introduction to React (For People Who Know Just Enough jQuery To Get By) ]]>
                </title>
                <description>
                    <![CDATA[ By Julien Benchetrit Back in 2015, @chibicode’s “React.js Introduction For People Who Know Just Enough jQuery To Get By” was my first contact with React and the tutorial that demystified the whole thing for me. It walks you through the fundamentals o... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/react-introduction-for-people-who-know-just-enough-jquery-to-get-by-2019-version-28a4b4316d1a/</link>
                <guid isPermaLink="false">66c35d6af83dfae169b2c064</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ jQuery ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Fri, 18 Jan 2019 17:32:37 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*FQnrGf3EajRiFzTH528w7w.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Julien Benchetrit</p>
<p>Back in 2015, <a target="_blank" href="https://twitter.com/chibicode">@chibicode</a>’s “<a target="_blank" href="https://chibicode.com/react-js-introduction-for-people-who-know-just-enough-jquery-to-get-by/">React.js Introduction For People Who Know Just Enough jQuery To Get By</a>” was my first contact with React and the tutorial that demystified the whole thing for me.</p>
<p>It walks you through the fundamentals of React in a meticulous manner and is especially well-suited for anyone who comes from the world of jQuery.</p>
<p>Unfortunately, when trying to share it recently, I realized it was using React’s now obsolete <code>createClass</code> API and the images and embedded code samples weren’t loading anymore.</p>
<p>So with <a target="_blank" href="https://twitter.com/chibicode">@chibicode</a>’s permission, I rewrote his article with the latest versions of React and JavaScript in mind and expanded upon some of the explanations.</p>
<p>Please note though that the vast majority of this tutorial is his work. I hope it will prove as useful to you as it did to me.</p>
<p>Without further ado, let’s learn us some React!</p>
<blockquote>
<p><strong>Small disclaimer:</strong> Some of the images are from <a target="_blank" href="https://twitter.com/chibicode">@chibicode</a>’s original article and the code they show is slightly different from the code we use here. The images are for illustrative purposes only. Always refer to the written code samples.</p>
</blockquote>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*FQnrGf3EajRiFzTH528w7w.png" alt="Image" width="710" height="324" loading="lazy"></p>
<h4 id="heading-target-audience-people-who-know-just-enough-jquery-to-get-by">Target Audience: People Who Know Just Enough jQuery to Get by</h4>
<p>Before I begin, I’d like to clarify who the target audience is.</p>
<p>Zed Shaw, the author of “<a target="_blank" href="https://learncodethehardway.org/">Learn Code the Hard Way</a>” series, wrote an excellent blog post called <a target="_blank" href="https://zedshaw.com/2015/06/16/early-vs-beginning-coders/">Early vs. Beginning Coders</a>. In his post, Zed criticizes programming educators who claim that their materials are for “beginners”, but in reality are incomprehensible for most “total” beginners.</p>
<p>I don’t want to make a similar mistake here. Of the people who have never tried out React, some are comfortable with frontend JS frameworks like <a target="_blank" href="http://backbonejs.org/">Backbone</a>, <a target="_blank" href="https://emberjs.com/">Ember</a> or <a target="_blank" href="https://angular.io/">Angular</a>. Some know JavaScript pretty well. Some know just enough jQuery to get by. A tutorial that’s effective for one group may not be optimal for the other groups.</p>
<p>In this tutorial, I’m targeting the third group I mentioned: <strong>people who know just enough jQuery to get by</strong>. Examples of people who might fit in this category would be:</p>
<ul>
<li><p>Designers who can do basic coding in HTML/CSS/jQuery.</p>
</li>
<li><p>WordPress developers who know how to use jQuery plugins.</p>
</li>
<li><p>Beginning developers who have completed basic HTML/CSS/JS tutorials online.</p>
</li>
<li><p>Backend developers who rely on Bootstrap and basic jQuery for their frontend needs.</p>
</li>
<li><p>Anyone who does more copy-pasting than architecting when it comes to JavaScript.</p>
</li>
</ul>
<p><strong>If you’re comfortable with JavaScript or any of the frontend frameworks like Backbone/Ember/Angular, this tutorial is NOT for you</strong>, and you’ll be very frustrated with my writing style. There are tons of great tutorials you can learn from, including the <a target="_blank" href="https://reactjs.org/tutorial/tutorial.html">official React tutorial</a>.</p>
<p>Also, <strong>if you already know React</strong>, you’ll be pretty upset with me as well because I’ll be talking mostly about <strong>state</strong> instead of immutability or components. However, I found that teaching state first is the best way for jQuery developers to see why React is superior.</p>
<p>Anyways, let’s get started!</p>
<h3 id="heading-time-estimate-1-2-hours">Time Estimate: 1 ~ 2 hours</h3>
<p>If you go really fast (and copy-paste example code instead of typing), this tutorial should take a bit over an hour. If you take your time, it should take a little over 2 hours.</p>
<h4 id="heading-if-youre-stuck">If you’re stuck</h4>
<p>If you’re stuck or have questions, you can tweet the original author <a target="_blank" href="https://twitter.com/chibicode">@chibicode</a> or me <a target="_blank" href="https://twitter.com/julienbenc">@julienbenc</a>.</p>
<h3 id="heading-overview-were-going-to-build-a-tweet-box">Overview: We’re Going to Build a “Tweet Box”</h3>
<p>Many React tutorials begin by explaining how React works or why React is awesome. This tutorial does not.</p>
<p>Instead, we’ll get right to building a simple UI, alternating between jQuery implementations and React implementations, explaining the differences along the way. I believe that you’ll think more this way as opposed to just typing out examples.</p>
<p>The UI we’ll build will resemble the Tweet box that you find on <a target="_blank" href="https://twitter.com/">Twitter</a>. It won’t be exactly like the real Tweet box but it’ll be pretty similar. Hopefully you’ll find this example to be practical.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*R8wu8o8n48hwUQMaQ2Atlg.png" alt="Image" width="800" height="205" loading="lazy"></p>
<h3 id="heading-step-1-introduction-to-codepen-510-minutes">Step 1: Introduction to CodePen (5–10 minutes)</h3>
<p>We’ll be using <a target="_blank" href="https://codepen.io/">CodePen</a>, an online code editor which supports both jQuery and React code. You might be familiar with similar services like <a target="_blank" href="https://jsbin.com">JSBin</a> or <a target="_blank" href="https://jsfiddle.net/">JSFiddle</a> — they’re all pretty similar but I went with CodePen for easier embedding.</p>
<p>Here’s an example Pen:</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/XoYQyj" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<p>Click “Run Pen” to see what happens when the code is run as well as the code itself (by clicking on the <code>HTML</code> button).</p>
<p>Next, click on “Edit on CodePen” to open the Pen in a new window. <strong>You can now modify the HTML on the top left</strong> — i.e. change the button’s text. You’ll see the changes appear in the bottom half of the window. That’s how CodePen works.</p>
<h4 id="heading-create-a-codepen-account">Create a CodePen Account</h4>
<p>Unless you already have a CodePen account, <strong>head to</strong> <a target="_blank" href="https://codepen.io/"><strong>https://codepen.io/</strong></a> to <strong>create an account</strong>. Click <strong>Sign Up</strong> to create your account.</p>
<p>After creating an account, you can <strong>fork</strong> public Pens to your account. More or less the same as forking a GitHub repository into your account.</p>
<p>Let’s try it. <strong>Open this next Pen in a new tab and click “Fork” in the top-right menu.</strong></p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/XoYQyj" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<p>Once you’re on the Pen, you can import popular open-source libraries. You do that by opening Settings and then heading to either the CSS or JavaScript tabs where you can then search for the library you want to add.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*fhlnejWv3z1PLhdbsyWjPg.png" alt="Image" width="800" height="881" loading="lazy"></p>
<p><em>CSS settings in CodePen</em></p>
<p><strong>Try doing the following in your forked Pen:</strong></p>
<ul>
<li><p>Add the latest Bootstrap from the CSS tab (the name will be “twitter-bootstrap”)</p>
</li>
<li><p>Add <code>btn btn-primary</code> classes on the <code>&lt;button&gt;</code> tag</p>
</li>
</ul>
<p>And the output becomes a little prettier:</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/dwKEWg" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<h4 id="heading-create-a-tweet-box">Create a Tweet Box</h4>
<p>You seem to be pretty comfortable with CodePen now. Alright, let’s build out a Tweet box. Still on the same Pen as before, <strong>change the HTML inside</strong> <code>&lt;body&gt;</code> like this:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card bg-light"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card-body text-right"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">textarea</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"form-control"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">textarea</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn btn-primary"</span>&gt;</span>Tweet<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>We’re using Bootstrap classes like <code>form-control</code>, <code>card</code>, <code>card-body</code>, etc. but those are just for the looks and irrelevant for the tutorial. Here’s the result:</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/jXKgZo" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<p>That’s it for the first step! Not too bad, eh?</p>
<h3 id="heading-step-2-implement-the-first-feature-tweet-button-should-initially-be-disabled-5-minutes">Step 2: Implement the First Feature — Tweet Button Should Initially Be Disabled (5 minutes)</h3>
<p>Now, time for some JS. We’ll first implement the following feature:</p>
<p><strong>Feature 1:</strong> the “Tweet” button should initially be disabled. When there’s at least one character in the text field, the “Tweet” button should be enabled.</p>
<p>Here’s the demo Pen. As you can see, the button is initially disabled. If you type something into the text box, the button becomes enabled.</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/gZKVjd" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<p>To get this to work, you first need to add jQuery into the Pen. Do that inside the JavaScript tab in your Pen’s Settings. (If you’re having trouble with this step, check out CodePen’s <a target="_blank" href="https://blog.codepen.io/documentation/editor/using-javascript-libraries/">official instructions</a>.) <strong>Once that’s done, go to the small JavaScript window and add the following jQuery code.</strong></p>
<pre><code class="lang-js"><span class="hljs-comment">// Initially disable the button</span>
$(<span class="hljs-string">"button"</span>).prop(<span class="hljs-string">"disabled"</span>, <span class="hljs-literal">true</span>);
<span class="hljs-comment">// When the value of the text area changes...</span>
$(<span class="hljs-string">"textarea"</span>).on(<span class="hljs-string">"input"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// If there's at least one character...</span>
  <span class="hljs-keyword">if</span> ($(<span class="hljs-built_in">this</span>).val().length &gt; <span class="hljs-number">0</span>) {
    <span class="hljs-comment">// Enable the button.</span>
    $(<span class="hljs-string">"button"</span>).prop(<span class="hljs-string">"disabled"</span>, <span class="hljs-literal">false</span>);
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-comment">// Else, disable the button.</span>
    $(<span class="hljs-string">"button"</span>).prop(<span class="hljs-string">"disabled"</span>, <span class="hljs-literal">true</span>);
  }
});
</code></pre>
<h4 id="heading-explanation">Explanation</h4>
<ul>
<li><p>I used the tag names <code>button</code> and <code>textarea</code> as selectors — no need to add IDs or classes for this example.</p>
</li>
<li><p>To enable/disable the button, use <code>$(...).prop("disabled", ...)</code>.</p>
</li>
<li><p>To listen for changes in <code>textarea</code>, use the <code>input</code> event which works on modern browsers.</p>
</li>
</ul>
<p><strong>Try it out</strong> by typing some text in the Tweet box and seeing the button’s enabled/disabled state change.</p>
<p>DO NOT PROCEED if this example was confusing to you — you might need to learn some more jQuery before moving onto React. There are lots of excellent learning resources like <a target="_blank" href="https://www.codecademy.com">Codecademy</a>, <a target="_blank" href="https://teamtreehouse.com/">Treehouse</a>, <a target="_blank" href="https://www.pluralsight.com/codeschool">Code School</a>, and others.</p>
<p>Now that this feature is complete, we’ll try to re-implement the same thing using React. This will take several steps.</p>
<h3 id="heading-step-3-the-tweet-box-using-react-510-minutes">Step 3: The Tweet Box Using React (5–10 minutes)</h3>
<p>One of the first things you’ll notice in React is that <strong>you’ll be writing markup in JS, not in HTML</strong>.</p>
<p>Let me show you what I mean. Here’s the React code which displays the same Tweet box.</p>
<h4 id="heading-warning-you-dont-need-to-follow-along-yet-just-read-the-code">WARNING! You don’t need to follow along yet — just read the code.</h4>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/majbMg" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<p>Some observations:</p>
<ul>
<li><p>Inside <code>return (...)</code> is HTML-like code, not JavaScript. In React, you’ll write in a special syntax called JSX which lets you put HTML-like code inside JavaScript.</p>
</li>
<li><p>I say HTML-“like” because it’s not identical to HTML. Notice that it uses <code>className</code> instead of <code>class</code> — but it’s pretty similar, so you’ll learn it quickly.</p>
</li>
<li><p>Your browser does not understand JSX so, before the code can be run, it is automatically converted into browser-compatible JavaScript by a JS compiler (called Babel).</p>
</li>
<li><p>The HTML code inside <code>return (...)</code> is pretty much identical to the HTML code from step 1.</p>
</li>
<li><p>Look at the remaining HTML code in our Pen and you’ll see that there’s no markup besides <code>&lt;body&gt;&lt;div id="container"&gt;&amp;</code>lt;/div&gt;. This is what I <strong>meant when I said that in React you’ll be writing markup in JavaScrip</strong>t (JSX) and not in HTML.</p>
</li>
</ul>
<h4 id="heading-frequently-asked-questions-amp-answers">Frequently Asked Questions &amp; Answers</h4>
<p><strong>Question:</strong> What do <code>class TweetBox extends React.Component</code> and <code>ReactDOM.render</code> do? Do I need to understand them now?<br><strong>Answer:</strong> Don’t worry about it for now. Basically, the first declares a React component with a name (in this case, <code>TweetBox</code>). This then gets rendered in the DOM via <code>ReactDOM.render(&lt;TweetBox /&gt;, document.getElementById("contai</code>ner")) — meaning this component is added insid<code>e the &lt;div id="co</code>ntainer"&gt; tag. That’s all you need to know for now.</p>
<p><strong>Question:</strong> Do I need to do anything special to write JSX on my local machine?<br><strong>Answer:</strong> Yes, but that’s outside the scope of this tutorial — in short, you need to enable something called Babel compilation. All you need to do to write JSX on CodePen is to (1) add the React and ReactDOM libraries and (2) select “Babel” from the list of JavaScript Preprocessors in the JS settings window.</p>
<p><strong>Question:</strong> Isn’t it bad practice to write markup (HTML) and behavior (JS) in the same place?<br><strong>Answer:</strong> It might be bad practice for simple web pages but not necessarily so for large web applications. In large web applications, there will be hundreds of pieces of UI, each containing their own markup and behaviors. The code will be more manageable if the markup and behaviors are kept together for each piece of UI, as opposed to keeping “all markup” together and “all behaviors” together. And React is designed for developing large web applications. In fact, React is developed and used by Facebook, one of the largest web applications out there.</p>
<p>Next, I’ll show you how to write the above React code step-by-step.</p>
<h3 id="heading-step-4-writing-your-first-react-code-510-minutes">Step 4: Writing Your First React Code (5–10 minutes)</h3>
<p>Here’s a starter Pen. In it, I’ve imported Bootstrap (the CSS portion) and React. I also set the JavaScript Preprocessor to Babel so we can write classes and JSX.</p>
<p><strong>Please try to follow along. To begin, fork this Pen so you can edit and save as you go.</strong></p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/YdjeWj" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<p>Now you’re ready to write some React. <strong>Try to follow along and type the following JS code snippets</strong> into your Pen.</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TweetBox</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  render() {
    <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;
  }
};
</code></pre>
<p>This is the template for creating a piece of UI using React (in this case, a Tweet box). It’s just as essential as <code>$(selector).append('your HTML code or element')</code> in jQuery.</p>
<p>To actually construct the UI, we must write the <code>render()</code> method. For now, let’s keep it simple with just a single <code>div</code> tag.</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TweetBox</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        Hello World!
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
};
</code></pre>
<p>Like in the example above, <strong>put a pair of parenthesis after</strong> <code>return</code>, and write the markup inside.</p>
<h4 id="heading-jsx-gotchas">JSX Gotchas</h4>
<p>There’s one thing you need to remember with JSX — in <code>render()</code>, you must return only <strong>one</strong> outermost tag (or anything that can be considered a valid DOM node such as a string or a string).</p>
<p>This will work because it’s a string:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">return</span> <span class="hljs-string">'Hello World!'</span>;
</code></pre>
<p>But the following won’t work because there’s no quotes or outermost tag around the text:</p>
<pre><code class="lang-js"><span class="hljs-keyword">return</span> (
  Hello World!
);
</code></pre>
<p>This also doesn’t work because there are two outer-most (<code>span</code>) tags inside <code>return (…)</code>:</p>
<pre><code class="lang-js"><span class="hljs-keyword">return</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>
    Hello
  <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span></span>
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>
    World
  <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span></span>
);
</code></pre>
<p>For the above example, the workaround is to create an extra <code>div</code> tag to wrap the two <code>span</code> tags.</p>
<pre><code class="lang-js"><span class="hljs-keyword">return</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>
      Hello
    <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>
      World
    <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
);
</code></pre>
<p>We used a <code>div</code> here but in the most recent versions of React, you can use the Fragment feature to render multiple outermost tags. Like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">return</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">React.Fragment</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>
      Hello
    <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>
      World
    <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">React.Fragment</span>&gt;</span></span>
);
</code></pre>
<h4 id="heading-attaching-the-ui-to-the-dom">Attaching the UI to the DOM</h4>
<p>Now we need to “attach” this UI to the DOM in order to see <code>Hello World</code>. To do this, <strong>add</strong> <code>ReactDOM.render()</code> underneath the code we just wrote:</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TweetBox</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  ...
};
ReactDOM.render(
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">TweetBox</span> /&gt;</span></span>,
  <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"container"</span>)
);
</code></pre>
<p>(<strong>Note</strong>: an ellipsis (…) in the code snippet indicates code that has been omitted for clarity. In other words, don’t touch this part of the code and leave it as is.)</p>
<p><code>ReactDOM.render</code> takes two arguments. The first argument is the React component, which is <code>&lt;VariableName</code> /&gt;. The second argument is the DOM element where we want to render (in this <code>case, document.getElementById('conta</code>iner')). Put together, the above code render<code>s the Tw</code>eetBox UI i<code>nside &lt;div id="co</code>ntainer"&gt;.</p>
<p>Now, you should see <code>Hello World</code> appear in your Pen. Congratulations, you wrote and rendered your first React component!</p>
<h4 id="heading-write-the-actual-html-for-the-tweet-box">Write the Actual HTML for the Tweet Box</h4>
<p>Now, instead of <code>Hello World</code>, we’ll implement the HTML for the Tweet Box. <strong>Swap the code inside</strong> <code>render()</code> with this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">return</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"card bg-light"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"card-body text-right"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">textarea</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"form-control"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"btn btn-primary"</span>&gt;</span>Tweet<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
);
</code></pre>
<p>There are two things you need to watch out for:</p>
<ul>
<li><p><strong>Don’t use</strong> <code>class</code>. Instead, use <code>className</code>. It’s because JSX gets translated to JS and <code>class</code> is a reserved keyword in JS.</p>
</li>
<li><p><strong>If you use</strong> <code>&lt;</code>br&gt; inste<code>ad of</code><br>  , you’ll get an error. Make sure to close all tags. Same thing w<code>ith images: &lt;img src</code>\="…" alt="…" /&gt;</p>
</li>
</ul>
<p>Everything else should be the same as the jQuery example from before.</p>
<p>If you typed this correctly, then you should see the Tweet box in your Pen. <strong>If nothing appears in the output, check your code very carefully to make sure there aren’t any typos.</strong></p>
<p>That’s it for this step! Here’s the Pen up to this part:</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/majbMg" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<h3 id="heading-step-5-re-implement-the-first-feature-tweet-button-should-initially-be-disabled-in-react-510-minutes">Step 5: Re-implement the First Feature — Tweet Button Should Initially Be Disabled — in React (5–10 minutes)</h3>
<p>We’re going to re-implement with React the first feature we implemented using jQuery:</p>
<p><strong>Feature 1:</strong> The “Tweet” button should initially be disabled. When there’s at least one character in the text field, the “Tweet” button should be enabled.</p>
<p>Here’s the jQuery code we wrote:</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/gZKVjd" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<p>Let’s see how we can do this in React.</p>
<p><strong>Start with your Pen from the previous step.</strong> (<strong>Tip:</strong> Since you won’t be touching HTML in React, <strong>you can minimize the HTML tab on CodePen</strong> to get more screen space. Same thing with the CSS tab.)</p>
<p><strong>First, let’s disable the button by adding</strong> <code>disabled</code> as an attribute.</p>
<pre><code class="lang-js">render() {
  <span class="hljs-keyword">return</span> (
    ...
    &lt;button className=<span class="hljs-string">"..."</span> disabled&gt;Tweet&lt;/button&gt;
    ...
  );
}
</code></pre>
<p>In JSX, this is equivalent to writing <code>disabled={true}</code>.</p>
<p>The button should now be disabled. Note that in our jQuery implementation we wrote:</p>
<pre><code class="lang-js">$(<span class="hljs-string">"button"</span>).prop(<span class="hljs-string">"disabled"</span>, <span class="hljs-literal">true</span>);
</code></pre>
<p>to initially disable the button, but we could have instead modified the button tag like above.</p>
<p>Now, we need to enable the button when there’s at least one character in the text field.</p>
<h4 id="heading-handle-change-event">Handle Change Event</h4>
<p>First, we need to listen for the user typing in the <code>textarea</code>. In our jQuery implementation, we wrote:</p>
<pre><code class="lang-js">$(<span class="hljs-string">"textarea"</span>).on(<span class="hljs-string">"input"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  ...
}
</code></pre>
<p>In the React world, we write the event handler as a <strong>class method</strong>. Let’s call it <code>handleChange</code>:</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TweetBox</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  handleChange = <span class="hljs-function">() =&gt;</span> {
  };
  render() {
    ...
  }
}
</code></pre>
<blockquote>
<p>Note that we’re using an arrow function so that we can access the class’s context (<code>this</code>) without having to bind the function in the <code>constructor</code>. Explaining this in-depth is outside the scope of this tutorial but you will very likely learn about it in due time.</p>
</blockquote>
<p>Next, we call this handler when text is entered. To do so, <strong>modify the</strong> <code>textarea</code> tag in <code>render()</code> like this:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">textarea</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"form-control"</span> <span class="hljs-attr">onChange</span>=<span class="hljs-string">{this.handleChange}</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">textarea</span>&gt;</span>
</code></pre>
<ul>
<li><p>We used the <code>input</code> event for jQuery but in React we use <code>onChange</code> — you’ll learn about how events differ in React’s JSX from the official React documentation so don’t worry too much for now.</p>
</li>
<li><p><strong>More importantly</strong>, we used curly brackets to include JavaScript code inside the HTML syntax part of JSX. In this case, we passed the handler <code>handleChange</code> and we prefixed it with <code>this</code> because it’s a class method.</p>
</li>
<li><p>If you’re used to jQuery, this might seem like bad practice but don’t worry. Again, in large applications, the code will be more manageable if the markup and behaviors are kept together for each piece of UI.</p>
</li>
</ul>
<p>To make sure that the handler is indeed being called, <strong>let’s add</strong> <code>console.log</code> inside <code>handleChange</code>:</p>
<pre><code class="lang-js">handleChange = <span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(e.target.value);
};
</code></pre>
<p>The <code>event</code> object (nicknamed <code>e</code>) contains <code>target</code> which is the <code>textarea</code>. We get the <code>value</code> to output the current content of the <code>textarea</code>.</p>
<p><strong>In your Pen, open the</strong> <code>console</code> tab (with the button in the bottom left of the screen) to check the output. Then type something in the Tweet box.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*8mSEfP4wqKBTHRGLaR64_Q.png" alt="Image" width="722" height="308" loading="lazy"></p>
<p><em>The console button in CodePen</em></p>
<p>You can also try it out here (you’ll need to open the Pen in a new tab to see the <code>console</code> button):</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/aPaoOJ" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<p>That’s it for this step! We’ll finish this feature in the next step.</p>
<p><strong>NOTE: Close the</strong> <code>console</code> tab in CodePen when you’re done. We no longer need it.</p>
<h3 id="heading-step-6-implementing-state-1015-minutes">Step 6: Implementing State (10–15 minutes)</h3>
<p>I’ll now explain one of the biggest differences between jQuery-style code and React-style code.</p>
<p>In jQuery, when some event happens, you usually modify the DOM directly (like we did earlier with <code>$("button").prop("disabled", true)</code>):</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*IhZHTYtjhRnTru2C" alt="Image" width="601" height="242" loading="lazy"></p>
<p>In React, <strong>you never modify the DOM directly.</strong> Instead, in an event handler, you modify something called <strong>the “component state”</strong>. And this is done by calling <code>this.setState</code>.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*vB6BPnDRykxd2PFP" alt="Image" width="647" height="206" loading="lazy"></p>
<p>Then, every time the <code>state</code> is updated, <code>**render()**</code> <strong>is called again.</strong> And <strong>inside</strong> <code>render()</code> you can access the state to tell React how you want the DOM to be modified.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*g9bDPyxJef0yg00Y" alt="Image" width="641" height="346" loading="lazy"></p>
<p>This is how you update the UI in response to an event. Yes it’s confusing at first so let me explain using code.</p>
<h4 id="heading-writing-the-event-handler">Writing the Event Handler</h4>
<p><strong>Start with your Pen from the previous step.</strong> First, we need to <strong>initialize the state object.</strong> We can do that inside the class <code>constructor</code>.</p>
<p>What goes in the object? <strong>Let’s create a single key called</strong> <code>text</code> and have it store whatever is inside the Tweet box.</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TweetBox</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  <span class="hljs-keyword">constructor</span>(props) {
    <span class="hljs-built_in">super</span>(props);

    <span class="hljs-built_in">this</span>.state = {
      <span class="hljs-attr">text</span>: <span class="hljs-string">''</span>,
    };
  }

  handleChange = <span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> {...};
  render() {...}
};
</code></pre>
<blockquote>
<p>Don’t worry about why we’re calling <code>super(props)</code> at the top of our <code>constructor</code>. It’s an important step but not necessary to understand React for now.</p>
</blockquote>
<p><strong>Next, we’ll modify the event handler</strong> to set the state’s <code>text</code> field to whatever is currently in the <code>textarea</code>. To do this, <strong>we use a special method called</strong> <code>setState</code> and pass the updated key-value pair.</p>
<pre><code class="lang-js">handleChange = <span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> {
  <span class="hljs-built_in">this</span>.setState({ <span class="hljs-attr">text</span>: e.target.value });
};
</code></pre>
<p>Now, let’s check that the state is correctly being set by writing some debug-only code in <code>render()</code>.</p>
<p>To do this, <strong>simply add</strong> <code>this.state.text</code> near the end of <code>render()</code> and use curly brackets to use JS code inside the HTML syntax part of JSX.</p>
<pre><code class="lang-js">render() {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"card bg-light"</span>&gt;</span>
      ...
      {this.state.text}
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p><strong>Now, try entering some text in the Tweet box.</strong> The same text should appear below the button.</p>
<p>You can try it out on the Pen below as well:</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/XoPrzR" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<p>Now the previous diagram might make more sense to you:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*8hdVUZaiVqm8J2mq" alt="Image" width="641" height="346" loading="lazy"></p>
<h4 id="heading-remove-the-debugging-code">Remove the Debugging Code</h4>
<p>Once you confirm that the state is correctly being set, <strong>remove the debugging code we just added:</strong></p>
<pre><code class="lang-javascript">{<span class="hljs-built_in">this</span>.state.text}
</code></pre>
<h4 id="heading-enablingdisabling-the-button">Enabling/Disabling the Button</h4>
<p>Now that we can listen for text changes, the next step is to enable/disable the <code>button</code> depending on whether the <code>text</code> is empty or not.</p>
<p>Using the <code>state</code>, we can use this logic:</p>
<ul>
<li>If <code>this.state.text.length === 0</code>, the button should be disabled.</li>
</ul>
<p>To do this in React, <strong>add the</strong> <code>disabled</code> attribute and set its value as the output of <code>this.state.text.length === 0</code>. Since this is JS code, you need to wrap it with <code>{}</code>.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"btn btn-primary"</span> <span class="hljs-attr">disabled</span>=<span class="hljs-string">{this.state.text.length</span> === <span class="hljs-string">0}</span>&gt;</span>Tweet<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
</code></pre>
<p>If you write <code>disabled="true"</code> or <code>disabled="false"</code> in raw HTML it won’t work — in raw HTML, you need to add/remove the <code>disabled</code> attribute to enable the <code>button</code>. But React is <strong>not</strong> raw HTML — it does the following behind the scenes:</p>
<ul>
<li><p>If you write <code>disabled={true}</code> in JSX, it gets converted to just <code>&lt;button disabl</code>ed&gt; in HTML.</p>
</li>
<li><p>If you write <code>disabled={false}</code> in JSX, the <code>disabled</code> attribute is removed from the <code>button</code> tag in HTML.</p>
</li>
</ul>
<p>This works with other Boolean attributes like <code>checked</code>. (You can read more about this aspect of JSX <a target="_blank" href="https://reactjs.org/docs/dom-elements.html">here</a>.)</p>
<p>The resulting Pen is below:</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/GPXKYa" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<h4 id="heading-reflections">Reflections</h4>
<p>Again, keep this difference between jQuery and React in mind before moving on to the next step:</p>
<ul>
<li><p>In jQuery, you write event handlers which <strong>modify the DOM</strong>.</p>
</li>
<li><p>In React, you write event handlers which <strong>modify the state</strong>. And you write <code>render()</code> to reflect the current state.</p>
</li>
</ul>
<h3 id="heading-step-7-remaining-character-count-in-jquery-5-minutes">Step 7: Remaining Character Count in jQuery (5 minutes)</h3>
<p>The next feature we’ll implement is the remaining character count.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*9ONPNObC5R_IeBvq" alt="Image" width="800" height="230" loading="lazy"></p>
<p>Here’s the spec:</p>
<ul>
<li>The character count will display <code>280 — the length of the text</code>.</li>
</ul>
<p><strong>We’ll first implement this in jQuery, then in React.</strong></p>
<p>We’ll start with our previous jQuery implementation and put our React code on hold for now. <strong>Going forward, I will give you new code to start with at the beginning of each chapter</strong>, as we alternate between jQuery and React. That means after you’re done with each step, you can play with the code before moving to the next step.</p>
<p><strong>✔ Fork the Pen below</strong> to get started.</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/gZKVjd" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<p>First, <strong>add the character count in HTML.</strong> Let’s set it inside a <code>span</code>:</p>
<pre><code class="lang-js">&lt;textarea {...}&gt;&lt;/textarea&gt;<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>280<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">button</span> {<span class="hljs-attr">...</span>}&gt;</span>Tweet<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>
</code></pre>
<p>And <strong>inside the input handler in JS, add this code to update the character count:</strong></p>
<pre><code class="lang-js">$(<span class="hljs-string">"textarea"</span>).on(<span class="hljs-string">"input"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  $(<span class="hljs-string">"span"</span>).text(<span class="hljs-number">280</span> - $(<span class="hljs-built_in">this</span>).val().length);
  ...
});
</code></pre>
<p>That’s it! <strong>Try typing in the Tweet box</strong> and you’ll see the character count gets updated as you type. Here’s the Pen:</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/gZdJNJ" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<h3 id="heading-step-8-remaining-character-count-in-react-5-minutes">Step 8: Remaining Character Count in React (5 minutes)</h3>
<p>How about in React? You should try doing this on your own. Start with our previous React implementation.</p>
<p><strong>✔ Fork the Pen below</strong> to get started.</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/GPXKYa" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<p>(<strong>Tip:</strong> Since you won’t be touching HTML in React, <strong>you can minimize the HTML tab on CodePen</strong> to get more screen space.)</p>
<p><strong>Hints:</strong></p>
<ul>
<li><p>No need to change the <code>constructor()</code> or <code>handleChange()</code> methods.</p>
</li>
<li><p>Use <code>this.state.text.length</code> in <code>render()</code>.</p>
</li>
</ul>
<h4 id="heading-answer">Answer:</h4>
<p>Add this code after <code>&lt;b</code>r/&gt; in <code>your re</code>nder():</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>{280 - this.state.text.length}<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
</code></pre>
<p>Here’s the Pen:</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/QzVXOd" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<p>Too easy? Not sure why building UIs with React is so much better than jQuery? Well, the next step has more complexity and this is where React really starts to shine.</p>
<h3 id="heading-step-9-the-add-photo-button-5-minutes">Step 9: The “Add Photo” Button (5 minutes)</h3>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*N697jN99MCP3cfUn" alt="Image" width="800" height="230" loading="lazy"></p>
<p>For our next feature, we’ll add an “Add Photo” button to the Tweet Box. This is where things start to get tricky.</p>
<p>However, <strong>we won’t actually write the code to upload images.</strong> Instead, here’s what we’re going to do:</p>
<p>When you upload a photo on Twitter, it counts against the character limit. On my attempt, it decreased the number of remaining characters from 280 to 257.</p>
<blockquote>
<p>Yes, I know that Twitter no longer counts photos against the character limit but we’ll disregard that for this tutorial.</p>
</blockquote>
<p>Here’s the spec:</p>
<ul>
<li><p>Create an “Add Photo” button.</p>
</li>
<li><p>Clicking this button toggles an <strong>ON/OFF state.</strong></p>
</li>
<li><p>If the button is ON, it will say <code>**✓ Photo Added**</code> and the number of available characters decreases by 23.</p>
</li>
<li><p>Also, if the button is ON, <strong>even if there’s no text entered, the “Tweet” button remains enabled.</strong></p>
</li>
</ul>
<p>Here’s the demo CodePen. <strong>Try clicking the “Add Photo” button</strong> and see what happens to the character count and the Tweet button.</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/roZXvE" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<p>Let’s implement this with jQuery first.</p>
<h3 id="heading-step-10-the-add-photo-button-in-jquery-1520-minutes">Step 10: The “Add Photo” Button in jQuery (15–20 minutes)</h3>
<p>Start with the latest version of our jQuery implementation.</p>
<p><strong>✔ Fork the Pen below</strong> to get started.</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/gZdJNJ" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<p>Earlier, we were attaching a handler to <code>$("button")</code> but this won’t work anymore if we have two buttons. <strong>So let’s modify the HTML like this:</strong></p>
<pre><code class="lang-html">...
<span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"js-tweet-button btn btn-primary"</span> <span class="hljs-attr">disabled</span>&gt;</span>Tweet<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"js-add-photo-button btn btn-secondary"</span>&gt;</span>Add Photo<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
...
</code></pre>
<p>Here are the changes:</p>
<ul>
<li><p><strong>Added the second button</strong> which says “Add Photo”.</p>
</li>
<li><p><strong>Added classes</strong> <code>js-tweet-button</code> and <code>js-add-photo-button</code> to each button. The class names are prefixed with <code>js-</code> to remember that they’re used only in JS and not in CSS.</p>
</li>
<li><p><strong>Added the initial</strong> <code>disabled</code> attribute to the Tweet button so we don’t have to do it in JS.</p>
</li>
</ul>
<p><strong>Next, rewrite the entire JS file like this:</strong></p>
<pre><code class="lang-js">$(<span class="hljs-string">"textarea"</span>).on(<span class="hljs-string">"input"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  $(<span class="hljs-string">"span"</span>).text(<span class="hljs-number">280</span> - $(<span class="hljs-built_in">this</span>).val().length);
  <span class="hljs-keyword">if</span> ($(<span class="hljs-built_in">this</span>).val().length &gt; <span class="hljs-number">0</span>) {
    $(<span class="hljs-string">".js-tweet-button"</span>).prop(<span class="hljs-string">"disabled"</span>, <span class="hljs-literal">false</span>);
  } <span class="hljs-keyword">else</span> {
    $(<span class="hljs-string">".js-tweet-button"</span>).prop(<span class="hljs-string">"disabled"</span>, <span class="hljs-literal">true</span>);
  }
});
</code></pre>
<p>Here are the changes:</p>
<ul>
<li><p><strong>(Important) Removed</strong> <code>$("button").prop("disabled", true);</code> from the first line because we added the <code>disabled</code> attribute directly to the Tweet button.</p>
</li>
<li><p><strong>Replaced</strong> <code>$("button")</code> with <code>$(".js-tweet-button")</code> so it can be distinguished from <code>.js-add-photo-button</code>.</p>
</li>
</ul>
<h4 id="heading-adding-the-button">Adding the Button</h4>
<p>Next, we’ll implement one of the features:</p>
<ul>
<li>Clicking the “Add Photo” button toggles the ON/OFF state. <strong>If it’s ON, the button will say</strong> <code>✓ Photo Added</code>.</li>
</ul>
<p>To do this, <strong>let’s add this piece of code:</strong></p>
<pre><code class="lang-js">$(<span class="hljs-string">"textarea"</span>).on(<span class="hljs-string">"input"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  ...
});

$(<span class="hljs-string">".js-add-photo-button"</span>).on(<span class="hljs-string">"click"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span> ($(<span class="hljs-built_in">this</span>).hasClass(<span class="hljs-string">"is-on"</span>)) {
    $(<span class="hljs-built_in">this</span>)
      .removeClass(<span class="hljs-string">"is-on"</span>)
      .text(<span class="hljs-string">"Add Photo"</span>);
  } <span class="hljs-keyword">else</span> {
    $(<span class="hljs-built_in">this</span>)
      .addClass(<span class="hljs-string">"is-on"</span>)
      .text(<span class="hljs-string">"✓ Photo Added"</span>);
  }
});
</code></pre>
<p>We use the class <code>is-on</code> to keep track of the state. <strong>Check to see that this works</strong> by clicking the “Add Photo” button multiple times and seeing the text alternate.</p>
<h4 id="heading-decrement-character-count">Decrement Character Count</h4>
<p>Next, we’ll implement this feature:</p>
<ul>
<li>If the “Add Photo” button is ON, <strong>the number of available characters decreases by 23.</strong></li>
</ul>
<p>To do this, <strong>modify the click handler we just added like this.</strong></p>
<pre><code class="lang-js"><span class="hljs-keyword">if</span> ($(<span class="hljs-built_in">this</span>).hasClass(<span class="hljs-string">"is-on"</span>)) {
  $(<span class="hljs-built_in">this</span>)
    .removeClass(<span class="hljs-string">"is-on"</span>)
    .text(<span class="hljs-string">"Add Photo"</span>);
  $(<span class="hljs-string">"span"</span>).text(<span class="hljs-number">280</span> - $(<span class="hljs-string">"textarea"</span>).val().length);
} <span class="hljs-keyword">else</span> {
  $(<span class="hljs-built_in">this</span>)
    .addClass(<span class="hljs-string">"is-on"</span>)
    .text(<span class="hljs-string">"✓ Photo Added"</span>);
  $(<span class="hljs-string">"span"</span>).text(<span class="hljs-number">280</span> - <span class="hljs-number">23</span> - $(<span class="hljs-string">"textarea"</span>).val().length);
}
</code></pre>
<p>We change the <code>span</code>’s content on every click. If the <code>button</code> is ON, we need to subtract the text length from <code>257</code> (i.e. <code>280 — 23</code>). We use <code>280 — 23</code> for clarity right now but, if we were building a production app, we should use constants instead.</p>
<p><strong>Check to see that this works</strong> by clicking the “Add Photo” button.</p>
<h4 id="heading-fixing-the-input-handler">Fixing the Input Handler</h4>
<p>This isn’t complete however — <strong>if you have the “Add Photo” button ON and start typing in the</strong> <code>textarea</code>, the remaining character count goes out of sync.</p>
<p>This happens because the handler for the <code>textarea</code> doesn’t take into account the status of the “Add Photo” button.</p>
<p>To fix this, <strong>we need to update the handler for</strong> <code>textarea</code> like this:</p>
<pre><code class="lang-js">$(<span class="hljs-string">"textarea"</span>).on(<span class="hljs-string">"input"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span> ($(<span class="hljs-string">".js-add-photo-button"</span>).hasClass(<span class="hljs-string">"is-on"</span>)) {
    $(<span class="hljs-string">"span"</span>).text(<span class="hljs-number">280</span> - <span class="hljs-number">23</span> - $(<span class="hljs-built_in">this</span>).val().length);
  } <span class="hljs-keyword">else</span> {
    $(<span class="hljs-string">"span"</span>).text(<span class="hljs-number">280</span> - $(<span class="hljs-built_in">this</span>).val().length);
  }

  <span class="hljs-keyword">if</span> (...) {
    ...
  }
});
</code></pre>
<p><strong>Make sure that this works</strong> by turning on the “Add Photo” button and then typing some text.</p>
<h4 id="heading-i-know-this-is-taking-some-time">I know this is taking some time…</h4>
<p>But stick with it! The jQuery code here is <strong><em>supposed</em></strong> to be confusing so don’t worry!</p>
<h4 id="heading-implement-the-last-feature">Implement the Last Feature</h4>
<p>The last feature we need to implement is this:</p>
<ul>
<li>If the “Add Photo” button is ON, <strong>even if there’s no text entered, the “Tweet” button remains enabled.</strong></li>
</ul>
<p>To do this, <strong>we need to modify the click handler of the “Add Photo” button:</strong></p>
<pre><code class="lang-js">$(<span class="hljs-string">".js-add-photo-button"</span>).on(<span class="hljs-string">"click"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span> ($(<span class="hljs-built_in">this</span>).hasClass(<span class="hljs-string">"is-on"</span>)) {
    ...
    if ($(<span class="hljs-string">"textarea"</span>).val().length === <span class="hljs-number">0</span>) {
      $(<span class="hljs-string">".js-tweet-button"</span>).prop(<span class="hljs-string">"disabled"</span>, <span class="hljs-literal">true</span>);
    }
  } <span class="hljs-keyword">else</span> {
    ...
    $(<span class="hljs-string">".js-tweet-button"</span>).prop(<span class="hljs-string">"disabled"</span>, <span class="hljs-literal">false</span>);
  }
});
</code></pre>
<p>Here’s the explanation:</p>
<ul>
<li><p>If the “Add Photo” button is going from ON to OFF (<code>if</code> clause), we need to check if there’s no text entered and, if so, disable the “Tweet” button.</p>
</li>
<li><p>If the “Add Photo” button is going from OFF to ON (<code>else</code> clause), we always enable the “Tweet” button.</p>
</li>
</ul>
<h4 id="heading-but-again-this-is-broken">But again, this is broken</h4>
<p>We’re not done yet. There’s a bug in the code right now. <strong>Try it out yourself by following these steps:</strong></p>
<ul>
<li><p>Turn on the “Add Photo” button.</p>
</li>
<li><p>Type some text.</p>
</li>
<li><p>Delete all of the text.</p>
</li>
<li><p>The “Tweet” button should still be enabled because the “Add Photo” button is ON, but this isn’t the case.</p>
</li>
</ul>
<p>This means that our input handler for <code>textarea</code> is missing some logic. To fix this, <strong>we need to add another condition to the</strong> <code>if</code> statement in the input handler.</p>
<pre><code class="lang-js">$(<span class="hljs-string">"textarea"</span>).on(<span class="hljs-string">"input"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  ...
  if ($(<span class="hljs-built_in">this</span>).val().length &gt; <span class="hljs-number">0</span> || $(<span class="hljs-string">".js-add-photo-button"</span>).hasClass(<span class="hljs-string">"is-on"</span>)) {
    ...
  } <span class="hljs-keyword">else</span> {
    ...
  }
});
</code></pre>
<p>This is the explanation for this additional condition:</p>
<ul>
<li>When the text changes, <strong>if the text isn’t empty OR if the “Add Photo” button is ON</strong>, do not disable the “Tweet” button.</li>
</ul>
<p><strong>Try the above steps again</strong> and this time it will work as expected.</p>
<h3 id="heading-step-11-reflection-on-the-jquery-code-why-so-confusing-5-minutes">Step 11: Reflection on the jQuery Code — Why So Confusing? (5 minutes)</h3>
<p>Here’s the final HTML and JS code from the previous step:</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/YdJKqE" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<p><strong>Take a look at the jQuery code once again.</strong> It’s very confusing. If you’re keeping the code as-is, you’ll probably need comments everywhere to remember how it works. There are also clear signs of code duplication but you’d have to think quite a bit before refactoring.</p>
<p>The question is: <strong>why did it get so ugly so fast?</strong></p>
<p>The answer has to do with the <strong>“jQuery style”</strong> of code we talked about previously. Recall this diagram:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*9p-0fEB0Z2DPuFF7" alt="Image" width="601" height="242" loading="lazy"></p>
<p>Things are simple when there is only 1 event handler and 1 DOM element. However, as we just saw, <strong>if several event handlers are modifying several parts of the DOM, the code gets ugly and complicated.</strong></p>
<p>This is an example of what people mean when they say “spaghetti code”.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*YTtV7PD_apkzdYMw" alt="Image" width="601" height="296" loading="lazy"></p>
<p>Imagine adding more features that could influence both the character limit and the “Tweet” button state. The code would become even harder to manage.</p>
<p>You can, in theory, mitigate this by refactoring into reusable functions. But you’d still have to think hard about it every time you add something new.</p>
<blockquote>
<p><strong>Note:</strong> Someone shared a <a target="_blank" href="https://pastebin.com/wbGZZs7U">refactored version of the jQuery code</a> (for the original tutorial). Very clean. You’ll notice that the <code>update()</code> function takes care of most of the updates to the DOM based on its current “state”. The event listeners then run this function on every call.</p>
<p>In that way it’s similar to React’s <code>render</code>. However, there are still many downsides to this solution. For one, the absence of a real <code>state</code> object makes the logic more opaque. It also doesn’t allow you to break down your UI into multiple components and is likely to have performance issues as you continue adding to it.</p>
</blockquote>
<p>Now, let’s see what it’s like to do the same thing with React.</p>
<p><strong>Spoiler alert: It’s going to be much simpler.</strong></p>
<h3 id="heading-step-12-the-add-photo-button-in-react-1020-minutes">Step 12: The “Add Photo” Button in React (10–20 minutes)</h3>
<p>Let’s start with our previous React implementation.</p>
<p><strong>✔ Fork the Pen below</strong> to get started.</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/QzVXOd" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<h4 id="heading-adding-the-button-1">Adding the Button</h4>
<p>First, let’s add the “Add Photo” button. <strong>Modify the JSX:</strong></p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">...</span>&gt;</span>Tweet<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"btn btn-secondary"</span>&gt;</span>
  Add Photo
<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
</code></pre>
<p>Now, let’s add a click handler to this button so that the text changes from <code>Add Photo</code> to <code>✓ Photo Added</code>. Recall the React way of writing code:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*FWEJiHF2VzD8p8L2" alt="Image" width="641" height="346" loading="lazy"></p>
<p>We will:</p>
<ol>
<li><p><strong>Create a state variable</strong> that keeps track of whether the “Add Photo” button is ON or OFF.</p>
</li>
<li><p><strong>Use the state</strong> in <code>render()</code> to decide whether to show <code>Add Photo</code> or <code>✓ Photo Added</code>.</p>
</li>
<li><p><strong>Update the state</strong> in the click handler.</p>
</li>
</ol>
<p>For (1), <strong>we’ll modify the initial state in the</strong> <code>constructor</code> by adding a key-value pair to keep track of whether the photo is added or not:</p>
<pre><code class="lang-js"><span class="hljs-keyword">constructor</span>(props) {
  <span class="hljs-built_in">super</span>(props);

  <span class="hljs-built_in">this</span>.state = {
    <span class="hljs-attr">text</span>: <span class="hljs-string">''</span>,
    <span class="hljs-attr">photoAdded</span>: <span class="hljs-literal">false</span>,
  };
}
</code></pre>
<p>For (2), <strong>we’ll modify the JSX markup</strong> for the “Add Photo” button. We’ll have the button say “Photo Added” if <code>this.state.photoAdded</code> is true. We can just use a <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator">ternary operator</a> here:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"btn btn-secondary"</span>&gt;</span>
  {this.state.photoAdded ? "✓ Photo Added" : "Add Photo" }
<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
</code></pre>
<p>Finally, for (3), <strong>we’ll attach an event handler on JSX</strong> like we did for the <code>textarea</code>:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"btn btn-secondary"</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.togglePhoto}</span>&gt;</span>
  {this.state.photoAdded ? "✓ Photo Added" : "Add Photo" }
<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
</code></pre>
<p>Notice that we’re using <code>onClick</code> instead of <code>onChange</code>. This is because we’re dealing with a <code>button</code> and not a <code>textarea</code> or an <code>input</code>.</p>
<p>We’ll also <strong>add a handler method which toggles the value of</strong> <code>this.state.photoAddded</code>:</p>
<pre><code class="lang-js">togglePhoto = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-built_in">this</span>.setState(<span class="hljs-function">(<span class="hljs-params">prevState</span>) =&gt;</span> ({ <span class="hljs-attr">photoAdded</span>: !prevState.photoAdded }));
}
</code></pre>
<p>This time you’ll see that we’re passing a function to <code>this.setState</code>. This is necessary if you want to update your component state but need to use a value from the previous state. Why we do that is outside the scope of this tutorial but you can read about it in <a target="_blank" href="https://reactjs.org/docs/state-and-lifecycle.html#using-state-correctly">this section</a> of the official React documentation.</p>
<p>Now, clicking on Add Photo should toggle the button text. <strong>Try it out yourself.</strong></p>
<h4 id="heading-decrement-character-count-1">Decrement Character Count</h4>
<p>We’ll now implement the next feature:</p>
<ul>
<li>If the “Add Photo” button is ON, <strong>the number of available characters decreases by 23.</strong></li>
</ul>
<p>Currently, the number of available characters is displayed as follows in <code>render()</code>:</p>
<pre><code class="lang-javascript">&lt;span&gt;{<span class="hljs-number">280</span> - <span class="hljs-built_in">this</span>.state.text.length}&lt;/span&gt;
</code></pre>
<p>This value will now also depend on <code>this.state.photoAdded</code> so we need an <code>if</code> and <code>else</code> here.</p>
<p>However, <strong>in JSX, you can’t write</strong> <code>if</code> or <code>else</code> inside <code>{ ... }</code>. You could use a ternary expression (<code>a ? b : c</code>) like we did earlier but it would be pretty long and hard-to-read in this case.</p>
<p>Often the simplest solution in this situation is to refactor a conditional into a method. Let’s try it.</p>
<p><strong>First, modify the above code to use a class method, like this</strong>:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>{this.getRemainingChars()}<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
</code></pre>
<p><strong>And define the method like this:</strong></p>
<pre><code class="lang-js">getRemainingChars = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">let</span> chars = <span class="hljs-number">280</span> - <span class="hljs-built_in">this</span>.state.text.length;
  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>.state.photoAdded) chars = chars - <span class="hljs-number">23</span>;
  <span class="hljs-keyword">return</span> chars;
}
</code></pre>
<p>Now, the remaining character count should update as expected when the “Add Photo” button is toggled.</p>
<p><strong>Question</strong>: In <code>render()</code>, why does <code>{this.getRemainingChars()}</code>have <code>()</code> but <code>{this.handleChange}</code> and <code>{this.togglePhoto}</code> don’t?</p>
<p>Good question. Let’s take a look at <code>render()</code> again:</p>
<pre><code class="lang-js">render() {
  <span class="hljs-keyword">return</span> (
    ...
      &lt;textarea className=<span class="hljs-string">"..."</span> onChange={<span class="hljs-built_in">this</span>.handleChange}&gt;&lt;/textarea&gt;
    ...

    &lt;span&gt;{<span class="hljs-built_in">this</span>.getRemainingChars()}&lt;/span&gt;
    ...    

    &lt;button className=<span class="hljs-string">"..."</span> onClick={<span class="hljs-built_in">this</span>.togglePhoto}&gt;
      ...
    &lt;/button&gt;
    ...
    );
  }
</code></pre>
<p><strong>Answer</strong>:</p>
<ul>
<li><p>We’ve written the <code>getRemainingChars()</code> method to <strong>return a number</strong>. We need to get this number and put it inside <code>&lt;span&gt;&amp;</code>lt;/span&gt;, so <strong>we n</strong>eed <code>to call the getRema</code>iningChars() meth<code>od</code> by using (). That’s <code>wh</code>y we <code>have () in getRema</code>iningChars().</p>
</li>
<li><p>On the other hand, <code>handleChange</code> and <code>togglePhoto</code> are <strong>event handlers</strong>. We want these methods to be called only when the user interacts with the UI (typing in the <code>textarea</code> or clicking a <code>button</code>). To do so, we need to write them without <code>()</code> in <code>render()</code> and assign them to attributes like <code>onChange</code> and <code>onClick</code>. React will take care of attaching the methods to the relevant event listeners for us.</p>
</li>
</ul>
<h4 id="heading-the-tweet-buttons-status">The “Tweet” Button’s Status</h4>
<p>We have one more feature to implement:</p>
<ul>
<li>If the “Add Photo” button is ON, <strong>even if there’s no text entered, the “Tweet” button remains enabled.</strong></li>
</ul>
<p>This is actually really easy thanks to React. Previously, the Tweet button’s <code>disabled</code> option was set as:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">...</span> <span class="hljs-attr">disabled</span>=<span class="hljs-string">{this.state.text.length</span> === <span class="hljs-string">0}</span>&gt;</span>...<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
</code></pre>
<p>In other words, previously the “Tweet” button was disabled if the text’s length was 0. <strong>Now, the “Tweet” button is disabled if</strong>:</p>
<ul>
<li><p>The text’s length is 0</p>
</li>
<li><p><strong>AND</strong></p>
</li>
<li><p>The “Add Photo” button is OFF.</p>
</li>
</ul>
<p>So the logic looks like this:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">...</span> <span class="hljs-attr">disabled</span>=<span class="hljs-string">{this.state.text.length</span> === <span class="hljs-string">0</span> &amp;&amp; !<span class="hljs-attr">this.state.photoAdded</span>}&gt;</span>...<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
</code></pre>
<p>One way you can clarify the above code is by utilizing <code>getRemainingChars()</code>. If there are 280 characters remaining, that means that the <code>textarea</code> is empty and the “Add Photo” button is OFF so the “Tweet” button should be disabled.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">...</span> <span class="hljs-attr">disabled</span>=<span class="hljs-string">{this.getRemainingChars()</span> === <span class="hljs-string">280}</span>&gt;</span>...<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
</code></pre>
<p>This works but could break if, for example, you later refactor <code>getRemainingChars</code> so that it returns a string instead of a number. Instead, we can keep the previous logic and just move it to the top of the <code>render()</code>:</p>
<pre><code class="lang-js">render() {
    <span class="hljs-keyword">const</span> isTweetButtonDisabled = <span class="hljs-built_in">this</span>.state.text.length === <span class="hljs-number">0</span> &amp;&amp; !<span class="hljs-built_in">this</span>.state.photoAdded;

    <span class="hljs-keyword">return</span> (
      ...
        &lt;button className=<span class="hljs-string">"..."</span> disabled={isTweetButtonDisabled}&gt;Tweet&lt;/button&gt;
      ...
    );
  }
</code></pre>
<p>That’s it! Try toggling the “Add Photo” button and check that the “Tweet Button” is enabled/disabled correctly.</p>
<h4 id="heading-were-done">We’re Done!</h4>
<p>Here’s the resulting Pen:</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/roZXvE" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<h3 id="heading-step-13-reflection-on-the-react-code-why-so-simple-5-minutes">Step 13: Reflection on the React Code — Why So Simple? (5 minutes)</h3>
<p>The changes to accommodate the “Add Photo” button were minimal when using React. No refactor needed. Why is this the case?</p>
<p>Again, it has to do with React’s style of writing UI code. In React, event handlers modify the <code>state</code>, and whenever the state is modified, <strong>React automatically calls</strong> <code>render()</code> again to update the UI.</p>
<p>In this particular example, the diagram now looks like this:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*DN_-X4stP1U7E6-h" alt="Image" width="717" height="353" loading="lazy"></p>
<p>The <code>state</code> becomes an intermediary thing which sits in between the event handlers and <code>render()</code>:</p>
<ul>
<li><p>Event handlers don’t need to worry about which part of the DOM changes. They just need to set the <code>state</code>.</p>
</li>
<li><p>Similarly, when you write <code>render()</code>, all you need to worry about is what the current <code>state</code> is.</p>
</li>
</ul>
<h3 id="heading-compare-with-jquery">Compare with jQuery</h3>
<p>You can imagine what would happen as the UI gets more features. Without the intermediary state, we’d have a tough time managing complexity. This is why you’d want to use React over jQuery for complex UIs.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*N2IvA6TZIvC-T155" alt="Image" width="717" height="666" loading="lazy"></p>
<p>Again, <strong>it’s possible</strong> to write clean jQuery code that doesn’t look like spaghetti code. <strong>But you have to come up with the code structure yourself</strong> and think about how to refactor every time you add a new feature. React provides you this structure and reduces your cognitive load.</p>
<blockquote>
<p>Note that the idea of separating the state from the rendering wasn’t invented with React. We’re just looking at it from the perspective of React.</p>
</blockquote>
<h3 id="heading-step-14-the-final-feature-highlighting-overflown-characters-5-minutes">Step 14: The Final Feature — Highlighting Overflown Characters (5 minutes)</h3>
<p>The last feature we’re going to implement is <strong>highlighting characters that are over the limit</strong>.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*C1OJuGMg8agMuugu" alt="Image" width="800" height="224" loading="lazy"></p>
<p>Unfortunately, <strong>we’re not going to highlight the actual text inside the Tweet box</strong> because that would require us to change <code>&lt;textar</code>ea&amp;<code>gt; to&lt;div contenteditabl</code>e="tr<code>ue"&gt; and con</code>tenteditable is a bit too complicated for illustrative purposes.</p>
<p>Instead, <strong>we’ll be displaying a Bootstrap alert box</strong> on top and indicate which characters need to be deleted, like this:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*3Lnj6RRDfqMk1c8l" alt="Image" width="800" height="223" loading="lazy"></p>
<p><strong>To try it out, copy the following quote from the official React documentation:</strong></p>
<blockquote>
<p>React embraces the fact that rendering logic is inherently coupled with other UI logic: how events are handled, how the state changes over time, and how the data is prepared for display.</p>
<p>Instead of artificially separating technologies by putting markup and logic in separate files, React separates concerns with loosely coupled units called “components” that contain both.</p>
</blockquote>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/bOmdWZ" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<ul>
<li><p>It should show an alert box with the overflow characters highlighted in red.</p>
</li>
<li><p>It should also show 10 characters before the cutoff point, without any highlighting.</p>
</li>
</ul>
<p>If we were to implement this in jQuery, our code would become a lot messier. Notice in the diagram that we’ll be adding two more arrows for one new feature.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*5IJpVy_aq3SfO9Lq" alt="Image" width="601" height="391" loading="lazy"></p>
<p><strong>So we’re not going to implement this in jQuery</strong>. We’ll just do it with React and call it a day. It’ll be pretty simple to do with React — just one extra arrow in the diagram:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*2lMKrwLX5WA-oUpb" alt="Image" width="717" height="353" loading="lazy"></p>
<h3 id="heading-step-15-highlighting-overflow-characters-with-react-1015-minutes">Step 15: Highlighting Overflow Characters with React (10–15 minutes)</h3>
<p>Let’s start with our previous React implementation.</p>
<p><strong>✔ Fork the Pen below</strong> to get started.</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/roZXvE" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<p>We’ll do this step by step. First, <strong>we’ll display a simple alert with static text when you write past the limit.</strong></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*ap1sBT75lN-f5hVD" alt="Image" width="800" height="191" loading="lazy"></p>
<p>Since this will require a conditional, let’s write it in a separate method. <strong>Add</strong> <code>{this.renderOverflowAlert()}</code> above the <code>textarea</code>:</p>
<pre><code class="lang-js">{<span class="hljs-built_in">this</span>.renderOverflowAlert()}
&lt;textarea ...&gt;&lt;/textarea&gt;
</code></pre>
<p>Now, this method should return:</p>
<ul>
<li><p><strong>A div tag</strong> for the alert box if there are no more characters left.</p>
</li>
<li><p><strong>Nothing</strong> (i.e. empty string or NULL) otherwise.</p>
</li>
</ul>
<p>It turns out that in React, <strong>you can return JSX markup from a method and use this in any other method</strong>, everything will just work. In other words, you can do something like:</p>
<pre><code class="lang-js">someMethod = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span>Hello World<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span></span>
  );
}
anotherMethod = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>
      {this.someMethod()}
    <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>
  );
}
</code></pre>
<p>In <code>renderOverflowAlert</code>, we can return <code>( &lt;div&gt; ... &lt;/div&gt; )</code> in one case and nothing in <strong>the other. So our</strong> <code>renderOverflowAlert</code> method will look like this:</p>
<pre><code class="lang-js">renderOverflowAlert = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>.getRemainingChars() &lt; <span class="hljs-number">0</span>) {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"alert alert-warning text-left"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">strong</span>&gt;</span>Oops! Too Long:<span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
  <span class="hljs-keyword">return</span> <span class="hljs-string">''</span>;
};
</code></pre>
<p>Notice that we’re checking <code>this.getRemainingChars()</code> to see if we need to show the alert or not.</p>
<p><strong>Try this out by typing 280+ characters (or 257+ characters with the “Add Photo” button ON).</strong> The alert should appear just as the character limit reaches -1.</p>
<h4 id="heading-displaying-overflown-characters">Displaying Overflown Characters</h4>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*9IQGNgFJtO4Stlpk" alt="Image" width="800" height="264" loading="lazy"></p>
<p><em>(This should say 280 instead of 140 characters.)</em></p>
<p>Here’s a breakdown of the logic we want for the alert message:</p>
<ul>
<li><p>Between “Oops! Too Long:” and the actual text, there’s an empty single space followed by three dots. I used here because when writing markup in JSX, white spaces between tags get removed. (You can add them manually using <code>{' '}</code>.)</p>
</li>
<li><p>Then there are the 271st~280th (total of 10) characters of <code>this.state.text</code>.</p>
</li>
<li><p>Then there are the remaining characters highlighted in red.</p>
</li>
</ul>
<p><strong>Let’s write this in JSX. Inside the</strong> <code>if</code> clause of <code>overflowAlert</code>, we’ll create two variables: <code>beforeOverflowText</code> and <code>overflowText</code>. We’ll use <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring">the substring method</a> on <code>this.state.text</code>.</p>
<pre><code class="lang-js">renderOverflowAlert = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>.getRemainingChars() &lt; <span class="hljs-number">0</span>) {
    <span class="hljs-keyword">const</span> beforeOverflowText = <span class="hljs-built_in">this</span>.state.text.substring(<span class="hljs-number">280</span> - <span class="hljs-number">10</span>, <span class="hljs-number">280</span>);
    <span class="hljs-keyword">const</span> overflowText = <span class="hljs-built_in">this</span>.state.text.substring(<span class="hljs-number">280</span>);
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"alert alert-warning text-left"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">strong</span>&gt;</span>Oops! Too Long:<span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span>
        <span class="hljs-symbol">&amp;nbsp;</span> <span class="hljs-symbol">&amp;#8230;</span>
        {beforeOverflowText}
        <span class="hljs-tag">&lt;<span class="hljs-name">strong</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"bg-danger text-light"</span>&gt;</span>{overflowText}<span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
  <span class="hljs-keyword">return</span> <span class="hljs-string">''</span>;
};
</code></pre>
<ul>
<li><p>If you do <code>.substring(a, b)</code>, it will return the <code>(a + 1)-nth</code> until the <code>b-th</code> characters from the string.</p>
</li>
<li><p>If you do <code>.substring(a)</code>, it will return the <code>(a + 1)-nth</code> until the last characters from the string.</p>
</li>
<li><p>We use Bootstrap’s <code>bg-danger</code> class to highlight the text in red and <code>text-light</code> to make the text readable against the now-dark background.</p>
</li>
</ul>
<p>Copy paste the following text again and check that the part of the text after the first 280 characters is highlighted. We’re almost done!</p>
<blockquote>
<p>React embraces the fact that rendering logic is inherently coupled with other UI logic: how events are handled, how the state changes over time, and how the data is prepared for display.</p>
<p>Instead of artificially separating technologies by putting markup and logic in separate files, React separates concerns with loosely coupled units called “components” that contain both.</p>
</blockquote>
<h4 id="heading-what-if-the-add-photo-button-is-on">What if the “Add Photo” button is ON?</h4>
<p>If the “Add Photo” button is ON then the character limit decreases by 23. <strong>So our</strong> <code>beforeOverflowText</code> and <code>overflowText</code> need to take that into account:</p>
<pre><code class="lang-js">renderOverflowAlert = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>.getRemainingChars() &lt; <span class="hljs-number">0</span>) {
      <span class="hljs-keyword">const</span> imageLength = <span class="hljs-built_in">this</span>.state.photoAdded ? <span class="hljs-number">23</span> : <span class="hljs-number">0</span>;
      <span class="hljs-keyword">const</span> beforeOverflowText = <span class="hljs-built_in">this</span>.state.text.substring(
        <span class="hljs-number">280</span> - imageLength - <span class="hljs-number">10</span>,
        <span class="hljs-number">280</span> - imageLength,
      );
      <span class="hljs-keyword">const</span> overflowText = <span class="hljs-built_in">this</span>.state.text.substring(<span class="hljs-number">280</span> - imageLength);
      <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"alert alert-warning text-left"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">strong</span>&gt;</span>Oops! Too Long:<span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span>
          <span class="hljs-symbol">&amp;nbsp;</span> <span class="hljs-symbol">&amp;#8230;</span>
          {beforeOverflowText}
          <span class="hljs-tag">&lt;<span class="hljs-name">strong</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"bg-danger text-light"</span>&gt;</span>{overflowText}<span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
      );
    }
    <span class="hljs-keyword">return</span> <span class="hljs-string">''</span>;
  };
</code></pre>
<p>Now, try toggling the “Add Photo” button while entering any text that’s longer than the limit. It should work as expected. Here’s the Pen:</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/julienben/embed/bOmdWZ" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p> </p>
<p>That’s it! Again, you can see that the code changes were simple:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/0*RJ-PiBFsvcPqqQCc" alt="Image" width="717" height="353" loading="lazy"></p>
<h3 id="heading-step-16-whats-next-5-minutes">Step 16: What’s Next? (5 minutes)</h3>
<p>This concludes the tutorial. Hopefully you:</p>
<ul>
<li><p>understood the advantages of React’s component structure vs manually modifying the DOM with jQuery, and</p>
</li>
<li><p>learned how to write simple React components using JavaScript and JSX.</p>
</li>
</ul>
<h4 id="heading-whats-next"><strong>What’s next?</strong></h4>
<p>There are many ways to go from here.</p>
<p>One possibility would be to take a look at this short article called <a target="_blank" href="https://medium.freecodecamp.org/learning-react-roadmap-from-scratch-to-advanced-bff7735531b6">How to Learn React — A roadmap from beginner to advanced</a>. It can help you decide how to best continue learning React.</p>
<p>I also highly recommend reading the following portions the official React documentation:</p>
<ul>
<li><p><a target="_blank" href="https://reactjs.org/docs/getting-started.html">Getting started</a> which includes the React team’s recommended learning resources, and</p>
</li>
<li><p><a target="_blank" href="https://reactjs.org/docs/thinking-in-react.html">Thinking in React</a> which will help you understand how to think about building components and applications with React.</p>
</li>
</ul>
<p>Before you leave though, I have an <strong>optional challenge</strong> for you!</p>
<p>If you feel comfortable enough with React already and want to write your own code, <strong>try to move</strong> <code>remainingChars</code> to the component’s <code>state</code>. Make sure it gets updated where necessary and use it in all the relevant places.</p>
<p><strong>Feel free to post the result as a Pen in the comments and I’ll be very happy to check it out!</strong></p>
<h4 id="heading-thanks">Thanks</h4>
<p>Thanks a lot for reading this far! And above all thanks to <a target="_blank" href="https://twitter.com/chibicode">@chibicode</a> for the huge amount of work he put into the first version of this tutorial! I hope this updated version does it justice.</p>
<p>I’m Julien. I work as a frontend engineer at <a target="_blank" href="https://healthy.io">Healthy.io</a> and I help maintain <a target="_blank" href="https://github.com/react-boilerplate/react-boilerplate">react-boilerplate</a> on GitHub. If you catch a mistake, want any clarifications or think I skipped something important, please let me know and I’ll make sure to fix it.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to create a login feature with Bootstrap Modal and jQuery AJAX ]]>
                </title>
                <description>
                    <![CDATA[ By Yogi Bootstrap Modal is an excellent way to create a Login form on your website. In this tutorial, you will learn how to create a login feature using Bootstrap for an ASP.NET website. The login Check feature will be created using jQuery AJAX. I wi... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-create-a-login-feature-with-bootstrap-modal-and-jquery-ajax-53dc0d281609/</link>
                <guid isPermaLink="false">66c350f5d4303af013602a2e</guid>
                
                    <category>
                        <![CDATA[ Bootstrap ]]>
                    </category>
                
                    <category>
                        <![CDATA[ coding ]]>
                    </category>
                
                    <category>
                        <![CDATA[ jQuery ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 03 Dec 2018 21:44:14 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*QQsDn3sXpsL06kzzS7a_oA.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Yogi</p>
<p><strong>Bootstrap Modal</strong> is an excellent way to create a <strong>Login form</strong> on your website. In this tutorial, you will learn how to create a login feature using Bootstrap for an ASP.NET website. The login Check feature will be created using jQuery AJAX.</p>
<p><strong>I will create the following features Step by Step:</strong></p>
<ol>
<li><p>A Bootstrap Modal that will contain a login form.</p>
</li>
<li><p>The Login Form will contain 2 fields, ‘Username’ &amp; ‘Password’. The user has to enter their values in these fields.</p>
</li>
<li><p>On clicking the submit button on the form, the user’s input (username and password) will be sent to the C# function.</p>
</li>
<li><p>This C# function will check whether the username and password are correct or not.</p>
</li>
<li><p>If they are correct then the user is redirected to the profile page.</p>
</li>
</ol>
<h4 id="heading-you-can-check-out-the-working-demo-herehttpwwwdemoyogihostingcomebootstrap-modal-login-form"><a target="_blank" href="http://www.demo.yogihosting.com/e/bootstrap-modal-login-form/">You can check out the working DEMO here</a>.</h4>
<h4 id="heading-creating-a-bootstrap-modal-with-a-login-form"><strong>Creating a Bootstrap Modal with a Login Form</strong></h4>
<p>Add the reference of “bootstrap CSS, jQuery and bootstrap js” files on the page head.</p>
<pre><code>&lt;link rel=<span class="hljs-string">"stylesheet"</span> href=<span class="hljs-string">"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"</span>&gt;
</code></pre><pre><code>&lt;script src=<span class="hljs-string">"https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"</span>&gt;&lt;/script&gt;
</code></pre><pre><code>&lt;script src=<span class="hljs-string">"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"</span>&gt;&lt;/script&gt;
</code></pre><p>Next Create a Bootstrap Modal that contains the login form:</p>
<pre><code>&lt;div <span class="hljs-class"><span class="hljs-keyword">class</span></span>=<span class="hljs-string">"container"</span>&gt;<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"validCredential"</span>&gt;</span></span>
</code></pre><pre><code>&lt;h3&gt;Try any one <span class="hljs-keyword">of</span> the following three:&lt;<span class="hljs-regexp">/h3&gt;&lt;div&gt;&lt;p&gt;  1. Username: Ram&lt;/</span>p&gt;<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>  Password: admin<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>&lt;/div&gt;
</code></pre><pre><code>&lt;div&gt;<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>  2. Username: Shiv<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span><span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>  Password: admin<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>&lt;/div&gt;
</code></pre><pre><code>&lt;div&gt;<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>  3. Username: Krishna<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>
</code></pre><pre><code>&lt;p&gt;  Password: admin&lt;<span class="hljs-regexp">/p&gt;&lt;/</span>div&gt;
</code></pre><pre><code>&lt;/div&gt;
</code></pre><pre><code>&lt;!-- The button that triggers the Modal --&gt;<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"button"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn btn-info btn-lg"</span> <span class="hljs-attr">data-toggle</span>=<span class="hljs-string">"modal"</span> <span class="hljs-attr">data-target</span>=<span class="hljs-string">"#myModal"</span>&gt;</span>Open Modal<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>
</code></pre><pre><code>&lt;!-- Bootstrap Modal --&gt;&lt;div id="myModal" class="modal fade" role="dialog"&gt;  &lt;div class="modal-dialog"&gt;
</code></pre><pre><code>    &lt;!-- Modal content--&gt;    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"modal-content"</span>&gt;</span>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"modal-header"</span>&gt;</span>        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"button"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"close"</span> <span class="hljs-attr">data-dismiss</span>=<span class="hljs-string">"modal"</span>&gt;</span>×<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>        <span class="hljs-tag">&lt;<span class="hljs-name">h4</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"modal-title"</span>&gt;</span>Log in with your Username<span class="hljs-tag">&lt;/<span class="hljs-name">h4</span>&gt;</span>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
</code></pre><pre><code>      &lt;div <span class="hljs-class"><span class="hljs-keyword">class</span></span>=<span class="hljs-string">"modal-body"</span>&gt;        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">table</span>&gt;</span>          <span class="hljs-tag">&lt;<span class="hljs-name">tbody</span>&gt;</span>            <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>                <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"userNameTextBox"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Username"</span> /&gt;</span>              <span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>            <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>            <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>                <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"userNamSpan"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>              <span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>            <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>            <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>                <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"passwordTextBox"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Password"</span> /&gt;</span>              <span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>            <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>            <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>                <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"passwordSpan"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>              <span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>            <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>            <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>                <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"button"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"submitButton"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"Login"</span> /&gt;</span>              <span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>            <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>            <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>                <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"messageSpan"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>              <span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>            <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>            <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"loadingImg"</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"loading.gif"</span> /&gt;</span>              <span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>            <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>          <span class="hljs-tag">&lt;/<span class="hljs-name">tbody</span>&gt;</span>        <span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span></span>      &lt;/div&gt;
</code></pre><pre><code>    &lt;div <span class="hljs-class"><span class="hljs-keyword">class</span></span>=<span class="hljs-string">"modal-footer"</span>&gt;      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"button"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn btn-default"</span> <span class="hljs-attr">data-dismiss</span>=<span class="hljs-string">"modal"</span>&gt;</span>Close<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>    &lt;<span class="hljs-regexp">/div&gt;  &lt;/</span>div&gt;&lt;!-- END Modal content--&gt;&lt;/div&gt;
</code></pre><pre><code>&lt;/div&gt;
</code></pre><pre><code>&lt;!-- END Bootstrap Modal --&gt;&lt;/div&gt;
</code></pre><p>This is how the bootstrap modal login form will look.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/DNem9yh40WOT2fNUPM33t9YynO7yWzqoiFsC" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Bootstrap Modal Login Form</strong></em></p>
<h4 id="heading-adding-the-jquery-code-on-the-button-click-event"><strong>Adding the jQuery Code on the button click event</strong></h4>
<p>In the button click, I will force users to enter some value to the username and password fields, before they submit the form.</p>
<p>When both the textboxes contain some value, only then I will be calling the C# function using the <a target="_blank" href="http://www.yogihosting.com/jquery-ajax/">jQuery AJAX method</a>. With this method, I will be able to pass the values of the 2 text boxes (username and password) to my C# function.</p>
<p>Add the below jQuery code to your page:</p>
<pre><code>$(<span class="hljs-string">"#submitButton"</span>).click(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">e</span>) </span>{
</code></pre><pre><code><span class="hljs-keyword">if</span> ($(<span class="hljs-string">"#userNameTextBox"</span>).val() == <span class="hljs-string">""</span>)
</code></pre><pre><code>$(<span class="hljs-string">"#userNamSpan"</span>).text(<span class="hljs-string">"Enter Username"</span>);
</code></pre><pre><code><span class="hljs-keyword">else</span>
</code></pre><pre><code>$(<span class="hljs-string">"#userNamSpan"</span>).text(<span class="hljs-string">""</span>);
</code></pre><pre><code><span class="hljs-keyword">if</span> ($(<span class="hljs-string">"#passwordTextBox"</span>).val() == <span class="hljs-string">""</span>)
</code></pre><pre><code>$(<span class="hljs-string">"#passwordSpan"</span>).text(<span class="hljs-string">"Enter Password"</span>);
</code></pre><pre><code><span class="hljs-keyword">else</span>
</code></pre><pre><code>$(<span class="hljs-string">"#passwordSpan"</span>).text(<span class="hljs-string">""</span>);
</code></pre><pre><code><span class="hljs-keyword">if</span> (($(<span class="hljs-string">"#userNameTextBox"</span>).val() != <span class="hljs-string">""</span>) &amp;&amp; ($(<span class="hljs-string">"#passwordTextBox"</span>).val() != <span class="hljs-string">""</span>))
</code></pre><pre><code>$.ajax({  <span class="hljs-attr">type</span>: <span class="hljs-string">"POST"</span>,  <span class="hljs-attr">url</span>: <span class="hljs-string">"index.aspx/login"</span>,  <span class="hljs-attr">contentType</span>: <span class="hljs-string">"application/json; charset=utf-8"</span>,  <span class="hljs-attr">data</span>: <span class="hljs-string">'{"username":"'</span> + $(<span class="hljs-string">"#userNameTextBox"</span>).val() + <span class="hljs-string">'","password":"'</span> + $(<span class="hljs-string">"#passwordTextBox"</span>).val() + <span class="hljs-string">'"}'</span>,  <span class="hljs-attr">dataType</span>: <span class="hljs-string">"json"</span>,  <span class="hljs-attr">success</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">result, status, xhr</span>) </span>{    <span class="hljs-keyword">if</span> (result.d == <span class="hljs-string">"Success"</span>) {      $(<span class="hljs-string">"#messageSpan"</span>).text(<span class="hljs-string">"Login Successful, Redireting to your profile page."</span>);      <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{ <span class="hljs-built_in">window</span>.location = <span class="hljs-string">"profile.aspx"</span>; }, <span class="hljs-number">2000</span>);    }    <span class="hljs-keyword">else</span>      $(<span class="hljs-string">"#messageSpan"</span>).text(<span class="hljs-string">"Login failed, Please try again."</span>);    },   <span class="hljs-attr">error</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">xhr, status, error</span>) </span>{     $(<span class="hljs-string">"#dbData"</span>).html(<span class="hljs-string">"Result: "</span> + status + <span class="hljs-string">" "</span> + error + <span class="hljs-string">" "</span> + xhr.status + <span class="hljs-string">" "</span> + xhr.statusText)   }});
</code></pre><pre><code>});
</code></pre><pre><code>$(<span class="hljs-built_in">document</span>).ajaxStart(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{  $(<span class="hljs-string">"#loadingImg"</span>).show();});
</code></pre><pre><code>$(<span class="hljs-built_in">document</span>).ajaxStop(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{  $(<span class="hljs-string">"#loadingImg"</span>).hide();});
</code></pre><p>In the success callback method, you can see that I am redirecting the user to the <strong>profile.aspx</strong> page if-and-only-if I receive the “<strong>Success</strong>” message.</p>
<p>The <strong>setTimeout()</strong> is a JavaScript function that will redirect to the profile page in 2 seconds.</p>
<p><strong>The following 2 images will explain the login feature:</strong></p>
<blockquote>
<p><em>1. When login fails.</em></p>
</blockquote>
<p><img src="https://cdn-media-1.freecodecamp.org/images/pEzx51hKTGLE3OTi76TdDiLQQKCiHN4VS8EZ" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Login Failed for wrong Username and Password</strong></em></p>
<blockquote>
<p><em>2. When login is successful.</em></p>
</blockquote>
<p><img src="https://cdn-media-1.freecodecamp.org/images/bttqLY1d9BT7-UNRPN-lrYCIxgXHCqVmVV0H" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Login Successful when Username and Password are correct</strong></em></p>
<h4 id="heading-the-c-code"><strong>The C# code:</strong></h4>
<p>Now in your <strong>.aspx.cs</strong> page, add the following code:</p>
<pre><code>[System.Web.Services.WebMethod]
</code></pre><pre><code>public <span class="hljs-keyword">static</span> string login(string username, string password)
</code></pre><pre><code>{
</code></pre><pre><code><span class="hljs-keyword">var</span> cred = LoadCredential();
</code></pre><pre><code><span class="hljs-keyword">var</span> count = (<span class="hljs-keyword">from</span> t <span class="hljs-keyword">in</span> cred
</code></pre><pre><code>where t.username == username &amp;&amp; t.password == password
</code></pre><pre><code>select t).Count();
</code></pre><pre><code><span class="hljs-keyword">if</span> (count == <span class="hljs-number">1</span>)
</code></pre><pre><code>{
</code></pre><pre><code>HttpContext.Current.Session[<span class="hljs-string">"User"</span>] = username;
</code></pre><pre><code><span class="hljs-keyword">return</span> <span class="hljs-string">"Success"</span>;
</code></pre><pre><code>}
</code></pre><pre><code><span class="hljs-keyword">else</span>
</code></pre><pre><code><span class="hljs-keyword">return</span> <span class="hljs-string">"Failed"</span>;
</code></pre><pre><code>}
</code></pre><pre><code><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Credential</span></span>
</code></pre><pre><code>{
</code></pre><pre><code>public string username { get; set; }
</code></pre><pre><code>public string password { get; set; }
</code></pre><pre><code>}
</code></pre><pre><code><span class="hljs-keyword">static</span> List&lt;Credential&gt; LoadCredential()
</code></pre><pre><code>{
</code></pre><pre><code>List&lt;Credential&gt; credList = <span class="hljs-keyword">new</span> List&lt;Credential&gt;();
</code></pre><pre><code>Credential cred = <span class="hljs-keyword">new</span> Credential();
</code></pre><pre><code>cred.username = <span class="hljs-string">"Ram"</span>;
</code></pre><pre><code>cred.password = <span class="hljs-string">"admin"</span>;
</code></pre><pre><code>credList.Add(cred);
</code></pre><pre><code>cred = <span class="hljs-keyword">new</span> Credential();
</code></pre><pre><code>cred.username = <span class="hljs-string">"Shiv"</span>;
</code></pre><pre><code>cred.password = <span class="hljs-string">"admin"</span>;
</code></pre><pre><code>credList.Add(cred);
</code></pre><pre><code>cred = <span class="hljs-keyword">new</span> Credential();
</code></pre><pre><code>cred.username = <span class="hljs-string">"Krishna"</span>;
</code></pre><pre><code>cred.password = <span class="hljs-string">"admin"</span>;
</code></pre><pre><code>credList.Add(cred);
</code></pre><pre><code><span class="hljs-keyword">return</span> credList;
</code></pre><pre><code>}
</code></pre><p>The login() function is the one that is called by the <strong>jQuery</strong> method. It checks if the <em>username and passwords are correct</em> and then returns the appropriate message.</p>
<h4 id="heading-css"><strong>CSS</strong></h4>
<p>To style the login form and the bootstrap modal so that they look perfect, add the following CSS to your page:</p>
<pre><code>.btn {
</code></pre><pre><code>margin: <span class="hljs-number">15</span>px <span class="hljs-number">0</span>;
</code></pre><pre><code>}
</code></pre><pre><code>#loadingImg {
</code></pre><pre><code>display: none;
</code></pre><pre><code>position: absolute;
</code></pre><pre><code>margin: auto;
</code></pre><pre><code>top: <span class="hljs-number">0</span>;
</code></pre><pre><code>left: <span class="hljs-number">0</span>;
</code></pre><pre><code>right: <span class="hljs-number">0</span>;
</code></pre><pre><code>bottom: <span class="hljs-number">0</span>;
</code></pre><pre><code>}
</code></pre><pre><code>.validCredential h3 {
</code></pre><pre><code>float: left;
</code></pre><pre><code>text-decoration: underline;
</code></pre><pre><code>}
</code></pre><pre><code>.validCredential div {
</code></pre><pre><code>clear: both;
</code></pre><pre><code>}
</code></pre><pre><code>.validCredential p {
</code></pre><pre><code>float: left;
</code></pre><pre><code>padding-right: <span class="hljs-number">10</span>px;
</code></pre><pre><code>}
</code></pre><pre><code>::-webkit-input-placeholder {
</code></pre><pre><code>color: #ccc;
</code></pre><pre><code>}
</code></pre><pre><code>#myModal {
</code></pre><pre><code>color: #<span class="hljs-number">1</span>fa67b;
</code></pre><pre><code>}
</code></pre><pre><code>#myModal table {
</code></pre><pre><code>position: relative;
</code></pre><pre><code>margin: auto;
</code></pre><pre><code>}
</code></pre><pre><code>#myModal table input {
</code></pre><pre><code>border-radius: <span class="hljs-number">5</span>px;
</code></pre><pre><code>border: solid <span class="hljs-number">1</span>px #CCC;
</code></pre><pre><code>margin: <span class="hljs-number">10</span>px;
</code></pre><pre><code>padding: <span class="hljs-number">3</span>px <span class="hljs-number">10</span>px;
</code></pre><pre><code>color: #<span class="hljs-number">000</span>;
</code></pre><pre><code>}
</code></pre><pre><code>#myModal table input[type=<span class="hljs-string">"button"</span>] {
</code></pre><pre><code>width: <span class="hljs-number">94</span>%;
</code></pre><pre><code>background: #<span class="hljs-number">1</span>fa67b;
</code></pre><pre><code>color: #FFF;
</code></pre><pre><code>}
</code></pre><pre><code>#myModal table span {
</code></pre><pre><code>float: left;
</code></pre><pre><code>font-size: <span class="hljs-number">12</span>px;
</code></pre><pre><code>color: #f00;
</code></pre><pre><code>padding-left: <span class="hljs-number">23</span>px;
</code></pre><pre><code>}
</code></pre><h4 id="heading-profile-page"><strong>Profile Page</strong></h4>
<p><img src="https://cdn-media-1.freecodecamp.org/images/DnAL6MlCl08x4PU4NDj9YKcfGhsYAoJnnsra" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Welcome Message on the Profile Page</strong></em></p>
<p>On the profile page, the user will get the welcome message. The code of the profile page is the following:</p>
<pre><code>&lt;h1 id=<span class="hljs-string">"welcomeMessage"</span> runat=<span class="hljs-string">"server"</span>&gt;&lt;/h1&gt;
</code></pre><pre><code><span class="hljs-keyword">if</span> (!IsPostBack){  welcomeMessage.InnerHtml = <span class="hljs-string">"Welcome "</span> + Session[<span class="hljs-string">"User"</span>] + <span class="hljs-string">" to the profile page."</span>;}
</code></pre><blockquote>
<p><strong><em>Check out the working demo by clicking the below link:</em></strong></p>
</blockquote>
<h4 id="heading-working-demohttpwwwdemoyogihostingcomebootstrap-modal-login-form"><a target="_blank" href="http://www.demo.yogihosting.com/e/bootstrap-modal-login-form/">Working DEMO</a></h4>
<h3 id="heading-conclusion"><strong>Conclusion</strong></h3>
<p>I hope you liked this tutorial. Feel free to contact me for any questions. I will be there to help if you need it. If you liked this tutorial, then kindly share it on your social accounts.</p>
<p><strong>I have also published another tutorial on freeCodeCamp, you would like to see it too — <a target="_blank" href="https://medium.freecodecamp.org/master-the-art-of-looping-in-javascript-with-these-incredible-tricks-a5da1aa1d6c5">Master the art of looping in JavaScript with these incredible tricks</a></strong></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ A Learning Path for Newbies in WordPress Development ]]>
                </title>
                <description>
                    <![CDATA[ By Ihtisham Zahoor In this information age, one obstacle in learning something new is not where to find the resources. It’s from where to start and make sense of all the resources available online these days. This holds especially true in web develop... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/learning-path-for-newbies-in-wordpress-development-a283981adf53/</link>
                <guid isPermaLink="false">66d45f32b6b7f664236cbdcc</guid>
                
                    <category>
                        <![CDATA[ Bootstrap ]]>
                    </category>
                
                    <category>
                        <![CDATA[ CSS3 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Design ]]>
                    </category>
                
                    <category>
                        <![CDATA[ HTML5 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ jQuery ]]>
                    </category>
                
                    <category>
                        <![CDATA[ js ]]>
                    </category>
                
                    <category>
                        <![CDATA[ learning ]]>
                    </category>
                
                    <category>
                        <![CDATA[ PHP ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Sass ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ WordPress ]]>
                    </category>
                
                    <category>
                        <![CDATA[ wordpress plugins ]]>
                    </category>
                
                    <category>
                        <![CDATA[ WordPress Theme ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 12 Sep 2018 18:46:58 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/0*RlFuY9MLjBJDt1U5" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Ihtisham Zahoor</p>
<p>In this information age, one obstacle in learning something new is not where to find the resources. It’s from where to start and make sense of all the resources available online these days. This holds especially true in web development when new tools are popping up overnight.</p>
<p>Back in 2015, since I first decided to learn web development, I have faced challenges of self-doubt and lack of motivation. But the one challenge which drained my energies was not knowing what path to follow.</p>
<p>Considering the initial challenges in this path I have written this piece for technology enthusiasts who want to get their hands dirty in web development. Especially WordPress development. All in the hope you are going to spend your time building something cool than getting into this never-ending vicious cycle of learning one tool to another.</p>
<h1 id="heading-caution-a-lot-of-learning-in-this-path"><strong>Caution! A lot of learning in this path</strong></h1>
<p>I am a WordPress developer (although my impostor syndrome tells me otherwise). I can share my experience only related to WordPress. But before diving into details, let’s examine a Q/A session first.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/NXiCcxwi7e3plLZMWr3KAfpSuCMA1KA5ODNe" alt="Image" width="700" height="418" loading="lazy"></p>
<p>Now, is the answer mean? Maybe. Is it true? Absolutely! WordPress and Web Development, in general, are as serious and demanding as any profession. So be ready for…</p>
<ul>
<li>Lifetime of Learning</li>
<li>Pulling all-nighters occasionally</li>
</ul>
<p>All right, have you read and understood the above points? Great. Now relax as it is not to discourage you or anything. It’s just how the life of a developer is as the ever-changing nature of this field requires.</p>
<p>The good news is there are not many web technologies you have to learn to master the craft of WordPress Development. That being said, give your time to this amazing piece of software, and WordPress will adore you in return.</p>
<p>WordPress will change your life if you let it — Chris Lema</p>
<p>It’s all inspiring and cute, right? It is one of the reasons <a target="_blank" href="https://topher1kenobe.com/">Topher DeRosia</a> (huge props) has provided the <a target="_blank" href="https://heropress.com/">HeroPress</a> platform for WordPressers (yes, this is what we proudly call ourselves). Here WordPressers share regularly their WordPress origin stories with the <a target="_blank" href="https://opensource.org/community">community</a>. Rest assured while reading through the stories you are going to feel adrenaline rush through your body as many of you are going to find yourself related to those stories.</p>
<h1 id="heading-wordpress-for-everyone">WordPress for Everyone!</h1>
<p>WordPress is super easy to use. In fact, anyone without any technical knowledge, can set up and start using WordPress in no time. However, WordPress has a vast ecosystem. I, for one, would categorize WordPress development being carried out in two different domains. I’ll first list those domains below and then I will discuss each one separately.</p>
<ul>
<li>WordPress Site Customization</li>
<li>WordPress Themes/Plugins Development</li>
</ul>
<h1 id="heading-wordpress-site-customization">WordPress Site Customization</h1>
<p>WordPress Site Customization is where all the buzz is about, as it amounts for most of the work done with respect to WordPress usage. Site Customization is that domain where no prior coding knowledge is required.</p>
<p>That is to say, a WordPress power user can easily perform tasks like Theme/Plugin setup and customization using page builders, site debugging, and site maintenance, etc. Moreover, Theme customization is all about customizing or modifying a pre-made theme as per the requirements of your client.</p>
<p>Considering that, and to give you a perspective about the process, you can watch the following short tutorial by Tyler Moore. In his tutorial, he builds a new website by customizing a pre-made theme using the Elementor page builder.</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/8AZ8GqW5iak" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h1 id="heading-wordpress-themesplugins-development">WordPress Themes/Plugins Development</h1>
<p>It is where all the fun is for WordPress geeks. We can call it the <em>actual</em> WordPress development, as this domain is all about knowing and getting into the nitty-gritty technical details of programming. It can be as simple as using a few WordPress hooks to modify responses or as complex as building a full-fledge product on WordPress.</p>
<p>So, in case you want to start into this domain, you have to invest more time learning about web technologies involved in WordPress development. Considering that, the rest of this article will discuss all the technologies and a few tips and tricks to get you started in <em>actual</em> WordPress development.</p>
<h1 id="heading-learning-path"><strong>Learning Path</strong></h1>
<p>To get started in your WordPress Development journey, these tools and technologies are what you are going to be working with.</p>
<ol>
<li><p>Say hello and embrace g̵o̵o̵d̵ ̵o̵̵̵l̵̵̵’ HTML5 modules with Semantics and Accessibility concepts. HTML is what gives structure to the site contents.</p>
</li>
<li><p>This quote says it all:</p>
</li>
</ol>
<p>You are the CSS to my HTML — Some Genius</p>
<p>CSS is how HTML is presented in the web browser. Make sure to learn SASS preprocessor as your savior in writing modular CSS once you get a grasp of the basics.</p>
<ol start="3">
<li>The organization behind WordPress has big plans for making JavaScript part of the WordPress Front-end. This officially came from the original author of WordPress.</li>
</ol>
<p>Learn JavaScript, Deeply — Matt Mullenweg</p>
<p>Yes, I know that might be far-fetched for a newbie who is starting out to follow this advice, but keep an eye out for development in this area. Now, this doesn’t mean you have to master JavaScript to work with WordPress. Start with the basics including its hugely popular library jQuery.</p>
<ol start="4">
<li><p>Wouldn’t it be nice if you could get a 1000-step head-start with these three technologies to speed up your development process? Well, that is where front-end frameworks like <a target="_blank" href="http://getbootstrap.com/">Twitter Bootstrap</a> come into play. Explore it and get amazed. Don’t confuse front-end frameworks with JavaScript frameworks. Front-end is used interchangeably with JavaScript libraries and frameworks, which is a whole different path.</p>
</li>
<li><p>PHP is the scripting language that powers WordPress. If you want to dive deep in WordPress, learn PHP head-first with its OOP concepts including MySQL as its back-end database.</p>
</li>
<li><p>In general, when people talk about WordPress Development, it usually means only two segments of WordPress.</p>
</li>
<li><p>WordPress Theme Development, which is developing a presentation (layout) of sites (hence the name “Theme”).</p>
</li>
<li>WordPress Plugin Development, which is to add functionality to WordPress sites</li>
</ol>
<p>For a developer, it is of utmost importance to understand and be comfortable working with both these segments. And to add a tad bit more in your learning journey, get familiar with Internationalization and Localization. You also need to know WordPress Hooks and APIs to name a few.</p>
<p>While developing sites, most of your time is going to be spent in a local development environment and in a code editor. For the sake of complete information in this article, let me point you to an easy-to-setup yet powerful local development environment (<a target="_blank" href="https://local.getflywheel.com/">Local by Flywheel</a>) and lightning-fast code editor (<a target="_blank" href="https://code.visualstudio.com/">Visual Studio Code</a> by Microsoft). Yes, they are both free of cost like WordPress.</p>
<h2 id="heading-workflow"><strong>Workflow</strong></h2>
<p>As your workflow becomes more advanced and complex, it’s important for you to stay sane and keep enjoying life. In that case, you are going to need to streamline your workflow. Try task automation tools like <a target="_blank" href="https://gruntjs.com/">Grunt</a>/<a target="_blank" href="https://gulpjs.com/">Gulp</a> and use a version control system like <a target="_blank" href="https://git-scm.com/">git</a> to keep you from making blunders.</p>
<h2 id="heading-illustration-of-learning-path-guide-published-on-github"><strong>Illustration of Learning Path Guide Published on GitHub</strong></h2>
<p>To make things more clear for you, I have published an illustration of the Learning Path as a step-by-step guide on <a target="_blank" href="https://github.com/ihtishamzahoor/wordpress-learning-path">GitHub</a> which I have provided below.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/BglSkXSPbis8Tkk86z5kuf3VVx4Pt2KNkdfx" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Learn from it, give feedback, improve it to make it better for newbies, and by that means feel free to send PR requests.</p>
<h1 id="heading-resources"><strong>Resources</strong></h1>
<p>In case you are still worried that you are going to need more than a bunch of motivational words to actually start doing meaningful work, maybe you need like a step by step guide — well, worry no more. Follow the <a target="_blank" href="https://developer.wordpress.org/"><strong><em>WordPress WordPress Developer Handbook</em></strong></a> and <a target="_blank" href="https://codex.wordpress.org/"><strong><em>WordPress Developer Codex</em></strong></a> like religion and consider yourself in safe hands.</p>
<p>Moreover, you can receive great help from the <a target="_blank" href="https://wordpress.org/support/"><strong><em>WordPress Support</em></strong></a> forum as well as digging your answers from the Q&amp;A site <a target="_blank" href="https://stackoverflow.com/"><strong><em>Stack Overflow</em></strong></a>, which is immensely popular among the developer community.</p>
<p>Apart, from the official WordPress resources, support forum, and developer-centric Q&amp;A sites. There are a plethora of sites like <a target="_blank" href="https://wpbeginner.com/"><strong><em>WPBeginner</em></strong></a><strong><em>,</em></strong> made possible by the awesome WordPress community, offering WordPress centric free tutorials and guides.</p>
<p><a target="_blank" href="https://www.codexspot.com/category/tutorials/"><strong><em>CodexSpot</em></strong></a> is one such attempt by a bunch of <a target="_blank" href="https://www.codexspot.com/about-us/"><em>WordPress geeks</em></a> including yours truly to give back to the community. <em>CodexSpot</em> is an online platform, focused solely on providing web solutions. Our aim is to provide quality tutorials, DIY guides, and keep you up-to-date with web industry news and trend reports.</p>
<p>We are a huge supporter of free and open-source software initiative and don’t miss any opportunity to evangelize FOSS practices to promote this movement. Besides that, we also offer free web development generators and tools to speed up your development time.</p>
<p>Now, there is one other type of learning resource, which is premium courses. By comparison with free resources, the main benefit of premium ones is the timely support offered by the course instructors for their students. That is to say, the ability to get your queries answered by the very instructors from whom you are learning, hugely benefits you during your learning journey. Furthermore, they stay on top of the latest industry trends.</p>
<p>I have personally learned a great deal from the <a target="_blank" href="https://www.lynda.com/learning-paths/Web/become-a-junior-wordpress-developer"><strong><em>Lynda.com</em></strong></a> (now <em>LinkedIn Learning</em>) courses, highly recommended. I have also found <a target="_blank" href="https://onemonth.com/courses/wordpress"><strong><em>Learn WordPress</em></strong></a> course by <em>Chris Castiglione, a nice beginner-friendly course to start with. So, if you are looking for a premium course on WordPress, you will find any of these courses worth your buck</em>.</p>
<p>At this point, you have the dedication and tons of resources readily available for you to learn everything about WordPress. However, it is equally important that you understand the job market and what the companies seek in candidates applying for the WordPress developer position.</p>
<p><a target="_blank" href="https://www.toptal.com/">Toptal</a>, an exclusive network of the top freelancers in the world, has published a guide for hiring a “<a target="_blank" href="https://www.toptal.com/wordpress#hiring-guide">Great WordPress Developer</a>” which you can read to better align yourself with the most in-demand WordPress skills.</p>
<h1 id="heading-conclusion">Conclusion</h1>
<p>Don’t try to learn everything at once. Start with the basics. Implement what you’ve learned and then build from there. Take note of these following two not hard-and-fast rules for guidance, as these rules will come in handy in your journey.</p>
<ul>
<li><em>Just in Time (JIT) learning</em> is you learn the tool when you need it.</li>
<li><em>80/20 rule of learning</em> is you give 20% of your time to learning and 80% of your time implementing what you have learned already.</li>
</ul>
<p>Now repeat after me:</p>
<p>I learn.<br>I code!<br>I code again!!<br>I code some more!!!<br>I keep coding!!!!!</p>
<p>…and that is how you start in WordPress. Good Luck WordPresser and welcome to the WordPress family!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ A Vue.js introduction for people who know just enough jQuery to get by ]]>
                </title>
                <description>
                    <![CDATA[ By Matt Rothenberg I’ve had a love-hate relationship with JavaScript for years. I got to know the language by way of the design and development community’s favorite whipping boy, jQuery. You see, at the time I began learning JavaScript, as a “Designe... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/vue-js-introduction-for-people-who-know-just-enough-jquery-to-get-by-eab5aa193d77/</link>
                <guid isPermaLink="false">66c3649f8e244e1678738610</guid>
                
                    <category>
                        <![CDATA[ Design ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ jQuery ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Vue.js ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 10 Apr 2017 23:30:41 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*-8AAdexfOAK9R-AIha_PBQ.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Matt Rothenberg</p>
<p>I’ve had a love-hate relationship with JavaScript for years.</p>
<p>I got to know the language by way of the design and development community’s favorite whipping boy, <a target="_blank" href="https://jquery.com/">jQuery</a>. You see, at the time I began learning JavaScript, as a “Designer who codes,” working with jQuery was a magical experience. I could make modals <code>fadeIn</code> and <code>fadeOut</code>. With a third-party library, I could add parallax scrolling to my portfolio with just a single function call. Nearly everything I could have possibly dreamed of was encapsulated in a <a target="_blank" href="https://code.jquery.com/jquery-3.2.1.min.js">single, ~100kb file</a>…</p>
<p>And then <a target="_blank" href="https://angularjs.org/">Angular</a> came out. I had <em>no</em> <em>choice</em> but to redo my entire portfolio with the framework. And then <a target="_blank" href="https://facebook.github.io/react/">React</a> came out. I had <em>no</em> <em>choice</em> but to redo my entire portfolio with the library. And then <a target="_blank" href="http://vuejs.org">Vue.js</a> came out. I had <em>no</em> <em>choice</em> but to redo my entire portfolio with the library… You see where this is going.</p>
<p>All jokes aside, I have greatly enjoyed honing my JavaScript chops through building things here and there with these different frameworks and libraries. I have read countless articles and tutorials in the process, but none has stuck with me more than Shu Uesugi’s piece, “<a target="_blank" href="http://chibicode.com/react-js-introduction-for-people-who-know-just-enough-jquery-to-get-by/">React.js Introduction For People Who Know Just Enough jQuery To Get By</a>.”</p>
<p>Shu takes readers — who are presumed to have some level of proficiency with JavaScript fundamentals and jQuery — on a journey through the world of React as they build a clone of Twitter’s “compose tweet” component.</p>
<p>This conceptual frame was quite helpful to me as someone who learns best by doing. Indeed, any time a new JavaScript library comes out, I find myself going back to the example from this article to test the waters. And so, I would like to borrow this frame as I step you all through my recent experience of learning Vue.</p>
<p>Before you begin the steps below, I highly encourage you to read Shu’s article. He does a fantastic job of walking you through the jQuery code you might write in order to implement some of these features. Thus, and so as to mitigate the risk of redundancy, I will focus on showing you the ins-and-outs of Vue.</p>
<h3 id="heading-what-were-building">What We’re Building</h3>
<p>Most of us tweet (some more prolifically than others). So we are probably familiar with the User Interface component in the screenshot below.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*U74cvvl2oY5Yf4Kq0AMMYA.png" alt="Image" width="635" height="408" loading="lazy">
<em>Twitter’s “Compose Tweet” box</em></p>
<p>Believe it or not, this UI component is a great example of how Vue (and React, as per Shu) might improve your life as a JavaScript/jQuery developer. The elements of this component that we will focus on building today are:</p>
<ul>
<li>The <code>&lt;textar</code>ea&gt; where users can enter their tweet</li>
<li>The <code>&lt;butt</code>on&gt; that is enabled/disabled as a function of how long the tweet is</li>
<li>A counter that indicates how many characters (out of 140) remain, and changes color to warn the user of this limit</li>
<li>A camera icon that, when clicked, allows users to attach photos to their tweet</li>
<li>A list of photos that have been attached</li>
<li>A button (per each photo) to remove it from the tweet</li>
</ul>
<h4 id="heading-if-youre-stuck">If You’re Stuck</h4>
<p>If at any point you come across something that is confusing or poorly explained, don’t hesitate to tweet me at <a target="_blank" href="https://twitter.com/mattrothenberg">@mattrothenberg</a>. Keep in mind as you read this article: it’s not you, it’s definitely me.</p>
<p>Let’s get started.</p>
<h4 id="heading-how-were-building-it">How We’re Building It</h4>
<p>Today, we will be using <a target="_blank" href="https://codepen.io">CodePen</a> to build our “Compose Tweet” component. For the uninitiated, CodePen is an online HTML/CSS/JavaScript editor akin to <a target="_blank" href="http://jsbin.com/">JSBin</a> or <a target="_blank" href="http://jsfiddle.net">JSFiddle</a>. For each step, I’ll embed a CodePen with the relevant code.</p>
<h3 id="heading-step-1-scaffold-the-project">Step 1: Scaffold The Project</h3>
<p>The first thing we need to do, before writing any JavaScript, is to write the markup for our “Compose Tweet” component. Today we will be using <a target="_blank" href="http://tachyons.io/">Tachyons</a> for almost all of our stylistic needs (so that we don’t have to write extraneous CSS, and can focus on the markup and JavaScript).</p>
<p>I am running with the assumption that you are pretty CSS savvy, so I will not spend time walking you through the Tachyons approach to styling (tl;dr so style, much classes, very functional).</p>
<p>In this CodePen I have also gone ahead and pulled in Vue via CDN. Indeed, one of the main selling points of Vue is the simplicity by which it can be integrated into a new or existing codebase.</p>
<p>With everything in place, let us get started on some feature work.</p>
<h3 id="heading-step-2-implement-the-first-feature-tweet-button-should-initially-be-disabled">Step 2: Implement the First Feature — Tweet Button Should Initially Be Disabled</h3>
<p><strong>Feature Description</strong>: Disable the blue Tweet button until a user has entered at least one character in the <code>textarea</code> .</p>
<p>First things first, let us set up our Vue instance. As mentioned above, Vue has won the hearts and minds of Developers by its simplicity of installation and ease of use. We can construct a Vue instance with the following code.</p>
<pre><code><span class="hljs-keyword">new</span> Vue({  <span class="hljs-attr">el</span>: <span class="hljs-string">'#twitterVue'</span>,  <span class="hljs-attr">data</span>: {    <span class="hljs-attr">tweet</span>: <span class="hljs-string">''</span>  },  <span class="hljs-attr">computed</span>: {    <span class="hljs-attr">tweetIsEmpty</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{      <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.tweet.length === <span class="hljs-number">0</span>;    }  }})
</code></pre><p>Let me explain what is going on here —</p>
<ul>
<li><code>el</code> refers to the DOM element to which we are attaching our Vue instance. This should feel similar to instantiating a jQuery plugin by passing in a given selector, be it a class name or ID.</li>
<li><code>data</code> is an object that describe’s our instance’s data model, or state. We can access the attributes specified within this model both in our HTML — via a special curly brace syntax <code>{{tweet}}</code>— and within the instance itself (hint, look at the body of the <code>tweetIsEmpty</code> function)</li>
<li><code>computed</code> is an object that describes, as you might guess, computed properties based on our data model. Instead of adding logic to our HTML, it is recommended to encapsulate this kind of state (or any derived value, for that matter) via functions defined on the <code>computed</code> key of our Vue instance.</li>
</ul>
<p>Turning our attention now to the HTML, you will see that our markup has changed ever so slightly from the first CodePen. Specifically, we have made three changes.</p>
<ol>
<li>We added the id <code>twitterVue</code> to the outermost <code>div</code> so that we could construct our Vue instance.</li>
</ol>
<pre><code>&lt;div id=<span class="hljs-string">"twitterVue"</span>&gt;...&lt;/div&gt;
</code></pre><ol start="2">
<li>We added the <code>**v-model**</code> directive to our <code>textarea</code>, thereby creating a <a target="_blank" href="https://vuejs.org/v2/guide/forms.html">two-way binding</a> between user input and our instance’s data model. Now, any time a user types in the <code>textarea</code>, the <code>**tweet**</code> attribute on our instance’s data model is automagically updated.</li>
</ol>
<pre><code>&lt;textarea v-model=<span class="hljs-string">"tweet"</span>&gt;&lt;/textarea&gt;
</code></pre><ol start="3">
<li>We added the <code>**:disabled**</code> attribute to our <code>button</code>. The colon preceding <code>disabled</code> denotes that we would like to <em>evaluate</em> the content inside the quotes as a JavaScript expression. If we were to omit the colon, the content would be treated as a string. You’ll also note that we’ve added a few lines of CSS to give the disabled button a distinct visual styling.</li>
</ol>
<pre><code>&lt;button :disabled=<span class="hljs-string">"tweetIsEmpty"</span>&gt;Tweet&lt;/button&gt;
</code></pre><pre><code>...
</code></pre><pre><code>button[disabled] {  <span class="hljs-attr">cursor</span>: not-allowed;  opacity: <span class="hljs-number">.5</span>;}
</code></pre><ol start="4">
<li>We also added a computed property on our instance called <code>**tweetIsEmpty**</code>. Note that this property is actually a <em>function</em> that returns a boolean value based on the length of our data model’s <code>tweet</code> attribute. Vue makes it dead simple to access your data model both in the HTML (as shown above) and in the instance itself. Thanks to the magic of two-way data binding, this function is evaluated when the value of <code>tweet</code> is updated. When the function evaluates to <em>true</em>, our button is disabled, and vice-versa.</li>
</ol>
<pre><code>tweetIsEmpty: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.tweet.length === <span class="hljs-number">0</span>;}
</code></pre><p>Admittedly, this felt like smoke and mirrors when I first got started with Vue. What helped me was to literally <em>see</em> what was happening to our data model under the hood as I interacted with the component. Since we can easily access our data model in our HTML via the aforementioned curly brace syntax, we can build a quick, visual feedback loop. Score!</p>
<pre><code>&lt;p&gt;The value <span class="hljs-keyword">of</span> &lt;strong&gt;tweet &amp;lt;<span class="hljs-regexp">/strong&gt;is: {{tweet}} &lt;/</span>p&gt;<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>The value of <span class="hljs-tag">&lt;<span class="hljs-name">;strong</span>&gt;</span>tweetIsEmpty<span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span></span>is: {{ tweetIsEmpty}}&lt;/p&gt;
</code></pre><p>Please feel free to repeat this step if anything along the way was confusing (either due to my poor writing or coding abilities, or due to Vue itself). Send a tweet or leave a comment if you have any particular questions.</p>
<h3 id="heading-step-3-implement-the-second-feature-show-the-number-of-characters-remaining">Step 3: Implement the Second Feature — Show The Number of Characters Remaining</h3>
<p><strong>Feature Description</strong>: As a user types, show the number of characters remaining (out of 140) in the tweet. If a user has entered more than 140 characters, disable the blue Tweet button.</p>
<p>So far we have learned about two-way data binding and computed properties, concepts that are at the very core of Vue. It is our lucky day, because we can leverage these concepts to build our next feature: showing the users how many characters (out of 140) remain, and disabling the button if this limit is eclipsed.</p>
<p>Once again, I will step you through both the JavaScript and HTML changes required to implement this feature.</p>
<p>In our JavaScript, we have done a few things.</p>
<ol>
<li>As a housekeeping measure, we enumerated the max length of a tweet (140 characters) as a constant, <code>**MAX_TWEET_LENGTH**</code>.</li>
</ol>
<pre><code><span class="hljs-keyword">const</span> MAX_TWEET_LENGTH = <span class="hljs-number">140</span>;
</code></pre><ol start="2">
<li>We added another computed property, <code>**charactersRemaining**</code>, which dynamically returns the difference between 140 and the length of the user-entered tweet.</li>
</ol>
<pre><code>charactersRemaining: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-keyword">return</span> MAX_TWEET_LENGTH - <span class="hljs-built_in">this</span>.tweet.length;}
</code></pre><ol start="3">
<li>We renamed the old <code>**tweetIsEmpty**</code> property to <code>**tweetIsOutOfRange**</code> and updated the function’s logic accordingly. Note how we are using the computed <code>**charactersRemaining**</code> property to derive <em>this</em> value. Hooray for code reuse!</li>
</ol>
<pre><code>tweetIsOutOfRange: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.charactersRemaining == MAX_TWEET_LENGTH       || <span class="hljs-built_in">this</span>.charactersRemaining &lt; <span class="hljs-number">0</span>; }
</code></pre><p>On the HTML side of things, we only have to make a few changes, thanks to the power of Vue’s two-way data binding.</p>
<pre><code>&lt;div <span class="hljs-class"><span class="hljs-keyword">class</span></span>=<span class="hljs-string">"flex items-center"</span>&gt;  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"mr3 black-70"</span>&gt;</span>{{ charactersRemaining }}<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span></span>  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">:disabled</span>=<span class="hljs-string">"tweetIsOutOfRange"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"button-reset bg-blue bn white f6 fw5 pv2 ph3 br2 dim"</span>&gt;</span>Tweet<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>&lt;/div&gt;
</code></pre><p>For the visual learners out there, watch the magic:</p>
<h3 id="heading-step-4-implement-the-third-feature-conditional-styling-of-characters-remaining-indicator">Step 4: Implement the Third Feature: Conditional Styling of “Characters Remaining” indicator</h3>
<p><strong>Feature Description:</strong> When composing a Tweet, the color of the “characters remaining” indicator should change to dark red when only twenty characters remain, and light red when ten or fewer remain.</p>
<p>Manipulating an element’s style or class can be cumbersome with jQuery, and Vue offers a much cleaner way of doing so. Vue’s approach feels more declarative, in that you describe <em>how</em> you want something’s style to change (based, for example, on a given state) and you let Vue do the heavy lifting.</p>
<p>In the context of this feature, our “characters remaining” indicator has two such states, and a corresponding CSS class for each.</p>
<ol>
<li>When between ten and twenty characters remain, the indicator should have the <code>dark-red</code> class</li>
<li>When fewer than ten characters remain, the indicator should have the <code>light-red</code> class</li>
</ol>
<p>By now your Vue brain should be shouting “COMPUTED PROPERTIES!” So, let us oblige this brain and wire up those properties.</p>
<pre><code>underTwentyMark: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.charactersRemaining &lt;= <span class="hljs-number">20</span>     &amp;&amp; <span class="hljs-built_in">this</span>.charactersRemaining &gt; <span class="hljs-number">10</span>;  },<span class="hljs-attr">underTenMark</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.charactersRemaining &lt;= <span class="hljs-number">10</span>;}
</code></pre><p>With our logic in place, let us take a look at one of the ways in which Vue handles conditional styling: the <code>v-bind:class</code> directive. This directive expects an object whose keys are CSS classes, and whose values are the corresponding computed properties.</p>
<pre><code>{ <span class="hljs-string">'dark-red'</span>: underTwentyMark, <span class="hljs-string">'light-red'</span>: underTenMark }
</code></pre><p>By adding the directive to the <code>span</code> tag that encloses our “characters remaining” indicator, we have completed our feature.</p>
<pre><code>&lt;span   v-bind:<span class="hljs-class"><span class="hljs-keyword">class</span></span>=<span class="hljs-string">"{ 'dark-red': underTwentyMark, 'light-red': underTenMark }"</span>&gt;  {{ charactersRemaining }}&lt;/span&gt;
</code></pre><p>Under the hood, and thanks to two-way data binding, Vue will handle the addition and removal of these classes as a function of the specified computed properties.</p>
<h3 id="heading-step-5-implement-the-fourth-feature-attach-photo-ux">Step 5: Implement the Fourth Feature: “Attach Photo” UX</h3>
<p><strong>Feature Description:</strong> Allow users to attach a single photo to their tweet via a file picker dialog. When the photo has been uploaded, show it beneath the <code>textarea</code>, and allow users to delete the attachment by clicking the image</p>
<p>Fair warning: there is <em>a lot</em> going on in this section. The beauty is, despite this feature adding considerable functionality, we will not have to write that much code. So, let us start by breaking down the interaction design into steps.</p>
<ol>
<li>User clicks the “Add Photo” button</li>
<li>User sees a file picker dialog and can select <strong>one</strong> <strong>photo</strong> to upload</li>
<li>Upon selecting the photo, a box appears underneath the <code>textarea</code> with the selected photo inside</li>
<li>User clicks the circular <strong>X</strong> button to remove the photo</li>
<li>User sees initial state from step 1</li>
</ol>
<p>Up to this point, we have not yet done any event handling (listening to button clicks, input changes, etc). As you might expect, Vue makes it easy to handle such events by affording us the <code>v-on</code> directive (@ for short). By passing a method as a value of this directive, we can effectively listen to DOM events and run JavaScript when they are triggered.</p>
<p>Before diving into our feature work, some rapid-fire practice.</p>
<p>Event handling is as easy as adding the <code>@click</code> directive to a given button and adding a corresponding method to the <code>methods</code> key on our Vue instance.</p>
<pre><code>&lt;button @click=<span class="hljs-string">"logNameToConsole"</span>&gt;Log User<span class="hljs-string">'s Name&lt;;/button&gt;...methods: {  logNameToConsole: function() {    if( this.name !== '</span>Donald Trump<span class="hljs-string">' ) {      console.log(this.name);     } else {      console.warn('</span>Sorry, I <span class="hljs-keyword">do</span> not understand<span class="hljs-string">');    }  },}</span>
</code></pre><p>Back to our feature work… In this step, our markup and JavaScript have changed in the following ways:</p>
<ol>
<li>We added a <code>button</code> with an <code>**@click**</code> directive. When a user clicks this button, the <code>**triggerFileUpload**</code> method will get called.</li>
</ol>
<pre><code>&lt;button @click=<span class="hljs-string">"triggerFileUpload"</span>&gt;...&lt;/button&gt;
</code></pre><p>So, in our JavaScript, let us add a <code>methods</code> key to our Vue instance with said method inside, as well as a data attribute for our photo.</p>
<pre><code>data: { <span class="hljs-attr">photo</span>: <span class="hljs-literal">null</span>},<span class="hljs-attr">computed</span>: {},<span class="hljs-attr">methods</span>: {  <span class="hljs-attr">triggerFileUpload</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{    <span class="hljs-built_in">this</span>.$refs.photoUpload.click(); <span class="hljs-comment">// LOLWUT?  },}</span>
</code></pre><ol start="2">
<li>It is notoriously difficult to <a target="_blank" href="http://stackoverflow.com/questions/572768/styling-an-input-type-file-button">style HTML5 file inputs</a>. One workaround involves putting an <code>input</code> in the DOM and hiding it with CSS. In order for the browser to open the native file picker, this <code>input</code> <em>must</em> be clicked. How it gets clicked, and how the client handles what the user uploads, though, is a different matter.</li>
</ol>
<p>In our markup, we’ve added one such <code>input</code> and hidden it with a special <code>hide</code> class. We have also added a few other attributes worth calling out:</p>
<pre><code>&lt;input ref=<span class="hljs-string">"photoUpload"</span> @change=<span class="hljs-string">"handlePhotoUpload"</span> type=<span class="hljs-string">"file"</span> <span class="hljs-class"><span class="hljs-keyword">class</span></span>=<span class="hljs-string">"hide"</span>&gt;
</code></pre><ul>
<li>The <code>ref</code> attribute is used to register a <em>reference</em> to a given DOM element. Given this reference, we can access the DOM element in our JavaScript code with <code>**this.$refs.photoUpload**</code>. Which means we can programmatically trigger a <code>click()</code> event on this element, thereby circumventing the challenge described above.</li>
<li>Clicking on the input is one thing; handling the file that the user uploads is another. Luckily, Vue allows us to attach a handler to the input’s change event via the <code>@change</code> directive. The method that we pass to this directive will be invoked after a user selects a file from the file picker. That method, <code>**handlePhotoUploa**d</code>, is fairly straightforward</li>
</ul>
<pre><code>handlePhotoUpload: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) </span>{  <span class="hljs-keyword">var</span> self = <span class="hljs-built_in">this</span>;  <span class="hljs-keyword">var</span> reader = <span class="hljs-keyword">new</span> FileReader();        reader.onload = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) </span>{    <span class="hljs-comment">// Set that base 64 string to our data model's 'photo' key    self.photo = (e.target.result);  }  // Read upload file as base 64  string  reader.readAsDataURL(e.target.files[0]); }</span>
</code></pre><p>Take a deep breath, because we are almost done with this feature!</p>
<p>Once a user has uploaded a photo, we need to show a box underneath the <code>textarea</code> with the selected photo inside. Just as the conditional styling of elements is a breeze with Vue, so too is the conditional <em>rendering</em>, or display of elements. You’ll note that in our HTML, we have added the following markup underneath the <code>textarea</code>.</p>
<pre><code>&lt;div v-<span class="hljs-keyword">if</span>=<span class="hljs-string">"photoHasBeenUploaded"</span>&gt;  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">figure</span>&gt;</span>    <span class="hljs-symbol">&amp;lt;</span>button @click="removePhoto"&gt;      ...    <span class="hljs-symbol">&amp;lt;</span>/button&gt;</span>    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">v-bind:src</span>=<span class="hljs-string">"photo"</span>&gt;</span>  <span class="hljs-tag">&lt;/<span class="hljs-name">figure</span>&gt;</span></span>&lt;/div&gt;
</code></pre><p>Vue offers a handful of template helpers (<code>v-if</code>, <code>v-show,</code> <code>v-else</code> , etc) to help you show and hide content conditionally. When the JavaScript expression passed to this directive evaluates to true, the element is rendered, and vice-versa.</p>
<p>In our case, we added a <code>**v-if**</code> statement that evaluates the computed property <code>**photoHasBeenUploaded**</code> .</p>
<pre><code>photoHasBeenUploaded: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.photo !== <span class="hljs-literal">null</span>;}
</code></pre><p>When that function evaluates to true — when the photo key of our data model is not equal to null — the entire <code>div</code> gets rendered. Voilà!</p>
<p>And inside that <code>div</code> we render two elements:</p>
<ol>
<li>The image that was attached, by passing the contents of our data model’s <code>photo</code> key to Vue’s <code>v-bind:src</code> directive</li>
<li>A delete button that features another example of the<code>@click</code> handler, this particular one invoking a function that “removes” the photo by setting our data model’s <code>photo</code> key to null.</li>
</ol>
<pre><code>removePhoto: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-built_in">this</span>.photo = <span class="hljs-literal">null</span>;}
</code></pre><p>We are almost there.</p>
<h3 id="heading-step-6-correction-user-can-attach-photos">Step 6: Correction, user can attach “<strong><em>photos</em></strong>”</h3>
<p>So, we can effectively handle a user attaching <em>one</em> photo to the Tweet, but what if she would like to upload many photos?</p>
<p>By now, you should be thinking something to the effect of: “I guess the only significant change here is being able to show <em>multiple</em> images in that box that appears conditionally beneath the textarea, considering we have already wired up our event handlers…” And you are correct! Let us take a look at the steps we need to follow</p>
<ol>
<li>We need to update our data model by changing <code>photo</code> to <code>photos</code>, the new key being an <em>array</em> of base64 strings (not a single base64 string)</li>
</ol>
<pre><code>data: {  <span class="hljs-attr">photos</span>: []},
</code></pre><ol start="2">
<li>We need to update our computed property <code>photoHasBeenUploaded</code> to check against the length of our new <code>photos</code> key, which is now an array.</li>
</ol>
<pre><code>photoHasBeenUploaded: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.photos.length &gt; <span class="hljs-number">0</span>;}
</code></pre><ol start="3">
<li>We need to update our input <code>@change</code> handler to <em>loop</em> over the uploaded files and push them onto our <code>photos</code> key.</li>
</ol>
<pre><code>handlePhotoUpload: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) </span>{  <span class="hljs-keyword">var</span> self = <span class="hljs-built_in">this</span>;  <span class="hljs-keyword">var</span> files = e.target.files;
</code></pre><pre><code>  <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; files.length; i++) {    <span class="hljs-keyword">let</span> reader = <span class="hljs-keyword">new</span> FileReader();
</code></pre><pre><code>    reader.onloadend = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">evt</span>) </span>{      self.photos.push(evt.target.result);    }
</code></pre><pre><code>    reader.readAsDataURL(files[i]);  }},
</code></pre><p>On the HTML side, however, we must embark into new territory. Iterating over data and rendering content with jQuery can be cumbersome.</p>
<pre><code><span class="hljs-keyword">var</span> array = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>];<span class="hljs-keyword">var</span> newHTML = [];<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; array.length; i++) {    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'UGHHHHHH'</span>);    newHTML.push(<span class="hljs-string">'&lt;span&gt;'</span> + array[i] + <span class="hljs-string">'&lt;/span&gt;'</span>);}$(<span class="hljs-string">".element"</span>).html(newHTML.join(<span class="hljs-string">""</span>));
</code></pre><p>Thankfully, Vue offers an abstraction over this procedure by way of the <code>v-for</code> directive. This directive expects you to <a target="_blank" href="https://vuejs.org/v2/guide/list.html">provide an expression in the form of</a> <code>(thing, index) in collectionOfThings</code>, where <code>collectionOfThings</code> is the source array, <code>thing</code> is an alias for the array element being iterated on, and <code>index</code> is, well, the index of that element.</p>
<p>A prototypical example might look like this:</p>
<p>Where before we had a singular <code>figure</code> element for the user-uploaded photo, we will now have <em>N</em> <code>figure</code> tags corresponding to the length of the <code>photos</code> source array.</p>
<p>Lucky for us, our markup doesn’t have to change too drastically since the overall structure of the design is still the same.</p>
<pre><code>&lt;figure v-<span class="hljs-keyword">for</span>=<span class="hljs-string">"(photo, index) in photos"</span>&gt;  &amp;lt;button @click=<span class="hljs-string">"removePhoto(index)"</span>&gt;    ...  &lt;<span class="hljs-regexp">/button&gt;  &lt;img v-bind:src="photo" class="h3 w3"&gt;&lt;/</span>figure&gt;
</code></pre><p>The one change that we need to make revolves around the <code>removePhoto</code> method which, before, set the singular <code>photo</code> key on our data model to <code>null</code>. Now, since we have <em>N</em> number of photos, we must pass the element’s index to the <code>removePhoto</code> method and pull that element out of the array.</p>
<pre><code>removePhoto: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">index</span>) </span>{  <span class="hljs-built_in">this</span>.photos.splice(index, <span class="hljs-number">1</span>);}
</code></pre><h3 id="heading-step-7-animation-extra-credit">Step 7: Animation + Extra Credit</h3>
<p>In Twitter’s UI, the “Compose Tweet” component opens in a modal. For our grand finale, I would like to apply all of the Vue techniques we have learned so far and introduce one more: <a target="_blank" href="https://vuejs.org/v2/guide/transitions.html">transitions</a>.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*yjSu85EKvxBdwRKqIwJtVA.png" alt="Image" width="800" height="400" loading="lazy">
<em>Transition Lifecycle</em></p>
<p>A word of caution, transitions are a <em>vast</em> subject in Vue land. We are going to examine and implement a thin slice of this functionality, namely integrating a 3rd party animation library, <a target="_blank" href="http://velocityjs.org/">Velocity JS</a>, with Vue.</p>
<p>In a nutshell, Vue provides a <code>transition</code> component that allows you to add enter/leave animations for the element contained within, provided the element is set to be displayed conditionally via, for example, a <code>v-if</code> or <code>v-show</code> directive.</p>
<pre><code>&lt;transition   name=<span class="hljs-string">"modal-transition"</span>  v-on:enter=<span class="hljs-string">"modalEnter"</span>   v-on:leave=<span class="hljs-string">"modalLeave"</span>&gt;    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">v-if</span>=<span class="hljs-string">"modalShowing"</span>&gt;</span>       <span class="hljs-comment">&lt;!-- Our modal contents goes here ! --&gt;</span>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>&lt;/transition&gt;
</code></pre><p>In our example, we have attached two methods that correspond with two events in the transition lifecycle: <code>v-on:enter</code> and <code>v-on:leave</code>. We have thusly added these method definitions to our Vue instance, deferring to Velocity JS to <code>fade</code> our modal in and out.</p>
<pre><code>methods: {  <span class="hljs-attr">modalEnter</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">el, done</span>) </span>{    Velocity(el, <span class="hljs-string">'fadeIn'</span>, { <span class="hljs-attr">duration</span>: <span class="hljs-number">300</span>, <span class="hljs-attr">complete</span>: done, <span class="hljs-attr">display</span>: <span class="hljs-string">'flex'</span> })  },  <span class="hljs-attr">modalLeave</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">el, done</span>) </span>{    Velocity(el, <span class="hljs-string">'fadeOut'</span>, { <span class="hljs-attr">duration</span>: <span class="hljs-number">300</span>, <span class="hljs-attr">complete</span>: done })  }}
</code></pre><p>As mentioned above, the <code>transition</code> will fire when the element contained within is conditionally set to display. So, on the inner <code>div</code> of our <code>transition</code> component, we have added a <code>v-if</code> declaration whose value is a boolean <code>modalShowing</code>. Let us update our instance’s data model accordingly.</p>
<pre><code>data: {  <span class="hljs-attr">modalShowing</span>: <span class="hljs-literal">false</span>}
</code></pre><p>Now, when we want to show the modal, all we have to do is set that boolean to true!</p>
<pre><code>&lt;button @click=<span class="hljs-string">"showModal"</span>&gt;Compose Tweet&lt;/button&gt;
</code></pre><p>And write a method to match.</p>
<pre><code>hideModal: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-built_in">this</span>.modalShowing = <span class="hljs-literal">false</span>;},<span class="hljs-attr">showModal</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{  <span class="hljs-built_in">this</span>.modalShowing = <span class="hljs-literal">true</span>;},
</code></pre><p>With some CSS trickery, we have also attached a <code>click</code> event handler to the backdrop, so users can hide the modal. Score!</p>
<pre><code>&lt;div   @click=<span class="hljs-string">"hideModal"</span>  <span class="hljs-class"><span class="hljs-keyword">class</span></span>=<span class="hljs-string">"backdrop"</span>&gt;&lt;/div&gt;
</code></pre><h3 id="heading-conclusion">Conclusion</h3>
<p>Well, I hope that was not too painful (and that you learned a thing or two along the way). We only took a look at a small sliver of what Vue has to offer, though, as mentioned above, these concepts are crucial to unlocking the potential of Vue.</p>
<p>I admit, it is unfair to compare Vue to jQuery. They are products of different times, with quite different use cases. However, for those who have struggled their way to learn DOM manipulation and event handling through jQuery, I hope these concepts are a breath of fresh air that you can apply to your workflow.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
