mahesh gahlot mahesh gahlot - 15 days ago 5
Javascript Question

Is there any way in javascript for setting different color on button for different text value

Only those button have same color which have same test value like github tags.

Thanks in advance.

Answer

You should first try yourself and should share your code what you have tried while asking question.

Get all buttons, compare there text values and set background colors.

function changeButtonsBgColors() {
            var buttons = document.getElementsByTagName('button');
            for (var i = 0; i < buttons.length; i++) {
                var button = buttons[i];
                if (button.value == "Close") button.style.background = '#C0C0C0';
                else if (button.value == "Ok") button.style.background = '#33FF3F';
                else if (button.value == "Done") button.style.background = '#4AC91B';
                else if (button.value == "Delete") button.style.background = '#FF0000';
            }
}

Just remember to have this code either in onload event or at the bottom of the page.