简体   繁体   中英

Button color change when selected

I have 10 buttons in one HTML page. I want to use JavaScript to change particular button (button 1) color when button is selected. When user click another button (button 2) the color of that button is should change and first selected button (button 1) should be deselected.

You can define a different CSS class for selected button.

.button-selected {
    border: 2px solid red;
}

Then when a button is clicked, you can call the function like this:

function focusMe(button) {
    document.getElementsByClassName("button-selected")[0].className = "";
    button.className = "button-selected";
}

Add this to HTML

<button name="button1" onClick="focusMe(this);" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM