-1

I need to change div background-image with linear-gradient from Javascript Please try to correct below codings:

<script>
function abc1(){
    var pos = document.getElementById("test");
    pos.style.background-image="linear-gradient(to right,  
    #85e085 0%,
    #85e085 90%,
    #ff9999 0%,
    #ff9999 10%);";
}
</script>
<body>
<div id="test" style="background-image: linear-gradient(to right,  
    #85e085 0%,
    #85e085 50%,
    #ff9999 0%,
    #ff9999 50%); width:200px;">dsfhdh2346346</div>

<input type=button value="CHANGE" onClick="abc1();">
</body>
1
  • Try pos.style.backgroundImage - notice the camel casing, changing background-image to backgroundImage Commented May 20, 2021 at 6:36

1 Answer 1

0

Please try this.

function abc1() {
    var pos = document.getElementById("test");
    pos.style.background="linear-gradient(to right, #85e085 90%, #ff9999 10%)";
}
<div 
  id="test"
    style="background: linear-gradient(to right,  
    #85e085 50%,
    #ff9999 50%); width:200px;"
  >
    dsfhdh2346346
  </div>
  <input type="button" value="CHANGE" onClick="abc1()" />

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.