Omar Elewa
Omar Elewa

Reputation: 372

How to get the dom element as an argument using data-bind attribute - knockout to manipulate with in my javascript code?

I want to pass the content of an li element as an argument to my function while using data-bind attribute, click method. For example

 <ul>
     <li data-bind='click: titleClick(argument)'>(CONTENT)</li>
 </ul>

What to put instead of argument to pass (CONTENT) to my titleClick function? This is my processing in the js file

var MapProcess = function(){
    this.titleClick = function(titleName){
        for (var i = 0; i<model.markers.length; i++){
            if (titleName == model.markers[i].title){
                var infoWindow = new google.maps.InfoWindow({          
                });
                infoWindow.setContent(model.markers[i].buborek)
                infoWindow.open(map, model.markers[i]);
                break;
            };
        };
    };
};

I want the parameter titleName in my function above to equal the content retrieved from the html.

Thanks in advance.

Upvotes: 0

Views: 144

Answers (1)

Jason Spake
Jason Spake

Reputation: 4304

The second argument in a click handler will be the click event. From that you can get the target element and its associated content.

https://knockoutjs.com/documentation/click-binding.html

var MapProcess = function() {
  this.titleClick = function(data, event) {
    console.log(event.target.textContent);
  }
}

ko.applyBindings(new MapProcess());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

<ul>
  <li data-bind='click: titleClick'>(CONTENT)</li>
</ul>

Upvotes: 2

Related Questions