0

I'm trying to including a css "left" property to my javascript code such that if the left property in the css file matches the one in the javascript code, a certain action takes place. Any help will be appreciated. The HTML code is:

<!DOCTYPE HTML>
<html>
<head></head>
<body>

<button id = "myBtn">MyButton<button>

<p id= "demo"></p>

</body>
</html>

The CSS code is:

#myBtn {
   position: absolute;
   left: 100px;
}

The script code is:

<script>
if (document.getElementById("myBtn").style.left == "100px") {
   actionX //this is just an example
   }
document.getElementById("demo").innerHTML = actionX;
</script>
2

1 Answer 1

0

Well you're setting the property value with the = operator, you want to use the == operator. In some rare unusual cases which I can't seem to think of, you'd do an assignment in an if statement, but setting a property of an object will return a truthy value in JS, so the branch will always be reached.

if (document.getElementById("demo").style.left == "100px") {
    actionX (just an example) 
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.