/    Sign up×
Community /Pin to ProfileBookmark

Changing background color in JS wipes out css rules.

I change the color of selected buttons to show the user which button to click next. Although all buttons are members of the same CSS class, only the ones I never change are still under CSS (turn red on hover).

[code]document.tournform.calc.style.backgroundColor = “#ffff00”;
document.tournform.taly.style.backgroundColor = “white”;
document.tournform.del1.style.backgroundColor = “white”;

—————————————————
body {
background-color: #B0FFFF;
}
.buttonStyle {
background-color: white;
}
.buttonStyle:hover {
background-color: red;
transition: ease 1s;
}
.buttonStyle:active {
background-color: red;
}
</STYLE>
——————————————————
<input type=”button” class=”buttonStyle” id = “calc” value=”Click to start” onClick=”calculate()”>
<input type=”button” class=”buttonStyle” id = “taly” value=”Tally” onClick=”doTally()”>
<input type=”button” class=”buttonStyle” id = “del1″ value=”DelOne” onClick=”delOne(-1)”>
<input type=”button” class=”buttonStyle” value=”Reset” onClick=”doClear(0)”>[/code]

I’m at a loss to know what happened here.
The page is at https://www.wjporter.com/misc/irvtest.htm

Thanks in advance.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumNov 29.2018 —  Although all buttons are members of the same CSS class, only the ones I never change are still under CSS (turn red on hover).[/quote]The reason is that you obviously styled them by use of Javascript. Such styles and inline styles have a priority higher than selectors by class. This is well explained here:

https://medium.com/@dte/understanding-css-selector-specificity-a02238a02a59
Copy linkTweet thisAlerts:
@NogDogNov 29.2018 — Would making the :hover background assignment !important address that, or would the JS setting still override it? (Curious question from a non-expert CSS person ? )
Copy linkTweet thisAlerts:
@SempervivumNov 29.2018 — Obviously styling by Javascript is equivalent to inline styling and can be overridden by !important. A short test verifies this:

[upl-image-preview url=https://www.webdeveloper.com/forum/assets/files/2018-11-29/1543523057-621617-hover-red.png]

When taking this screeshot the mouse was over the Tally button.
Copy linkTweet thisAlerts:
@wbportauthorDec 05.2018 — Thank you, gentlemen. It's a lot to take in but I'll be giving it a shot.

I've updated the link with the !important ideas.

What browser were you using for that screenshot. I've tried IE, Firefox, and Chrome but couldn't reproduce that shot.
Copy linkTweet thisAlerts:
@SempervivumDec 05.2018 — What browser were you using for that screenshot[/quote]I used the developer tools of Opera (Strl+Shift+I). Chrome should look the same.
Copy linkTweet thisAlerts:
@wbportauthorDec 06.2018 — Thanks. The reference to specificity in the medium site was very useful as well.
Copy linkTweet thisAlerts:
@rootDec 07.2018 — Screen shots are a system call and not all systems have that ability to autosave.

In Microsoft you are meant to hit the screen shot buttons, open PAINT and then paste in to there and save it.

On my system, which is linux, I press screen shot button and I get a list of options, some are useful, I'd screen shot it but not worked out how to screen shot the screen shot window yet...

×

Success!

Help @wbport spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...