I have just started studying jquery and javascript and have encountered a line of code that I dont know how to translate. Any help would be much appreciated. Id like to translate it from JavaScript to jQuery so that I can use classes. Here are the lines of code.
var rgb = getAverageRGB(document.getElementById('try'));
document.body.style.backgroundColor = 'rgb('+rgb.r+','+rgb.g+','+rgb.b+')';
You're mixing things up with that second line:
$('.post').css("background-color", 'rgb('+rgb.r+','+rgb.g+','+rgb.b+')');
And the jQuery way to find an element by its "id" value is
var rgb = getAverageRGB($('#try')[0]);
The $('#try')
part creates a jQuery-wrapped list of nodes that match the selector, so in this case it'll be just one node. However, presuming that API expects a DOM node and not a jQuery wrapper, the trailing [0]
extracts the raw DOM node from the jQuery wrapper.
Keep in mind that jQuery is JavaScript — we're not talking about two different languages.