December 20, 201114 yr So I tried to make a bunch of buttons to change the background of another div the javacript code I used was HTML <div id="btn1" onMouseClick="change()" ></div> <div id="btn2" onMouseClick="change()" ></div> <div id="btn3" onMouseClick="change()" ></div> CSS .div{ background-image:URL(.../images/image.jpg); } javascript <script language="JavaScript"> function change(){ document.getElementById("btn1").style.backgroundImage = "URL:.../images/img2.jpg"; document.getElementById("btn2").style.backgroundImage = "URL:.../images/img3.jpg"; document.getElementById("btn3").style.backgroundImage = "URL:.../images/img3.jpg"; } </script> not working...at all, might be due to lack of caffeine and sleep. if anyone has a solution I'd be greatful note: I tried substituting the document.getElementById("btn1") for document.getElementById("div") not working Edited December 20, 201114 yr by D.Schuster
December 20, 201114 yr Inline event handlers really aren't kosher, and it would be much cleaner and easier to achieve with jQuery using click() and css().
December 20, 201114 yr Inline event handlers really aren't kosher, and it would be much cleaner and easier to achieve with jQuery using click() and css(). Not for a 3 line script. Why include 50kb of plain text and get the JS engine to read it all, and only do one thing with it? Hardly cleaner and easier. The code should work like this: document.getElementById("btn1").style.backgroundImage = ".../images/img2.jpg"; Without the URL bit, also at the top, <script type="text/javascript"> will probably help you out too. Edited December 20, 201114 yr by Georgew
Create an account or sign in to comment