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
3 Answers
Reset to default 2you 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
版权声明:本文标题:Javascript, change colour of element on-click with if-statement - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744906809a2631673.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论