0

I have a C# web application that uses Razor views and Kendo UI web controls. On my form, I have a DropDownList defined like so:

@(Html.Kendo().DropDownListFor(m => m.MentalStatus).HtmlAttributes(new { id = "cmbMentalStatus", @class = "k-dropdown-width-30", @tabIndex = "1", style = "width:60px", onchange = "OnChangeC0100(cmbMentalStatus);" }).BindTo(ViewBag.ZeroToOne).OptionLabel(" "))

The onchange property refers to this JS method:

function OnChangeC0100(cmb)
{
    var stupidJS = document.getElementById("cmbV14_Adm_C0200_RepetitionThreeWords_Tab6");

    stupidJS.disabled = (cmb.options[cmb.selectedIndex].value == "0");
}

When the second line tries to execute, I get this error

"Cannot read property 'undefined' of undefined"

What am I doing incorrectly? If I set a breakpoint, cmb is not null. stupidJS is not null.

Per this link, disabled is the property to set. Is that not accurate?

Edit: I've looked at these questions:

How to Get Dropdown's Selected Item's text in Kendo UI?

how to get selected value for Kendo DropDownList

http://www.telerik.com/forums/dropdownlist-getting-clientside-value-on-selected-item-in-mvc

and the documentation here; http://demos.telerik.com/kendo-ui/dropdownlist/api

But no matter what I try in the Watch window, I can't get it to work:

enter image description here

That first one is the closest; at least it returns a value, but it's not the correct one. I selected the item whose text and value are both "1", yet it gives an empty string. Not shown in the screenshot, but $("#cmb").kendoDropDownList().val() gives undefined. Why is that if I hardcode a reference to the control, it sort-of works, yet the object passed as a parameter to the method doesn't work?

Community
  • 1
  • 1
sab669
  • 3,984
  • 8
  • 38
  • 75
  • _"`cmb` is not `null`"_, what is it though? the error indicates it's probably something to do with that rather than `stupidJS.disabled` - it's reading a property, and it's reporting `undefined` for both the name of the property it's trying to read (`cmb.selectedIndex`) and the object it's trying to read it on (`cmb.options`), indicating that `cmb` doesn't have either of those properties. It _looks_ like it should be your ` – James Thorpe May 26 '16 at 12:43
  • @JamesThorpe `cmb` is the control created in my razor snippet at the beginning of my post. And from [this SO post](http://stackoverflow.com/questions/1085801/get-selected-value-in-dropdown-list-using-javascript) that's how I determined that's how you access the drop down list's selected item – sab669 May 26 '16 at 12:49

1 Answers1

0

I was able to figure out a solution to my actual task:

function OnChangeC0100(cmb)
{
    var cmbVal = $("#" + cmb.id).val();
    var enabled = (cmbVal != "0");

    $("#cmbSomeControl").kendoDropDownList({ enable: enabled });
    $("#cmbSomeOtherControl").kendoDropDownList({ enable: enabled });

    if (cmbVal == "0")
        document.getElementById("cmbV14_Adm_C900A_MemoryRecall_Season_Tab6").focus();
}

And I modified my kendo DropDownList to pass this to the method, rather than the string containing the ID.

sab669
  • 3,984
  • 8
  • 38
  • 75