Hi guys, I'm trying to add / remove the border of an image when I check/uncheck a checkbox. here's my code: <html> <body> <script language="JavaScript" type="text/javascript"> function chnageborder(imageid) { document.getElementById(imageid).style.border = "solid 4px #000077"; } </script> <img id="Image1" src="myimage.png"><br/> <input type="checkbox" name="mycheckbox" onclick="chnageborder('Image1');"> </body> </html> Code (markup): With that code when i check the checkbox the border is added, but it will not remove the border when I uncheck it, and that's exactly what I'm trying to do. Any help with this would be greatly appreciated ! Thanks.
Try this <html> <body> <script language="JavaScript" type="text/javascript"> function chnageborder(imageid) { var img = document.getElementById(imageid); if (img.style.border == '') img.style.border = "solid 4px #000077"; else img.style.border = ''; } </script> <img id="Image1" src="myimage.png"><br/> <input type="checkbox" name="mycheckbox" onclick="chnageborder('Image1');"> </body> </html> Code (markup):
you can try this one.. <script type="text/javascript" > function changeborder(chk, imageid) { Â Â Â document.getElementById(imageid).style.border = (chk.checked)?"solid 4px #000077":"none"; }Â </script> <img id="Image1" src="myimage.png"><br/> <input type="checkbox" name="mycheckbox" onclick="changeborder(this,'Image1');"> HTML: Â
thanks ! both are working perfectly as I needed. now, my issue is that the border makes the image to be larger, and it will move the checkbox down and up when i add it and remove it so I was wondering if there's any way to add an inner border with javascript ? thanks
Give the image a transparent border, then you only need to change the color. <html> <head> <style type="text/css"> #Image1 { border: solid 4px transparent; } </style> <script language="JavaScript" type="text/javascript"> function changeborder(imageid) { var img = document.getElementById(imageid); if (img.style.borderColor == '') img.style.borderColor = "#000077"; else img.style.borderColor = ''; } </script> </head> <body> <img id="Image1" src="myimage.png"><br/> <input type="checkbox" name="mycheckbox" onclick="changeborder('Image1');"> </body> </html> Code (markup):