Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knockoutjs get the (real bound) element through click event

See this question. Except that the answer returns the child element when a child element is clicked, i.e. in the case that you bind a div.

<div id="parent" data-bind="click: log">Parent Div<div id="child">Child</div></div>

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

See this fiddle

I want to get the original element the click event was bound to. Any suggestions?

like image 285
Dirk Boer Avatar asked Dec 31 '12 12:12

Dirk Boer


1 Answers

event.currentTarget will give you the element to which the event is bound. Change your Console.log as below:

console.log("you clicked " + event.currentTarget.id);
like image 62
Ravi Y Avatar answered Oct 12 '22 03:10

Ravi Y