0

I want to save a background color of an element in a variable which I sent with an AJAX call, so I can use it in a PHP SESSION, but for some reason I cannot save this css in the variable.

var color_rgb   = $( "#config-steps #selected-color" ).css( 'background-color' );

My AJAX call works fine, I have tested to put a test string in color_rgb and this works fine.

$.ajax( {
    type: "POST",
    url: sbg.ajaxurl,
    data: {
        color_rgb: color_rgb,
        action: 'sbg_config'
    }

});

Has anyone an idea how to fix this?

Andresch Serj
  • 31,835
  • 14
  • 56
  • 99
Robbert
  • 1,196
  • 6
  • 27
  • 56

2 Answers2

1

Try this,

function hexc(colorval) {
    var parts = colorval.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    delete(parts[0]);
    for (var i = 1; i <= 3; ++i) {
        parts[i] = parseInt(parts[i]).toString(16);
        if (parts[i].length == 1) parts[i] = '0' + parts[i];
    }
    return '#' + parts.join('');
}
var color_rgb = $("#config-steps #selected-color").css( 'background-color' );
$.ajax( {
    type: "POST",
    url: sbg.ajaxurl,
    data: {
        // set default black, if not color found, else convert it by hex function
        color_rgb: color_rgb ? hexc(color_rgb) : '#000000',
        action: 'sbg_config'
    }
});

Check the id of your selector. Is it exists or not.

Rohan Kumar
  • 39,838
  • 11
  • 73
  • 103
0

Cast a variable to String in Ajax Call like this :

$.ajax( {
    type: "POST",
    url: sbg.ajaxurl,
    data: {
        color_rgb: String(color_rgb),
        action: 'sbg_config'
    }
});
theHarsh
  • 650
  • 5
  • 8