Reputation: 113
From what I understand this should be simple. I am trying to make a row in a table clickable and to do that I wanted to use location.href on my tr.
My href is built with an Id I get from data-bind. How do I combine the click and attribute. I feel I am missing something evident here.
<tr data-bind="click : attr:{'location.href' : '/project/details/' + m.Id}">
Upvotes: 1
Views: 37
Reputation: 3043
You're conflating a couple of Knockout concepts here.
The attr
binding is for binding HTML attributes for that element to the values on your model.
The click
binding is a function that will be called on the click event (usually you'd reference a function on your view model but you can put an anonymous function inline).
To do what you want you should do:
<tr data-bind="click : gotoProject">
Then something like this on your view model:
this.gotoProject = function(){
location.href = '/project/details' + this.Id;
}
Of course there's a million ways to hook up something like this and without seeing the code of your view model I can't give any more specifics.
Also relating to the attr
binding so you can see how it is supposed to work. If you hadn't had it as the value of click but instead at the top level:
<tr data-bind="attr:{'location.href' : '/project/details/' + m.Id}">
This would result in the following (well it won't as it's not a valid attribute name but you get the point).
<tr location.href="/projecct/details/thisID">
Upvotes: 2