/    Sign up×
Community /Pin to ProfileBookmark

calculations unreversable?

Hi, first post here, and I’m probably being stupid, but oh well.

I have this extremely simple javascript, which works in one order but when reversed it stops.

This works.

<script type=”text/javascript”>
function setOpaque(){
document.getElementById(‘panelayer’).style.opacity+=document.getElementById(‘panelayer’).style.opacity-.1;
}
</script>
</head>
<body onclick=”setOpaque();”>
<div id=”panelayer” style=”opacity:1;”>
layer layer layer layer layer
</div>

This does not work.

<script type=”text/javascript”>
function setOpaque(){
document.getElementById(‘panelayer’).style.opacity+=document.getElementById(‘panelayer’).style.opacity+.1;
}
</script>
</head>
<body onclick=”setOpaque();”>
<div id=”panelayer” style=”opacity:0;”>
layer layer layer layer layer
</div>

The first one repeatedly decreases the opacity of the text each time you click on the background, while the second sets the opacity of the layer to 0.1 and never changes it after that.

By the bye, please disregard that the only method of changing opacity is through the opacity style, I was just testing this using the Safari browser, once I actually put it to use I will add the mozilla and IE compatable tags as well.

Thanks in advance…

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJun 09.2006 — The reason is because [b]opacity[/b] is a string -- not an actual number. The plus-sign is an overloaded operator. In other words, it performs a different function in different situations. With two numbers, the plus-sign operator will add the two arguments [U]arithmetically[/U]. If one of the arguments is a string, the plus-sign operator will add the two arguments [U]logically[/U] -- i.e., resulting is [I][U]concatenation[/U][/I] (not arithmetical addition).

The solution is to convert [b]opacity[/b] to a number before attempting your addition.
Copy linkTweet thisAlerts:
@fuzicauthorJun 09.2006 — wonderful, thanks a lot!
Copy linkTweet thisAlerts:
@phpnoviceJun 09.2006 — You're welcome.

Cheers.
×

Success!

Help @fuzic 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 5.7,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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