admin管理员组

文章数量:1431426

I have no idea how to change the colour of my element if the colour's crimson to white. It has me confused because I've tried many solutions such as turning them into variables. Would anyone be able to tell me what I'm doing wrong, or possibly point me in the right direction? I've tried "duplicate" questions, but none of them really share the same issue.

<button class="btn-startcall10" onclick="recorda()"><i class="fa fa-wave-square"></i> </button>

  function recorda() {
    document.getElementsByClassName("fa-wave-square")[0].style.color = "crimson";
      if () {}
  }

I have no idea how to change the colour of my element if the colour's crimson to white. It has me confused because I've tried many solutions such as turning them into variables. Would anyone be able to tell me what I'm doing wrong, or possibly point me in the right direction? I've tried "duplicate" questions, but none of them really share the same issue.

<button class="btn-startcall10" onclick="recorda()"><i class="fa fa-wave-square"></i> </button>

  function recorda() {
    document.getElementsByClassName("fa-wave-square")[0].style.color = "crimson";
      if () {}
  }
Share Improve this question edited Feb 17, 2021 at 11:51 crzb asked Feb 17, 2021 at 11:43 crzbcrzb 431 silver badge6 bronze badges 4
  • You have to show your HTML. There are many ways to color an element. – Wais Kamal Commented Feb 17, 2021 at 11:46
  • @WaisKamal The HTML has nothing to do with the question I'm asking, similar to your own ment. I'll gladly provide it though. – crzb Commented Feb 17, 2021 at 11:50
  • Do you want to check the color of your element ? if the color is "crimson" you want to do something in the if condition ? or do you want to change your elements color to "crimson" ? – Wisely D Cruizer Commented Feb 17, 2021 at 12:03
  • @crzb I asked because, if the color is defined in a stylesheet or in a <style> element, then the answer provided by @Raycas won't work. This answer only works if the style is defined inline. In the case I mentioned you will have to check for the puted style, which is a little more plicated. – Wais Kamal Commented Feb 17, 2021 at 12:06
Add a ment  | 

3 Answers 3

Reset to default 2

you can try something like this

 function changeColor(){
    el = document.getElementById("fa-wave-square");
  if(el.style.color === 'crimson'){
    el.style.color = 'white';
  } else {
    el.style.color = 'crimson';
  }
 }

https://jsfiddle/Lyuvf9a6/3/

You can use Element.style.color in the javascript to get the current color of the element.

Then based on that color you can change the color of your element.

let clickElement = document.getElementById("span-to-change-color");

clickElement.addEventListener("click", changeColor);

function changeColor() {

  if (clickElement.style.color == "red") {
    clickElement.style.color = "blue";
  } else {
    clickElement.style.color = "red";
  }
}
<span style="color: red;" id="span-to-change-color">I am red(Click Me)</span>

First of all you have to add an event listener for on click event e.g

YOUR_ELEMENT.addEventListener("click", YOUR_LISTENER) // e.g recorda

Your event listener will get the event object from where you can access the object but if it's a nested object e.g your event listener is on div but you have a span inside and on click of span on click event will be triggered and the target object will be the span. But you can cache YOUR_ELEMENT and use that also.

Now you can check the style color for the color and do as necessary.

if (YOUR_ELEMENT.style.color === 'crimson') {
  YOUR_ELEMENT.style.color = 'white'
} else {
  YOUR_ELEMENT.style.color = 'crimson'
}

Here is a sample code e.g

<div id="textChange">Some text</div>
<script>
  var elem = document.getElementsById('textChange')
  function changeColor(e) {
    // You can use and get elem using
    // var ele = e.target
    // Or as we have cached elem we can use that

    if (elem.style.color === 'red') {
      elem.style.color = 'green'
    } else {
      elem.style.color = 'red'
    }
  }

  elem.addEventListener('click', changeColor)
</script>

本文标签: javascriptchange colour of element onclick with ifstatementStack Overflow