tuupola / jquery_chained

Chained Selects for jQuery and Zepto
https://appelsiini.net/projects/chained/
589 stars 283 forks source link

Throws TypeError when data-chained is number #58

Closed yeslad closed 6 years ago

yeslad commented 7 years ago

This line assumes that the value of data-chained is a String. However, as per jQuery docs (behavior of Zepto is similar):

Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null). A value is only converted to a number if doing so doesn't change the value's representation. For example, "1E02" and "100.000" are equivalent as numbers (numeric value 100) but converting them would alter their representation so they are left as strings. The string value "100" is converted to the number 100.

Since Number does not have a .split() method, the script fails at this point if data-chained is a number.

Two solutions are:

  1. Use .attr('data-chained') which will always return a string
  2. var data = $(this).data("chained").toString()
dafik commented 6 years ago

Solution nr 2 will not work when data will be null since null have no method toString()

better:

const data = $elem.data("chained"); if (data) { matches = data.toString().split(" "); }

samuelecarpene commented 6 years ago

Any news about this bug?

callumstrubi commented 6 years ago

Sorry I duplicated this bug here: https://github.com/tuupola/jquery_chained/issues/67