0

I am using dragula.js, and using ondrop event in mycode,

drake.on('drop', function (el) {
    console.log(el);                       //result <div>foo bar</div>
    var n = typeof(el);                    //return object
    var x = el.indexOf("test");            //error TypeError: el.indexOf is not a function(…)
    // do another code
    })

I want to check if "test" exist on el parameter, but error occur. thx.

Aep Saepudin
  • 172
  • 2
  • 5
  • 14

3 Answers3

2

I think it should be as easy as if ('test' in el)

Jordan Soltman
  • 3,795
  • 17
  • 31
0

indexOf works on strings, so you need to call it using the element's text, rather than the element itself, like so:

el.textContent.indexOf("test");

Edit:

Looks like you want the index of the dragged element rather than matching its text:

[].slice.call(el.parentElement.children).indexOf(el)
skyline3000
  • 7,639
  • 2
  • 24
  • 33
  • error gone but the code does not work even if i find for `foo` that it exist in `el` parameter. it return -1. – Aep Saepudin Dec 06 '16 at 04:26
  • What do you mean you want to check for "test"? The `
    ` contains the strings foo and bar, obviously "test" is not one of those. Do you mean "test" is an attribute of the `
    `? Like `
    foo bar
    `? What condition are you expecting to be true?
    – skyline3000 Dec 06 '16 at 05:43
  • all, include attribute, tag and text. Thx for help, this question is solved i found the answer in github when i wait for answer here. – Aep Saepudin Dec 06 '16 at 07:06
0

Use the dom. Roughly: el.parentElement.children.indexOf(el)

Solved - https://github.com/bevacqua/dragula/issues/209

Aep Saepudin
  • 172
  • 2
  • 5
  • 14