0

I've made a simple script to change the color of a div with the class '.logged_inn_text'. The script works by taking the background style from the clicked link and inserting it to the class.

The code was made in Chrome and it worked perfectly fine, while in IE and Edge nothing happens. If I alert the var named color, there is no response. Is there a reasonable reason why it's not working?

$(document).ready(function() {
    $('.settings_color_btn').click(function() {
        var colorId = $(this).attr('id');
        var colorIdFull = '#'+colorId;
        var color = $(colorIdFull).css('background');
        $('.logged_inn_text').fadeTo('fast', 0.4, function() {
            $(this).css('background', color);
        }).fadeTo('fast', 1);
        return false;
    });
});
6
  • 1
    I'm getting really confused by reading that? When you click an element, you get the ID, then you use that ID as a selector, and you end up with .. the element that was just clicked, or in other words this? Anyway, assuming you're using a version of jQuery that works in the browsers you're using, there's no reason jQuery wouldn't work in IE. Commented Jul 16, 2016 at 20:45
  • Anyway, it works perfectly fine -> jsfiddle.net/kd4eqdvh I'm guessing you didn't give the element an ID Commented Jul 16, 2016 at 20:48
  • The var color = $(colorIdFull).css('background'); will not give you a color, var color = $(colorIdFull).css('backgroundColor'); should Commented Jul 16, 2016 at 20:48
  • @LGSon - correct, it gets you something like rgb(255, 0, 0) none repeat scroll 0% 0% .... etc. and setting that value as the background style on another element should be valid as well. Commented Jul 16, 2016 at 20:49
  • @adeneo Yepp ... just wanted to point out that it is not only a color returned Commented Jul 16, 2016 at 20:50

1 Answer 1

1

Seems IE return undefined (thanks to adeneo who tested that), so changing

var color = $(colorIdFull).css('background');

to

var color = $(colorIdFull).css('background-color');

will do the trick

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.