dzash2000
asked on
Change an image when user holds down "Alt"
Hi -
When the user holds down the ALT key I want a CSS class I've named "Add" to change to the CSS class I've named "Delete" - and when the user releases the ALT key the class should change back to "Add".
These classes have background images that I hope will change with the key down and up events.
How can this be done with Javascript?
Thanks for your input.
d2
When the user holds down the ALT key I want a CSS class I've named "Add" to change to the CSS class I've named "Delete" - and when the user releases the ALT key the class should change back to "Add".
These classes have background images that I hope will change with the key down and up events.
How can this be done with Javascript?
Thanks for your input.
d2
HTML:
<a href="#" class="btnPair Add" ></a>
<a href="#" class="btnPair Delete" ></a>
CSS:
.btnpair.Add{
background: url("../_images/btnPair_Add.gif") no-repeat 0 0;
}
.btnpair.Delete{
background: url("../_images/btnPair_Delete.gif") no-repeat 0 0;
}
Try this. I'm not sure if it's working, because I don't have the images.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
.add{
background: url("../_images/btnPair_Add.gif") no-repeat 0 0;
}
.delete{
background: url("../_images/btnPair_Delete.gif") no-repeat 0 0;
}
</style>
<script type="text/javascript"><!--
document.getElementsByClassName = function(cl) {
var retnode = [];
var myclass = new RegExp('\\b'+cl+'\\b');
var elem = this.getElementsByTagName('*');
for (var i = 0; i < elem.length; i++) {
var classes = elem[i].className;
if (myclass.test(classes)) retnode.push(elem[i]);
}
return retnode;
};
// Capture Keyboard events
function KeyDown(e) { try {
var k = (window.event)?event.keyCode:e.keyCode;
if(k==18) {
e=document.getElementsByClassName("add");
for(i=0;i<e.length;i++){
e[i].ClassName="btnpair delete";
}
}
}catch(e){} }
function KeyUp(e) { try {
var k = (window.event)?event.keyCode:e.keyCode;
if(k==18) {
e=document.getElementsByClassName("delete");
for(i=0;i<e.length;i++){
e[i].ClassName="btnpair add";
}
}
}catch(e){} }
document.onkeydown = KeyDown;
document.onkeyup = KeyUp;
//--></script>
</head>
<body>
<a href="#" class="add"></a>
<a href="#" class="delete"></a>
</body>
</html>
ASKER
AsishRaj and sam2912
I could not make those solutions work but I appreciate your input.
2 things I should have done: 1: told you I'm pretty in the dark with JS and 2: provided the images.
Both omissions have been corrected here.
There is also expanded CSS to make sure these gifs show up on your screen when called with the html.
Thanks for your help.
btn-Delete-off.gif
I could not make those solutions work but I appreciate your input.
2 things I should have done: 1: told you I'm pretty in the dark with JS and 2: provided the images.
Both omissions have been corrected here.
There is also expanded CSS to make sure these gifs show up on your screen when called with the html.
Thanks for your help.
html -----------
<a href="#" class="add" ></a>
<a href="#" class="delete"></a>
css ----------------
.add{
background: url("_images/btn_Add_off.gif") no-repeat 0 0;
display: inline-block;
width: 54px;
height: 20px;
margin-top: -22px;
}
.delete{
background: url("_images/btn_Delete_off.gif") no-repeat 0 0;
display: inline-block;
width: 54px;
height: 20px;
margin-top: -22px;
}
btn-Add-off.gifbtn-Delete-off.gif
ASKER
HI - PLEASE note that the images I've uploaded were somehow renamed in transit from having underscores to hyphens between words.
*******my name:
btn_Add_off.gif
*******after uploading:
btn-Add-off.gif
Sorry for any confusion this has caused.
d2
*******my name:
btn_Add_off.gif
*******after uploading:
btn-Add-off.gif
Sorry for any confusion this has caused.
d2
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
sam2912 -
Thanks - I think I can work with that.
I can see that it will flip ANY button on the page with the classname of "add" or "delete". So if I want to target only one button I'll just name it and it's counterpart something else - - - like "zig" and "zag". Right?
Thanks again.
d2
Thanks - I think I can work with that.
I can see that it will flip ANY button on the page with the classname of "add" or "delete". So if I want to target only one button I'll just name it and it's counterpart something else - - - like "zig" and "zag". Right?
Thanks again.
d2
<a href="#" class="btnPair Delete" ></a>
add and id to these elements
Open in new window