JavaScript Button background color toggle button background color css,button background color android,button background

I have been trying to toggle the background-color property of a button onclick but the color only changes once and does not toggle back and forth. Below is the code.

function btnColor(btn, color) {
var property = document.getElementById(btn);
    if (property.style.backgroundColor == "rgb(244,113,33)") {
        property.style.backgroundColor=color;
    }
    else {
        property.style.backgroundColor = "rgb(244,113,33)";
    }
}

<input type="button" id="btnHousing" value="Housing" onclick="toggleLayer('transparent1');btnColor('btnHousing','rgb(255,242,0)');" />
Answer:1

The problem you're having is that the background-color of an element is reported differently in browsers, either rgb, rgba (with, or without, spaces) in hex or in HSL...

So the button will likely never satisfy the if condition, meaning it will always go to the else.

With that in mind, I'd suggest using a class-name to keep track of the un/toggled state:

function btnColor(btn, color) {
var property = document.getElementById(btn);
    if (property.className !== 'toggled') {
        property.style.backgroundColor=color;
        property.className = 'toggled'
    }
    else {
        property.style.backgroundColor = "rgb(244,113,33)";
        property.className = '';
    }
}

JS Fiddle demo.

Of course, if we're using the class of the element, we might as well use CSS to style the element:

function btnColor(btn) {
var property = document.getElementById(btn);
    if (property.className !== 'toggled') {
        property.className = 'toggled'
    }
    else {
        property.className = '';
    }
}

With the CSS:

#btnHousing {
    background-color: rgb(255,242,0);
}

#btnHousing.toggled {
    background-color: rgb(244,113,33);
}

JS Fiddle demo.

The previous JavaScript could be simplified (using the same CSS) to:

function btnColor(btn) {
    var property = document.getElementById(btn);
    property.className = 'toggled' == property.className ? '' : 'toggled';
}

JS Fiddle demo.

Answer:2
if(document.getElementById(id).style.background !== 'rgb(0, 0, 0)')
Answer:3

I have looked for possible roots of my issue but have been unable to do so. I have some java Script that dynamically creates a list of check boxes. Some have text other have anchor links with text ...

I have looked for possible roots of my issue but have been unable to do so. I have some java Script that dynamically creates a list of check boxes. Some have text other have anchor links with text ...

  1. checkbox text not visible c#

I am simply calling as window.location = '../MyController/MyAction?campId=' + campId + '&campName=' + campName; and in action am receiving it as [HttpGet] public ActionResult MyAction(int ...

I am simply calling as window.location = '../MyController/MyAction?campId=' + campId + '&campName=' + campName; and in action am receiving it as [HttpGet] public ActionResult MyAction(int ...

  1. parameter value missing for '-top'
  2. parameter missing value ssrs
  3. invalid_parameter_value missing required field size
  4. xst - parameter value missing for '-top'
  5. parameter is missing value
  6. required parameter 'value' is missing for event 'purchase'
  7. missing parameter value of type interface org.gradle.logging.styledtextoutputfactory
  8. missing parameter value for aura.tag
  9. missing parameter value of type interface org.gradle.internal.factory
  10. missing parameter value in crystal report
  11. report viewer parameter missing value
  12. ssrs hidden parameter missing value
  13. a parameter value is missing from the subscription
  14. missing parameter value error in crystal report

this is similar to my last question. sorry I don't know whether i could edit the same question. so here is my requirement. my HTML has the following part <ul class="products-grid"> <li ...

this is similar to my last question. sorry I don't know whether i could edit the same question. so here is my requirement. my HTML has the following part <ul class="products-grid"> <li ...

I have been trying to write a shared directive for my angular apps that turns an input field into jquery timepicker. I have two input fields, fromTime and toTime, and I want to update the toTime when ...

I have been trying to write a shared directive for my angular apps that turns an input field into jquery timepicker. I have two input fields, fromTime and toTime, and I want to update the toTime when ...

  1. angular directive data binding
  2. angular directive data
  3. angular directive data attribute
  4. angular directive data binding not working
  5. angular directive data change
  6. angular directive pass data
  7. angular directive async data
  8. angular directive get data attribute
  9. angular directive get data
  10. angular directive set data attribute
  11. angular directive send data to controller
  12. angular custom directive data binding
  13. angular update directive data
  14. angularjs data directive
  15. angular directive meta data
  16. angular directive wait for data
  17. angular directive loads before data
  18. angular directive with dynamic data
  19. angular refresh directive on data change
  20. angular directive wait for async data