Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript opacity translation from css

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #168806
    alpkank
    Participant

    As a beginner to javascript, I was wondering if there is a way to specify opacity in javascript like in css. I googled but could not find how to define it as in css. I get an error when I try in js.
    This is what I want to say in css.
    background-color: rgb(0, 0, 0, 0.5);

    I cannot figure out adding opacity with out getting error.
    var colors = [
    [0, 0, 0],
    ];

    thank you.

    #168892
    dyr
    Participant

    You’ll need to set the style property of the element.
    https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.style

    With Javacript you’ll need to get a reference to the element whose styles you wish to change. This could be an ID or class.

    For example, given the following HTML:

    <div id="myDiv"></div>
    

    You could write something like:

    // grab the element we want to apply styles to
    var el = document.getElementById('myDiv');
    
    // change the background color style property to this rgb value
    el.style.backgroundColor = "rgb(0, 0, 0)";
    

    You can alter any css property this way!

    Cheers

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.