Knockoutjs native template - To create url
Asked Answered
C

1

6

How to create a url using knockout js native templating.

<a href="http://" title="Visit site">
    <span data-bind="text: site"></span>
</a>

say the expression

<span data-bind="text: site">

substitute www.xyz.com. I am looking a way to prefix it with http://.

So, something like

<a href="http://${site}" title="Visit site">

which may work with jQuery templating. In essence, I wonder we always need <span>, <li> etc to output the text.

Clever answered 5/6, 2012 at 16:20 Comment(0)
S
7

By using the data-bind with the attr binding. You can use plain javascript in the data-bind, so also you can use default concatenation

<a data-bind="attr: {'href':  'http://' + $data.site}, text: 'visit site'"></a>

data-bind can be done on practically everything, as long as it is rendered by the DOM. So you can put it on <a> tags, or whatever you want to use

Shipshape answered 5/6, 2012 at 16:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.