Rails / jQuery: How to check for parameters
<p>I've got the following script that gets executed via AJAX in Rails and uses the ERB Template:</p>

<p><strong>Problem</strong><br>
<em>It appears that only the first specified option (unchecking) works for switches and checkboxes works, the part inside the else statements does not work!</em></p>

<pre><code>&lt;% if params[:type] == "switch" %&gt;
    var obj = $('.&lt;%= params[:name] %&gt; a:not(.label) #myonoffswitch:checkbox');
    &lt;% if params[params[:name]] %&gt; // WORKS
        $('.&lt;%= params[:name] %&gt; a:not(.label)').attr("href", "/update?name=abc&amp;abc=true&amp;type=switch"); // Changes link to check on next click
        obj.prop('checked', false); // Unchecks switch
    &lt;% else %&gt; // DOES NOT WORK
        $('.&lt;%= params[:name] %&gt; a:not(.label)').attr("href", "/update?name=abc&amp;abc=false&amp;type=switch"); // Changes link to uncheck on next click
        obj.prop('checked', true); // Checks switch
    &lt;% end %&gt;
&lt;% elsif params[:type] == "checkbox" %&gt;
    &lt;% if params[params[:name]] %&gt; // WORKS
        $('.&lt;%= params[:name] %&gt; a:not(.label)').remove(); // Removes checked checkbox
        $('.&lt;%= params[:name] %&gt;').prepend("&lt;a data-remote='true' href='/update?name=&lt;%= params[:name] %&gt;&amp;amp;&lt;%= params[:name] %&gt;=true&amp;amp;type=checkbox'&gt;&lt;i class='icon ion-android-checkbox-outline-blank' tabindex='0'&gt;&lt;/i&gt;&lt;/a&gt;"); // Changes link to check on next click &amp; Adds unchecked checkbox
    &lt;% else %&gt; // DOES NOT WORK
        $('.&lt;%= params[:name] %&gt; a:not(.label)').remove(); // Removes unchecked checkbox
        $('.&lt;%= params[:name] %&gt;').prepend("&lt;a data-remote='true' href='/update?name=&lt;%= params[:name] %&gt;&amp;amp;&lt;%= params[:name] %&gt;=false&amp;amp;type=checkbox'&gt;&lt;i class='icon ion-android-checkbox' tabindex='0'&gt;&lt;/i&gt;&lt;/a&gt;"); // Changes link to uncheck on next click &amp; Adds checked checkbox
    &lt;% end %&gt;
    $('.&lt;%= params[:name] %&gt; a:not(.label) i').focus(); // Focuses on checkbox
&lt;% end %&gt;
</code></pre>

<p><strong>DOM</strong></p>

<pre><code>      &lt;div class="name flex"&gt;
        &lt;a data-remote="true" href="/update?name=abc&amp;amp;abc=false&amp;amp;type=switch"&gt;&lt;div class="onoffswitch"&gt;
          &lt;input checked="" class="onoffswitch-checkbox" id="myonoffswitch" name="onoffswitch" type="checkbox"&gt;
          &lt;label class="onoffswitch-label" for="myonoffswitch"&gt;&lt;/label&gt;
        &lt;/div&gt;
        &lt;/a&gt;
        &lt;div class="label"&gt;
          &lt;a class="abc label" data-remote="true" href="/update?name=abc&amp;amp;abc=false&amp;amp;type=switch"&gt;ABC
          &lt;/a&gt;
          &lt;p class="abc labeltext"&gt;Lorem ipsum.&lt;/p&gt;
        &lt;/div&gt;
      &lt;/div&gt;

      &lt;div class="name1 flex"&gt;&lt;a data-remote="true" href="/update?name=name1&amp;amp;name1=true&amp;amp;type=checkbox"&gt;
        &lt;i class="icon ion-android-checkbox-outline-blank" tabindex="0"&gt;&lt;/i&gt;&lt;/a&gt;

        &lt;div class="label"&gt;
          &lt;a class="abc label" data-remote="true" href="/update?name=name1&amp;amp;name1=false&amp;amp;type=checkbox"&gt;ABC
          &lt;/a&gt;
          &lt;p class="abc labeltext"&gt;Lorem ipsum.&lt;/p&gt;
        &lt;/div&gt;
      &lt;/div&gt;
</code></pre>

<p>However, I noticed that only if a checkbox or a switch is checked it gets unchecked, and if it is unchecked it will not change it's appearance (the DOM).</p>

<p>What am I doing wrong?</p>