5

I am using the kendo dropdownlisthelper, which has a change event, see below

@(Html.Kendo().DropDownListFor(m => m)
        .Name(Model.Name)
        .Text(Model.PlaceHolder)
        .BindTo(Model.ListItems)
        .DataTextField("Text")
        .DataValueField("Value")
        .Enable(Model.Enabled)
        .Events(e =>
        {
            e.Change("change");
        })
        .HtmlAttributes(new {@id= Model.ID.ToString() })

The function that handles the change event:

function change(e) {
    var dataItem = this.dataItem(e.item);
    console.log("selected values (" + dataItem.Text + " : " + dataItem.Value + ")");
}

SO this displays the selected value in the console.

The Question: I'm struggling to work this out, but how do i display the Name and ID of the parent element that made the call to the change event(in this case the drop-down list).

So basically i want to display these values: - Name attribute of the dropdown list - Id attribute of the dropdown list

Cheers!

MCSD
  • 151
  • 2
  • 12
  • What do you mean - "how do i display the Name and ID of the parent element that made the call " ? Please provide more details on what you whant to achive – Anton Aug 15 '16 at 13:54
  • Your answer below was useful and was the answer i was looking for. It would be interesting to know if there was a way to pass the entire model to the change event..hmm. – MCSD Aug 15 '16 at 14:57

1 Answers1

11

You can use this approach to get Id of element on which event have occured

function change(e) {
    var elementId = e.sender.element[0].id
}
Anton
  • 1,010
  • 10
  • 30
  • That's useful. This means that you can get the entire element through this code: var inputElement = e.sender.element.context; This variable will then give you access to all the attributes on your input element. Thanks Anton – MCSD Aug 15 '16 at 14:52
  • 1
    Working Fine :) – Thulasiram Oct 28 '20 at 15:18