Within a foreach, I have a span tag and a textarea. Whenever I click the span tag, I want to toggle the visibility of the textarea.
This works partially except it toggles the visibility of all of the textareas within the foreach instead of just the textarea for the particular item I am on.
Here is my code. The code doesn't actually run, but I think there's enough there for you to see what I am trying to do.
function MyViewModel(data) {
var self = this;
self.checkListItems = [1,2,3];
self.textAreaVisible = ko.observable(false);
self.toggleTextArea = function () {
self.textAreaVisible(!self.textAreaVisible());
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<div data-bind="foreach: MyViewModel.checkListItems">
<span data-bind="click: toggleTextArea">Add Comments ></span>
<textarea data-bind="value: comments, visible: textAreaVisible"></textarea>
</div>
I found this link here http://knockoutjs.com/documentation/foreach-binding.html which sounds like maybe I should be using $data somehow, but I'm not sure how to get that to work in this context.
I appreciate any help you can provide.