mahesh gahlot mahesh gahlot - 1 year ago 68
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 Source

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") = '#C0C0C0';
                else if (button.value == "Ok") = '#33FF3F';
                else if (button.value == "Done") = '#4AC91B';
                else if (button.value == "Delete") = '#FF0000';

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download