Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have multiple data-bind attributes on one element?

I need to have multiple data bindings on one element. For example, I want a href as well as a html data-binding on one a tag. I have tried this,

<a data-bind="html: name"     data-bind="attr: { href: url }"     data-bind="attr: { 'data-prop': xyz }"> </a> 

But this doesn't work. It seems knockout only supports binding one data-bind property? How to bind both the href, the inner html, and a custom "data-prop" attribute on one element?

like image 202
Imran Qadir Baksh - Baloch Avatar asked May 22 '12 10:05

Imran Qadir Baksh - Baloch


People also ask

What is data bind attribute?

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.

Which attribute is used for data binding in HTML?

Datafld attribute specify a column name from the data source object. It is like datafld attribute. It is used for data binding under Internet Explorer 4.

What is data bind in Javascript?

Data binding in concept is quite simple. On one side, you have a data model and on the other side, you have an interface, often called a view. The idea is that you want to “bind” some piece of data to something on the view so that when the data changes, the view changes.


2 Answers

Like this:

<a data-bind="html: name, attr: { href: url }"> 

You use comma-separated bindings - the attribute is the same as passing an object:

{     html: name,      attr: { href: url } } 

Or, if you're asking about multiple attr bindings at once:

<a data-bind="html: name, attr: { href: url, 'data-prop': FullName }"> 
like image 140
paulslater19 Avatar answered Oct 06 '22 23:10

paulslater19


This is how I implemented the source attribute and click event using data-bind. You may find it useful.

<img data-bind="{click: function(data, event) {ESVendorWidget.loadFunction(data,event)},                  attr: {src: $data.Photo.PhotoUrl }}"      alt="package pic" class="big" /> 
like image 25
aamir sajjad Avatar answered Oct 06 '22 23:10

aamir sajjad