Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript pass variable to inner click function not working
    primarykey
    data
    text
    <p>I can't seem to figure this out. It's working fine for the first section of the <strong>for loop</strong>, but then the var is lost on the inner click function I commented where it breaks...</p> <p>Plus, there's probably a cleaner way to write it to begin with:</p> <pre><code>$(function () { var a = "rgb(58,88,90)"; var b = "rgb(123,156,158)"; for (var c = 1; c &lt;= 3; c++) { if ($("#select" + c).is(":checked")) { $("#select" + c + "-icon").css("background", a); var d = document.getElementById("select" + c + "_title").innerHTML; $("#Selection_" + c).val(d) } else { $("#select" + c + "-icon").css("background", b); $("#Selection_" + c).val("Off") } $("#select" + c).click(function () { // here's where it stops working... var c is no longer recognized... if ($("#select" + c).is(":checked")) { $("#select" + c + "-icon").css("background", a); var d = document.getElementById("select" + c + "_title").innerHTML; $("#Selection_" + c).val(d) } else { $("#select" + c + "-icon").css("background", b); $("#Selection_" + c).val("Off") } }) } return false }); </code></pre> <p>Here are the first pair of objects it's targeting:</p> <pre><code>&lt;label for="select1"&gt;&lt;aside id="select1-icon" class="icon-form right rounded"&gt;&lt;img src="../common/images/icon-viewDemo.png" /&gt;&lt;/aside&gt; &lt;input type="checkbox" id="select1" name="select" checked="checked" class="view" /&gt; &lt;h5 id="select1_title"&gt;Watch Demo&lt;/h5&gt;&lt;/label&gt; </code></pre> <p>And:</p> <pre><code>&lt;input type="hidden" id="Selection_1" name="Selection_1" value=""/&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload