What's new
HTML Forums | An HTML and CSS Coding Community

Welcome to HTMLForums; home of web development discussion! Please sign in or register your free account to get involved. Once registered you will be able to connect with other members, send and receive private messages, reply to topics and create your very own. Our registration process is hassle-free and takes no time at all!

basic if statement dow not work

buzzersdad

New member
This works as shown, but if you include the "if" statement, it doesn't work. (I'm new at this, and have searched everywhere I know of to find what's wrong - it must be simple)

<!DOCTYPE html>
<html>
<body>
<style>
.box { width: 80px; height: 80px; margin: 0px; padding:0px; border: 1px solid black; position: absolute; }
</style>
<script>
function swap(id) {
var a=document.getElementById(id).getAttribute("value")
// if a == "x" {
document.write(a);
// }
}
</script>
<div class="box" onclick="swap('00')" id="00" value="x"
</div>
</body>
</html>
 
<div class="box" onclick="swap('00')" id="00" value="x"

I'm sure you've solved it by now, but you forgot the > on the other side of the div class
 
Could you help us with what you’re trying to achieve? Maybe there’s a better alternative?
 
What I was trying to achieve was more complicated, I just reduced the code bit by bit to the smallest number of statements to isolate where my code was failing. It started as an attempt to set up a toggle switch (using the "value" fiield as a 1 or a 0) to change a background image back and forth by clicking on its container. This required the "if" statement to determine if the value was a 1 or a 0.
 
What I was trying to achieve was more complicated, I just reduced the code bit by bit to the smallest number of statements to isolate where my code was failing. It started as an attempt to set up a toggle switch (using the "value" fiield as a 1 or a 0) to change a background image back and forth by clicking on its container. This required the "if" statement to determine if the value was a 1 or a 0.
Use a Boolean for the value. Booleans are either True or False. Where as integers can be many different options.
 
Back
Top