Question

I'm using a jQuery chained select dropdown box from this site/script. I put it in the sidebar and it works fine on the homepage but it isn't working on post pages and the debugger points out this error.

Uncaught TypeError: Object function ()
{for(var a=[];this.length;)a.push(this.splice(Math.random()*this.length,1));
for(;a.length;)this.push(a.pop());return this} has no method 'replace'

It says there's an error in escapeQuotes : function(str) {return str.replace(/([""\\])/g, "\\$1");

The beginning of the script:

(function($) {
  $.dynamicDropdown = {
    /**
     * Escape quotation marks and slashes
     * @param {String} String to format
     * @return {String}
     */
    escapeQuotes : function(str) {
      return str.replace(/([""\\])/g, "\\$1");
    },

Here's how I call the function. I'm using a json file to pull the options text and value into the selected boxes :

$(document).ready(function(){

    $.getJSON('suggest.json', function(data){

        var $select = $('#mySelectID');

        $.each(data, function (index, o) {
            var $option = $("<option/>").attr("value", o.Box1ID + ":" + o.Box3).text(o.Box1 + "|" + o.Box2 + "|" + o.Box3);
            $select.append($option);
        });

        $("#mySelectID").dynamicDropdown({"delimiter":"|"});

    });
});

Edited:

It seems that there's a conflict with a random image rotator I just put on the site. I temporarily removed the rotator and the chained select box is working fine. Here's an example to show the error. And this is without the random rotator.

Array.prototype.shuffle = function() {
                var s = [];
                while (this.length) s.push(this.splice(Math.random() * this.length, 1));
                while (s.length) this.push(s.pop());
                return this;
            }

            var picData = [
                ['img1','url_1'],
                ['img2','url_2'],
                ['img3','url_3'],

            picO = new Array();
            randIndex = new Array();  //array of random indexes
            for(i=0; i < picData.length; i++){
                picO[i] = new Image();
                picO[i].src = picData[i][0];
                picO[i].alt = picData[i][1];
                randIndex.push(i);
            }
            randIndex.shuffle();
            window.onload=function(){
                var mainImgs = document.getElementById('carouselh').getElementsByTagName('img');


                for(i=0; i < mainImgs.length; i++){
                    mainImgs[i].src = picO[randIndex[i]].src; //assign a random image
                    mainImgs[i].parentNode.href = picData[randIndex[i]][1];
                    mainImgs[i].alt = picData[randIndex[i]][1];
                }

            }
Was it helpful?

Solution

in this script which you use, the problem is most likely in these lines of code:

for (var i in parts) {
      name += "[\"" + $.dynamicDropdown.escapeQuotes(parts[i]) + "\"]";
  ...
}

The point is, do not iterate over an array using for in loop, since there is probably a function added to the Array.prototype which shows up in for in loop over the array, simply change it to:

for (var i=0;i<parts.length;i++) {
      name += "[\"" + $.dynamicDropdown.escapeQuotes(parts[i]) + "\"]";
  ...
}

then this won't cacth that function any more.


as you added to your post, the reason is exactly what I have pointed out. but if you still insist on using for in loop, you should check the type of parts[i] like this:

for (var i in parts) {
      if(typeof parts[i] != "string") continue;
      name += "[\"" + $.dynamicDropdown.escapeQuotes(parts[i]) + "\"]";
  ...
}

you have the same problem with another for in loop:

for (var i in options) {
    option = $(document.createElement("option"))
      .val($.isArray(options[i]) ? i : options[i])
      .html(i)
      .appendTo(select);
}

change it to for (var i=0;i<options.length;i++) or add this:

if(typeof options[i] != "string") continue;

to the first line of your for loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top