I'm working on a calculator and have a mousedown and mouseup function on every id. Is there some way to clean that up and have a function called anytime a class element is clicked and be able to get the id that's been clicked inside the function?
here's a link to a myfiddle calculator
also, I noticed that when I placed my javascript directly in the JS box it didn't work, however as a script embedded in the html it works fine. Could someone explain why?
<tr>
<td class='noborder' colspan='1' rowspan='2'> </td>
<td id='1' class='buttons' colspan='4' rowspan='2' onmousedown="mouseDown(this.id)" onmouseup="mouseUp(this.id)" ;>1</td>
<td id='2' class='buttons' colspan='4' rowspan='2' onmousedown="mouseDown(this.id)" onmouseup="mouseUp(this.id)" ;>2</td>
<td id='3' class='buttons' colspan='4' rowspan='2' onmousedown="mouseDown(this.id)" onmouseup="mouseUp(this.id)" ;>3</td>
<td id='plus' class='buttons' colspan='4' onmousedown="mouseDown(this.id)" onmouseup="mouseUp(this.id)" ;>+</td>
<td class='noborder' colspan='1' rowspan='2'> </td>
</tr>
<script>
function mouseDown(id) {
document.getElementById(id).style.backgroundColor = "gray";
}
function mouseUp(id) {
document.getElementById(id).style.backgroundColor = "white";
}
</script>