I found this code :
<script>
document.onkeydown = function(e) { // or document.onkeypress
e = e || window.event;
if (e.keyCode == 32 || e.keyCode == 09) {
alert("do something");
}
};
</script>
It shows a pop up on spacebar or tab click. I don't want a pop up but the changing of the div.
I found another code which changes the div on click of text. Here is is:
<html>
<head>
<script type="text/javascript">
top.visible_div_id = 'right';
function toggle_visibility(id) {
var old_e = document.getElementById(top.visible_div_id);
var new_e = document.getElementById(id);
if(old_e) {
console.log('old', old_e, 'none');
old_e.style.display = 'none';
}
console.log('new', new_e, 'block');
new_e.style.display = 'block';
top.visible_div_id = id;
}
</script>
</head>
<body onload="toggle_visibility('left');">
<div onclick="toggle_visibility('left');">
Left
</div>
<div onclick="toggle_visibility('right');" >
Right
</div>
<div id="left" >
This is the content for the left side
</div>
<div id="right" >
This is the content for the ride side
</div>
-------
-------
</body>
</html>
I almost as if want to mix the codes, so they have the same effect but the text clicking replaced with keyboard click.
What I've tried is adding
<script>
document.onkeydown = function(e) { // or document.onkeypress
e = e || window.event;
if (e.keyCode == 32 || e.keyCode == 09) {
toggle_visibility('right');
}
};
</script>
but this only worked on the first time and didn't actually toggle it. I could make it work with two different characters but I only want the spacebar. Hope you can help me figure this out.