1

I have a Kendo DropDownList what I have to select an element by the text containing 8. I can select element by the full string, I only need to select it by a substring.

The partly working code looks like:

var ddlist = $("#HibaTipusKod_" + munkatargyaId).data("kendoDropDownList");
console.log(ddlist);
ddlist.value("8/a");
ddlist.trigger("change");

My need is:

ddlist.value(*startswith/contains*"8");
ddlist.trigger("change");

At the logging I can see it right.

Szabbi97
  • 31
  • 8

1 Answers1

2

You could try this:

var options = ddlist.dataSource.options.data;                 
$.each(options, function(i, item) {
    if (item.text.indexOf('8') !== -1) {
      dropdownlist.select(i);
      return false;
    }
});

Demo

Bare in mind that using indexOf to search for the sub-string will be case sensitive, there are lots of alternative methods that could better suit your needs in this question.

DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
jbob77435
  • 147
  • 1
  • 10
  • I get an error like: Uncaught TypeError: Cannot read property 'length' of null at Function.each I've tried this before, but got the same error. – Szabbi97 Sep 03 '18 at 10:46
  • Sounds like it's having trouble getting the dataSource. Your ddlist variable should be defined like this: `var ddlist = $("#CSS-ID-HERE").data("kendoDropDownList");` – jbob77435 Sep 03 '18 at 11:34
  • Updated the anwser with a bigger part of my code. If I'm not mistaken, it's the same. – Szabbi97 Sep 04 '18 at 07:11
  • Try printing the dataSource to the console and check how it's structured or check my demo, there's a link to it my answer. I hope this helps. – jbob77435 Sep 04 '18 at 10:53