Hi,
I am trying to access a variable outside of an if statement. The variable is the id of a button obtained using an event listener.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Student of the Day</title>
<style>
body{
background-color: rgb(207,218,224);
}
div{
display: inline-block;
vertical-align: top;
border: 3px solid rgb(83,95,128);
border-radius: 6px;
}
table{
background-color: rgb(167,188,188);
border-radius: 3px;
}
button{
border: 3px solid rgb(83,95,128);
background: rgb(139,167,180);
border-radius: 6px;
}
</style>
</head>
<body>
<% 3 %>
<img src="http://latex.codecogs.com/gif.latex?\frac{1+sin(x)}{tan(x)}" border="0"/>
<div>
<table>
<tr>
<!-- <td>Sashasss</td><td><button id="student1up" onclick="move(this);">Up</button></td> -->
<td>Sashasss</td><td><button id="student1up">Up</button></td>
<td><button id="student1down">Down</button></td>
</tr>
<tr>
<td>Dasha</td><td><button id="student2up">Up</button></td>
<td><button id="student2down">Down</button></td>
</tr>
</table>
</div>
<script type="text/javascript">
thing = new Object();
document.body.addEventListener("click", function(event) {
if (event.target.nodeName == "BUTTON")
thing = {num: event.target.id};
console.log("Inside if: ", thing.num);
});
console.log("Outside if: ", thing.num);//why can I not read thing.id outside the if() statement?
</script>
</body>
</html>
In the console I am getting,
Outside if: undefined
I am guessing that this may be a scope issue. If so how can I solve it?
Thanks