22

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>
nanonerd
  • 1,964
  • 6
  • 23
  • 49

2 Answers2

44

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>
nemesv
  • 138,284
  • 16
  • 416
  • 359
  • 1
    Perfect. Thanks to @ Roberto for the same tip. Muchos gracias to @nemesv to show the code as it should be. I'm sure for experienced coders, this is easy to answer. But for newbies like myself, this is not so easy to figure out. The answer is very appreciated. Thank you. – nanonerd May 03 '13 at 17:41
  • How to bind external link in a tag? It's bind with baseurl. I want to add without baseurl. – Rohan Hapani Jul 10 '19 at 06:23
7

Have you looked at possibly using the attr bindings.

http://knockoutjs.com/documentation/attr-binding.html

Roberto Hernandez
  • 2,397
  • 1
  • 16
  • 18