0

I have the following array in my main.js.

ko.observableArray(
    [ output[0],output[1],output[2],output[3],output[4]]);

and I want to dislpay each output[i] one by one on the ul element of the following html:

<section>
    <ul id="results" ></ul>
</section>

How to achieve it ? Thanks in advance!

3
  • And you have tried... what, exactly? Commented Jun 18, 2017 at 11:32
  • @Tomalak yes have tried the for each binding, but could not successfully achieve it. Commented Jun 18, 2017 at 11:37
  • Then show your code. (Besides: What you want is extremely basic. It's the bread and butter of knockout. The Knockout documentation covers it. The knockout live tutorial covers it. Thousands of threads on Stack Overflow cover it. "I gave up after one attempt" does not cut it. Try harder.) Commented Jun 18, 2017 at 11:39

1 Answer 1

2

maybe you should work through the getting started interactive tutorial. http://learn.knockoutjs.com/

function viewModel(output) {
  this.myArray = ko.observableArray(
    [output[0], output[1], output[2], output[3], output[4]]);
}

ko.applyBindings(new viewModel(['one', 'two', 'three', 'four', 'five']));
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>


<section>
  <ul id="results" data-bind="foreach: myArray">
    <li data-bind="text: $data"></li>
  </ul>
</section>

Sign up to request clarification or add additional context in comments.

1 Comment

Also, it would be better to simply assign the observable array this way: this.myArray = ko.observableArray(output);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.