Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knockout - HTML href

Tags:

I have a foreach loop that goes through an array (filesObservableArray). The array has a key/value with the key: URLPath. When I bind the array within the HTML, I would like to set the 'href=' value with the URLPath.

I know this part is a fail, but conceptually, can you see what I'm trying to do?

href="< span data-bind='text: URLPath'>"

Or maybe I can use a 'databind="click: someCode(url)"' and within the someCode function, open the link? The url maps to either a document file (e.g., .doc) or an image file.

Tips appreciated. Thanks!

<tbody data-bind="foreach: $root.filesObservableArray">                     <tr id="tradeRow">                         <td><a href="<span data-bind='text: URLPath'></span>">Open file</a></td>                     </tr>                 </tbody> 
like image 398
nanonerd Avatar asked May 03 '13 17:05

nanonerd


People also ask

How do I use knockout js in HTML?

It is very easy to use KnockoutJS. Simply refer the JavaScript file using <script> tag in HTML pages. A page as in the following image will be displayed. Click on download link and you will get the latest knockout.

What is Databind in HTML?

A data binding connects data from a custom element (the host element) to a property or attribute of an element in its local DOM (the child or target element). The host element data can be a property or sub-property represented by a data path, or data generated based on one or more paths.

How do you activate knockout?

To activate Knockout, add the following line to a <script> block: ko. applyBindings(myViewModel); You can either put the script block at the bottom of your HTML document, or you can put it at the top and wrap the contents in a DOM-ready handler such as jQuery's $ function.

What is Knockout used for?

Knockout is a JavaScript library that helps you to create rich, responsive display and editor user interfaces with a clean underlying data model.


1 Answers

I am not sure what do you want to achive with the span in the href but with the attr binding you can set just fine the href (or any other) attribute:

<tbody data-bind="foreach: $root.filesObservableArray">    <tr id="tradeRow">        <td><a data-bind="attr: { href: URLPath }">Open file</a></td>    </tr> </tbody> 
like image 196
nemesv Avatar answered Sep 25 '22 01:09

nemesv