2

I get border-width,border-color dynamically.

I have to set the opacity for the border.but the border-color value is in the hexadecimal format.

var borderCSS = { border: borderWidth + "px solid "+borderColor};

output is:

 border:10px solid #cccccc;

I don't know how to set the border opacity in this case using jquery or javascript.

pcs
  • 1,864
  • 4
  • 25
  • 49
Gmv
  • 2,008
  • 6
  • 29
  • 46

2 Answers2

2

Is this case, you have to convert your Hex color to RGB. I just converted Hex to RGB and then used your code to produce the color property:

var borderColor ="#cccccc";
borderWidth=3;
function hexToRgb(hex) {
    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    return result ? {
        r: parseInt(result[1], 16),
        g: parseInt(result[2], 16),
        b: parseInt(result[3], 16)
    } : null;
}
R = hexToRgb(borderColor).r;
G = hexToRgb(borderColor).g;
B = hexToRgb(borderColor).b;
var borderCSS = "border: "+borderWidth+"px solid rgba("+R+", "+G+", "+B+", "+.5+")";

.5 is opacity value

0

use css() method for setting the border opacity. try using following code

$(document).ready(function(){
    var borderWidth='10';
    var borderColor='#CCCCCC';
    var borderCSS=borderWidth+'px solid '+borderColor;
    $('h1').css('border',borderCSS);
});

or else refer this fiddle

Nagesh Sanika
  • 1,090
  • 1
  • 8
  • 16