/    Sign up×
Community /Pin to ProfileBookmark

JavaScript If/Else Statement – unexpected result

Hi there, first time posting so please forgive me if I break etiquette 🙂

I’m playing around with making a circle appear and disappear when it’s clicked – I wrote an If/Else statement to see if I’d got the first step right and I obviously haven’t because something isn’t working! I’m not getting any errors in the console though – it’s just saying No when I’m expecting it to say Yes.

Can anyone tell me what I’ve done wrong? I’m sure it’ll be glaringly obvious to everyone else but I just cannot see it!

Thanks!

[code]
<html>

<head>

<title>Javascript</title>

<style type=”text/css”>

.circle {
height: 100px;
width: 100px;
border-radius: 50%;
border-width: 0px;
display: inline-block;
background-color: purple;
opacity: 1;
}

</style>

</head>

<body>

<div id=”circle1″ class=”circle”></div>

<script type=”text/javascript”>

document.getElementById(“circle1”).onclick = function() {
if (document.getElementById(“circle1”).style.opacity == 1) {
alert(“Yes”);
}
else {
alert(“No”);
}
}

</script>
</body>
</html>
[/code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@peterdillonSep 18.2018 — Because opacity cannot be read by the property name itself (unless opacity is applied inline.) You need to use "getComputedStyle()" and "getPropertyValue()"

https://codepen.io/peterdillon/pen/yxQJLL

I do notice if you change the opacity without a full page refresh - the computedStyle doesn't change, so you'd have to force a redraw.
Copy linkTweet thisAlerts:
@anjanakumari888135Sep 18.2018 — Good Forum! You can share accounting outsourcing related problems to experts when you communicate with the professional’s team. At any time, you can get quality services from them via Bookkeeping toll-free number +38-0443-577-6 47/ +38-0967-478-575. For more services, you can click on the link: -https://accounting-outsourcing.com.ua/accounting-services-in-ukraine
Copy linkTweet thisAlerts:
@cootheadSep 18.2018 — Hi there redheadstandingby,

this problem can be solved without _JavaScript_. ?

The follow code shows how, and also gives the correct

_JavaScript_ , if you choose to ignore the _CSS_ method...

``<i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;html lang="en"&gt;
&lt;head&gt;

&lt;meta charset="utf-8"&gt;
&lt;meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"&gt;

&lt;title&gt;untitled document&lt;/title&gt;

&lt;!--&lt;link rel="stylesheet" href="screen.css" media="screen"&gt;--&gt;

&lt;style media="screen"&gt;
body {
background-color: #f9f9f9;
font: 100% / 162% verdana, arial, helvetica, sans-serif;
}

#circle0, #circle1 {
height: 6.25em;
width: 6.25em;
border-radius: 50%;
display: inline-block;
background-color: #800080;
cursor: pointer;
transition: 1s ease-in-out;
}
#cb {
position: absolute;
left: -999em;
}

#circle0 {
background-color: #f00;
}

#cb:checked ~ #circle0 {
opacity: 0.2;
}

.state {
opacity: 0.2;
}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;input type="checkbox" id="cb"&gt;

&lt;label id="circle0" for="cb"&gt;&lt;/label&gt;

&lt;div id="circle1"&gt;&lt;/div&gt;

&lt;script&gt;
(function( d ) {
'use strict';
var el = d.getElementById( 'circle1' ),
flag = true;

el.addEventListener( 'click',
function() {
if ( flag === true ) {
el.classList.add( 'state' );
flag = false;
}
else {
el.classList.remove( 'state' );
flag = true;
}
}, false );
}( document ));
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;<i>
</i>
``


_coothead_
Copy linkTweet thisAlerts:
@SempervivumSep 18.2018 — making a circle appear and disappear when it's clicked[/quote]
My proposal:

http://jsfiddle.net/Sempervivum/4gr17tzf/5/
Copy linkTweet thisAlerts:
@peterdillonSep 18.2018 — The question was why his snippet behaved the way it does. My initial response/pen answers that question (getComputedStyle.) I updated the pen to force a redraw (with offsetheight) so that if you go in and change css (manually or with JavaScript, the alert will give the correct response. Thank you!
Copy linkTweet thisAlerts:
@redheadstandingbyauthorSep 19.2018 — Thanks everyone! I'm very new to all this so that's lots to take in but I'll look through it all :-)
×

Success!

Help @redheadstandingby 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.10,
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,
)...