kishan
kishan

Reputation: 27

Change button value and color both

I am trying to change value on click and color too. I am able to change value via js but unable to change color. This is my Js code

<script type="text/javascript">
    function change(el) {
        if (el.value === "P")
            el.value = "A";
        else
            el.value = "P";
    }
</script>

And this is button:

<td>
    <input id="press5" name="press5" type="button" value="<?php echo $ro5[$_SESSION['dyy']] ?>"
           onclick="return change(this);" onBlur="checkAvailability5()" class="btn btn-primary"/>
</td>

Please help me out. thanks.

Upvotes: 0

Views: 59

Answers (2)

Rahul K
Rahul K

Reputation: 413

This code will change your background color, font color & values of your input button. Working example here.

<input type="button" value="P" onclick="return change(this);"/>

<script type="text/javascript">
function change( el )
{
    if ( el.value === "P" ){
        el.value = "B";
        el.style.backgroundColor = "lightblue";
        el.style.color = "blue";
    }else{
        el.value = "P";
        el.style.backgroundColor = "lightpink";
        el.style.color = "red";
    }
}
</script>

Upvotes: 1

Ram Segev
Ram Segev

Reputation: 2571

use the dom style object read here

function change( el )
    {
        if ( el.value === "P" ){
            el.value = "A";
            el.style.backgroundColor = "red";
        } else {
            el.value = "P";
            el.style.backgroundColor = "blue";
        }
    }

Upvotes: 0

Related Questions