0

I want do this

$.fn.randomColor=function (cssSelect) {
  var color='RGB(' + rand(0, 255) + ',' + rand(0, 255) + ',' + rand(0, 255) + ')';
  $(this).css({cssSelect:color }); //error
  return this;
};

I want use cssSelect variable in css() function of jQuery

1 Answer 1

1

Use the other form of .css().

$.fn.randomColor=function (cssSelect) {
    var color='RGB(' + rand(0, 255) + ',' + rand(0, 255) + ',' + rand(0, 255) + ')';
     $(this).css(cssSelect, color);
    return this;
};

jQuery.fn.randomColor=function (cssSelect) {
    var color='RGB(' + rand(0, 255) + ',' + rand(0, 255) + ',' + rand(0, 255) + ')';
     $(this).css(cssSelect, color);
    return this;
};

jQuery('div').on('click', function() {
  jQuery(this).randomColor('background');
});

function rand(min, max) {
  return Math.floor((Math.random() * max) + min);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>Click me!</div>

Sign up to request clarification or add additional context in comments.

Comments

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.