Pooshonk
Pooshonk

Reputation: 1324

Unable to get property '1' of undefined or null reference

I have a calendar on my site that opens a pop-up box showing a RGB color selector. It works fine in all browsers apart from IE8. My code is below.

function rgb2hex(rgb) {
    rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    function hex(x) {
        return ("0" + parseInt(x).toString(16)).slice(-2);
    }
    return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}

This is the error

SCRIPT5007: Unable to get property '1' of undefined or null reference

Which points to this line

return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);

I am not familiar with this code as it was completed by someone else at my workplace who has went on holiday for a couple of weeks. If anyone can help point me in the right direction it would be greatly appreciated.

EDIT

This is where the function is called. When I console.log(bgColor) in IE8 it shows up as #rgb(238, 238, 238), so i don't see why it isn't working. Maybe it is me being stupid but for life of me I cannot see it.

$('.period_day').click(function(e) {
    var bgColor = $(this).css('background-color');

if (rgb2hex(bgColor) == "#eeeeee") {
    $('.colour_picker').css('background-color', '#ff0000');
    $('#cal_colour_val').val('#ff0000');
} else {
    $('.colour_picker').css('background-color', bgColor);
    $('#cal_colour_val').val(bgColor);
}
});

Upvotes: 1

Views: 5849

Answers (1)

Pointy
Pointy

Reputation: 413709

The error means that the rgb2hex function is being called with an argument that doesn't match the regular expression. The match failure will result in the variable rgb being null, and that code doesn't check for that possibility.

Thus the real problem is probably with the code that's calling the function.

Upvotes: 2

Related Questions