0

So I am writing an app where color labels for images are loaded via Ajax. Let's say I request the labels for a certain images and the server returns a string with the colors. Each image container also contains four hidden divs in the respective colors with the classnames representing the colors...

<div class="red"></div> etc...

So I thought I split the string into an array, loop over it and set it to visible like so:

function attachLabel(obj,col)
...
$.ajax({
        ...
        success:function(response){
            //response contains an array of colors like red,green,blue,yellow
            var cols=response.split(",");
            for(var i=0;i<cols.length;i++) {
                console.log(cols[i]);
                obj.find("."+cols[i])[0].setAttribute('style','display:block')
                }
            },
        });
}

Anyway this results in the following error, but only after exactly two colors have been set successfully:

Uncaught Error: Syntax error, unrecognized expression: .red

I really can't wrap my head around this... Any help would be appreciated...

NateW
  • 2,101
  • 3
  • 28
  • 37
aushilfe444
  • 135
  • 1
  • 10
  • 5
    Can you give us the full code? '...' isn't too useful. Try using jsFiddle. – Michael Giovanni Pumo Mar 20 '15 at 12:39
  • Assuming `obj` is a jQuery object, try changing `obj.find("."+cols[i])[0].setAttribute('style','display:block')` to `obj.find("." + cols[i]).show();` – lshettyl Mar 20 '15 at 12:45
  • [How can I debug my JavaScript code?](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code) – Liam Mar 20 '15 at 13:48
  • Post your HTML and JS code – ArinCool Mar 20 '15 at 13:54
  • The error you are erperiencing isn't trackable to the code you have given us! – Joakim M Mar 20 '15 at 13:56
  • you are right.. it turns out the problem is the response i get from php. if i log cols[0] it prints "red".. but when i log cols[0].length it prints 49.. so seems there is hidden characters in the response?? – aushilfe444 Mar 20 '15 at 14:49

0 Answers0