I have this link on my page – for a find entry by first letter. I want to pass the text of the anchor tag into knockout’s click event inside the data-bind attribute. Is this possible?
<a href="#" data-bind="click: $parent.getManageableEntries()><%= Convert.ToChar(i + 65)%></a></li>
// here's my javascript method in my knockout view model
function ManageEntriesViewModel() {
var self = this;
this.getManageableEntries = function(firstLetter) {
// i want to pass in the text of the <a> tag as the 'firstLetter' variable
}
}
You should probably have the anchor’s text as an observable in your view model. Then you just need to access that observable (instead of directly accessing the anchor element’s text). This gives you maximum flexibility.
I made 2 examples because I wasn’t sure how you’re structuring your view model objects.
Both work fine:
example-1 ,
example-2
If you really want to have the first-letter extraction in your data-bind attribute, you could either put an anonymous function there, or bind your function with an argument, like I did in this fiddle.
I hope one of these approaches fits your needs.