knockoutjs get element id through click event

jquery, knockout-2.0, knockout.js

Solution

You actually can get access to the event object via a KO click handler.

<button id="somebutton" data-bind="click: log">Click Me </button>

var ViewModel = function() {
    this.log = function(data, event) {
        console.log("you clicked " + event.target.id);
    }
};
ko.applyBindings(new ViewModel());

http://jsfiddle.net/madcapnmckay/e8JPT/

Hope this helps.

Problem

I'm using knockoutjs and I currently have something in my view that looks like this: ``` <img id="myTab1" data-bind="click: pressedTab.bind($data, '#myTab1')" src="images/image1.png"></img> ``` This allows me to get the element ID in my view model: ``` pressedTab = function(tab){ console.log("Element ID: " + tab); } ``` This writes: Element ID: #myTab1 However, it's too repetitive to send the name of the img id in the click event. Is there a way to send the img id without explicitly re-writing it?

Original source

Related problems