Reputation: 19
I have some ajax request from database and for each echo I have some css code made for jquery css code.
If I have echoed:
echo '"background-color","red"';
And I by this code from ajax:
success: function(sup){
var code = sup;
$(#divy).css(code); }
If I alert(code);
I get "background-color","red"
But it won't work inside .css() code..
UPDATE: It can work if I have echoed some words and put if statement inside jquery code. And say if code is equal to some word change css.. But I want to know why first one does not work?
Upvotes: 0
Views: 43
Reputation: 4792
You're putting the whole string as one argument, instead of two.
Try splitting the string by the comma like this:
var code = '"background-color","red"';
var args = code.split(',');
$('#divy').css(args[0].slice(1, args[0].length - 1), args[1].slice(1, args[1].length - 1));
Edit: Actually, you also have to strip the quotation marks. Fixed that.
It's probably not the best way, but it will get rid of quotation marks, assuming they're always there at the start and the end.
If you're sure the quotation marks won't be used anywhere in the CSS values, like url("img.png")
, then you could just remove the quotation marks altogether.
code = code.split('"').join('');
Upvotes: 4