Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding Disabled Class Based On Matching Indices
    primarykey
    data
    text
    <p>Attempting to "disable" a <code>div</code> if its index does not match a clicked <code>a</code> tag:</p> <pre><code>&lt;div data-bind="foreach: slides" class="slidebtns"&gt; &lt;a class="slidebtn" data-bind="text: title, click: $parent.activeIndex.bind($data, $index())" href="#"&gt;&lt;/a&gt; &lt;/div&gt; &lt;div data-bind="foreach: slides"&gt; &lt;div class="screen" data-bind="attr: { id: title }, disable: $parent.activeIndex() !== $index"&gt; &lt;div&gt;test&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>CSS:</p> <pre><code>... .disabled { display: none; } </code></pre> <p>JS:</p> <pre><code>function viewModel() { var self = this; self.activeIndex = ko.observable(0); self.numSlides = ko.observable(3); ... self.slides = ko.computed(function() { var arr = [], n; if (self.numSlides()&lt;6) n = self.numSlides(); else n = 5; for (var i=0; i&lt;n; i++) { arr.push(slideModel(i)); } return arr; }); } function slideModel(n) { var self = this; self.includeImage = ko.observable(true); self.imageSrc = ko.observable('slide_bg1.jpg'); return { title: 'slide'+(n+1) }; } ko.applyBindings(new viewModel()); </code></pre> <p>which does not work. Every <code>div</code> receives a <code>disabled</code> class.</p> <p>As a test I changed the last div to the following to ensure that activeIndex is actually updating properly on click (and it is):</p> <pre><code>&lt;div class="screen" data-bind="attr: { id: title }, css: $root.activeIndex() !== $index ? 'disabled' : ''"&gt; &lt;div data-bind="text: $root.activeIndex()"&gt;&lt;/div&gt; &lt;/div&gt; </code></pre> <p>However, I am too new to KO to figure out how to additionally output the screen $index when the .slidebtn <code>a</code> is clicked so I can actually check if the indices are equal (regardless, they should be equal because there are only 3 of each element type inside a container).</p>
    singulars
    1. This table or related slice is empty.
    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.
    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