<?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" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kwame Asiedu on Medium]]></title>
        <description><![CDATA[Stories by Kwame Asiedu on Medium]]></description>
        <link>https://medium.com/@kwameopareasiedu?source=rss-7e38bf52d485------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*q4QqJG2O5fmu_-cEg5U1Sg.jpeg</url>
            <title>Stories by Kwame Asiedu on Medium</title>
            <link>https://medium.com/@kwameopareasiedu?source=rss-7e38bf52d485------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 23:11:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kwameopareasiedu/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Restoring values to the pesky <input type=”radio”> and the <select>]]></title>
            <link>https://kwameopareasiedu.medium.com/restoring-values-to-the-pesky-input-type-radio-and-the-select-40805e42f60f?source=rss-7e38bf52d485------2</link>
            <guid isPermaLink="false">https://medium.com/p/40805e42f60f</guid>
            <category><![CDATA[ejs]]></category>
            <category><![CDATA[radiobutton]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[select-tag]]></category>
            <dc:creator><![CDATA[Kwame Asiedu]]></dc:creator>
            <pubDate>Fri, 25 May 2018 17:27:01 GMT</pubDate>
            <atom:updated>2018-05-25T17:27:01.377Z</atom:updated>
            <content:encoded><![CDATA[<p>So you’ve created your brand new web app that stores around an entity (e.g. posts). Your plans are to implement CRUD operations for the said entity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*635VxLPdmzRRpUvWr6t8fw.png" /></figure><p>So, let’s say you created an amazing form to cater for the <strong>CREATE</strong>, you have a list that <strong>READ</strong>s all the entities and you’ve implemented <strong>DELETE</strong> functionality.</p><p>Nicely done, but the remaining task is to implement the <strong>UPDATE</strong> feature. Now to do this, the first thing you need is the instance of the data you want to update. Next you want to restore the values of your <strong>input</strong> elements (these include <strong>select</strong> and <strong>textarea</strong> elements as well). It’s most likely you’ll reuse the create form for this.</p><h4>Restoring Values To Trivial Tags</h4><p>When I say “trivial tags”, I’m referring to inputs which are not selectable (i.e you can use the <strong>value</strong> attribute to assign their value)</p><p>With these trivial tags you can simply inject the required values with the value attribute. I’ll be using <strong>E</strong>mbedded <strong>J</strong>ava<strong>S</strong>cript (EJS )for my HTML templates.</p><pre>&lt;input type=&quot;text&quot; id=&quot;name-input&quot; <strong>value=&quot;&lt;%= user.name %&gt;&quot;</strong>&gt;</pre><p>or using a textarea tag,</p><pre>&lt;textarea id=&quot;description-input&quot;&gt;<strong>&lt;%= user.description %&gt;</strong>&lt;/textarea&gt;</pre><p>As displayed above, this is very simple. Insert the value and we are good to go.</p><h4>Restoring values to non-trivial tags</h4><p>Intuitively, you can guess that these non-trivial tags refer to either <strong>radio</strong> or <strong>select</strong> inputs or even <strong>check boxes</strong>. The reason I refer to them as such is that, you must have some code that determines which of the elements must have the “checked” or “selected” attribute. Having to write that same code for multiple elements can make your HTML look very unpleasant, very quickly.</p><p>Our goal is to have JavaScript perform that evaluation for us and also to have that as a function we can call for different elements, hence, reusable.</p><h4>Restoring values to [&lt;input type=”radio”&gt;]</h4><p>For radio inputs, the idea is to gather all radio buttons with a particular <strong>name</strong> attribute, search through them for the target value and set the <strong>checked</strong> attribute.</p><pre>&lt;input type=&quot;radio&quot; name=&quot;option1&quot; value=&quot;first&quot;&gt; First&lt;br&gt;<br>&lt;input type=&quot;radio&quot; name=&quot;option1&quot; value=&quot;second&quot;&gt; Second&lt;br&gt;<br>&lt;input type=&quot;radio&quot; name=&quot;option1&quot; value=&quot;third&quot;&gt; Third&lt;br&gt;<br>&lt;input type=&quot;radio&quot; name=&quot;option1&quot; value=&quot;fourth&quot;&gt; Fourth&lt;br&gt;<br>&lt;input type=&quot;radio&quot; name=&quot;option1&quot; value=&quot;fifth&quot;&gt; Fifth&lt;br&gt;</pre><p>Let’s say that’s your radio group in your HTML. Now for the JavaScript (built on top of JQuery):</p><pre><strong><em>1.</em></strong><em> let</em> inputs = $(&quot;input[name=option1]&quot;);</pre><pre><strong>2.</strong> $.each(inputs, <em>function</em> (<em>index</em>, <em>input</em>) {</pre><pre><strong>3<em>.</em></strong><em>     let </em>inputValue = $(input).val();</pre><pre><strong>4.</strong>     <strong>// Let&#39;s say our model&#39;s value is &quot;third&quot;</strong></pre><pre><strong>5.</strong>     if (<strong>inputValue == &quot;third&quot;</strong>) {</pre><pre><strong>6.</strong> <strong>        $(input).click();</strong></pre><pre><strong>7.</strong>     }</pre><pre><strong>8.</strong>     <strong>$(input).prop(&quot;checked&quot;, inputValue == value);</strong></pre><pre><strong>9.</strong> });</pre><p>In the code snippet above, we are iterating across our radio inputs with JQuery’s <strong>$.each</strong> function. On each iteration, we check if the value is the value we want to be selected (in this case “third”). If it is, we first trigger a click event and sets the “checked” attribute to true. The reason we call .click() is to call click handlers radio button might have.</p><p>By using this technique, we have restored the value for a set of radio buttons. We can make this reusable by putting it in a function.</p><pre>$.restoreSelectableInputValue = function (inputSelector, targetValue) {</pre><pre>    /* <strong><em>FUNCTION DEFINITION HERE </em></strong>*/<br>}</pre><h4>Restoring values to [&lt;select&gt;&lt;/select&gt;]</h4><p>The idea for restoring the value of a <strong>select </strong>element is similar to that of the radio buttons, except that we gather all the <strong>&lt;option&gt;&lt;/option&gt;</strong> tag inside the target <strong>select</strong> tag and perform the same operations.</p><pre>&lt;select id=&quot;occupation-select&quot;&gt;<br>    &lt;option value=&quot;teacher&quot;&gt;Teacher&lt;/option&gt;<br>    &lt;option value=&quot;engineer&quot;&gt;Engineer&lt;/option&gt;<br>    &lt;option value=&quot;doctor&quot;&gt;Doctor&lt;/option&gt;<br>    &lt;option value=&quot;other&quot;&gt;Other&lt;/option&gt;<br>&lt;/select&gt;</pre><p>In the script tag, we type:</p><pre><strong><em>1.</em></strong><em> let</em> options = $(&quot;select#occupation-select option&quot;);</pre><pre><strong>2.</strong> $.each(options, <em>function</em> (<em>index</em>, <em>option</em>) {</pre><pre><strong>3<em>.</em></strong><em>     let</em> optionValue = $(option).val();</pre><pre><strong>4.</strong>     <strong>// Let&#39;s say our model&#39;s value is &quot;engineer&quot;</strong></pre><pre><strong>5.</strong>     $(option).prop(&quot;selected&quot;, optionValue == &quot;engineer&quot;)</pre><pre><strong>6.</strong> <strong>        .trigger(&quot;change&quot;);</strong></pre><pre><strong>7.</strong> });</pre><p>Like I said earlier, the ideology is similar but this time, we use JQuery’s<strong> .prop()</strong> to set the <strong>selected</strong> attribute to true if the value matches. Here we can see that the <strong>change</strong> event is manually triggered (line 6). This is because we want any change listener to be aware of the change in selection and using prop() does not automatically trigger events.</p><p>Now that we have the restore functionality for our “non-trivial” inputs, we’ll then extend the function definition above to something like this.</p><pre>$.restoreSelectableInputValue = function (inputSelector, targetValue, <strong>type=&quot;radio&quot;</strong>) {<br>    /* <strong>The type determines which implementation to use when<br>restoring the value</strong> */<br>    /* <strong><em>FUNCTION DEFINITION HERE </em></strong>*/<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*GxMekSXx_XcUjVO0s9UcHA.jpeg" /><figcaption>If it don’t work, keep trying</figcaption></figure><h4>Conclusion</h4><p>We now have an easy-to-understand, reusable technique on how to restore values to “non-trivial” HTML tags. For the full implementation visit my <a href="https://github.com/kwameopareasiedu/restore-selectable-input-values">github repo</a>.</p><p>Feel free to play around with this, modify it and make it better. I would love to see how you can work with it. I’ll see you soon and happy coding.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=40805e42f60f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>