0

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?

1
  • 2
    Your question is sort of hard to follow. You should post an example that people can run and reproduce the problem. See this: stackoverflow.com/help/mcve Commented May 4, 2016 at 22:28

1 Answer 1

4

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('');
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah my bad didnt removed that as well, I accepted answer. Need to think a bit forgot "" are for code I dont need em for vars and inside echo I have em so it wont work. :D Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.