daveoffy Posted March 10, 2009 Share Posted March 10, 2009 I have a set of buttons, all in a div. The buttons are transparent over the div, so the div has the background I want to switch from. button and buttonhover are the divs. it is like this so far <div class="button"><img src="image.png"><img src="image1.png"></div> I want so when I hover that the class changes. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 10, 2009 Share Posted March 10, 2009 http://techpatterns.com/downloads/examples/class-switcher.htm this what you after ??? Quote Link to comment Share on other sites More sharing options...
daveoffy Posted March 10, 2009 Author Share Posted March 10, 2009 great, I will get to work on that code soon. Quote Link to comment Share on other sites More sharing options...
daveoffy Posted March 10, 2009 Author Share Posted March 10, 2009 *could not modify* Ok so, When I have an image in that div of the class, when I am over the image it wont switch. <div class="buttonbg"> <img src="../skin/images/editor.png" width="15px" height="15px" onclick="addText('editor', '<html>\n<head>\n<title></title>\n</head>\n<body>\n\n</body>\n</html>');"> </div> Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 11, 2009 Share Posted March 11, 2009 can you paste the function ??? Quote Link to comment Share on other sites More sharing options...
daveoffy Posted March 11, 2009 Author Share Posted March 11, 2009 the javascript <script type="text/javascript"> var classname = 'buttonbg'; var classname1 = 'buttonbg'; var classname2 = 'hoverbuttonbg'; var feature = 'single'; function getElementsByClassName(needle) { var my_array = document.getElementsByTagName("*"); var retvalue = new Array(); var i; var j; for (i=0,j=0;i<my_array.length;i++) { var c = " " + my_array[i].className + " "; if (c.indexOf(" " + needle + " ") != -1) retvalue[j++] = my_array[i]; } return retvalue; } function toggle() { var divs = getElementsByClassName(classname) for(i=0; i <divs.length;i++) { if(divs[i].className == classname1) { divs[i].className = classname2; classname = classname2; } else { divs[i].className = classname1; classname = classname1; } } } function rollon(e) { var srcId, srcElement, targetElement; if (window.event) e = window.event; srcElement = ( e.srcElement ) ? e.srcElement : e.target; if ( srcElement.className == classname1 ) { if ( feature == 'all' ) { toggle(); } else if ( feature == 'single' ) { srcElement.className = classname2; classname = classname2; } } } function rolloff(e) { var srcId, srcElement, targetElement; if (window.event) e = window.event; srcElement = ( e.srcElement ) ? e.srcElement : e.target; if ( srcElement.className == classname2 ) { if ( feature == 'all' ) { toggle(); } else if ( feature == 'single' ) { srcElement.className = classname1; classname = classname1; } } } document.onmouseover = rollon; document.onmouseout = rolloff; </script> Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 11, 2009 Share Posted March 11, 2009 use fulll stop character notations not [] also you are missing a semi colon on line 22 Quote Link to comment Share on other sites More sharing options...
daveoffy Posted March 11, 2009 Author Share Posted March 11, 2009 what is full stop character notations Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 11, 2009 Share Posted March 11, 2009 Dot-notation is most commonly used in JS to access properties of objects, which means that the property name is given after a full stop character. For example: Given: <form id="myForm" action="/"> <div><input name="foo"></div> </form> We can say: var myForm = document.getElementById("myForm"); var input = myForm.foo; Here we call the getElementById property of the document object as a function, and then we take the foo property of the form (which is the input with the name "foo"). Quote Link to comment Share on other sites More sharing options...
daveoffy Posted March 11, 2009 Author Share Posted March 11, 2009 That doesn't fix my problem. I am trying to get it so when the image is hovered also, it still changes the class. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 11, 2009 Share Posted March 11, 2009 also some of your if statements are missing curly brackets {} var classname = 'buttonbg'; var classname1 = 'buttonbg'; var classname2 = 'hoverbuttonbg'; var feature = 'single'; function getElementsByClassName(needle) { var my_array = document.getElementsByTagName("*"); var retvalue = new Array(); var i; var j; for (i=0,j=0;i<my_array.length;i++) { var c = " " + my_array[i].className + " "; if (c.indexOf(" " + needle + " ") != -1)retvalue[j++] = my_array[i]; } return retvalue; } function toggle() { var divs = getElementsByClassName(classname); for(i=0; i <divs.length;i++) { if(divs[i].className == classname1) { divs[i].className = classname2; classname = classname2; } else { divs[i].className = classname1; classname = classname1; } } } function rollon(e) { var srcId, srcElement, targetElement; if (window.event) e = window.event; srcElement = ( e.srcElement ) ? e.srcElement : e.target; if ( srcElement.className == classname1 ) { if ( feature == 'all' ) { toggle(); } else if ( feature == 'single' ) { srcElement.className = classname2; classname = classname2; } } } function rolloff(e) { var srcId, srcElement, targetElement; if (window.event) e = window.event; srcElement = ( e.srcElement ) ? e.srcElement : e.target; if ( srcElement.className == classname2 ) { if ( feature == 'all' ) { toggle(); } else if ( feature == 'single' ) { srcElement.className = classname1; classname = classname1; } } } document.onmouseover = rollon; document.onmouseout = rolloff; Quote Link to comment Share on other sites More sharing options...
daveoffy Posted March 11, 2009 Author Share Posted March 11, 2009 That doesn't fix my first problem. When I go over the image in the div tag, it wont change, but if I go over any other part of that div tag it works fine. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 12, 2009 Share Posted March 12, 2009 got a working example ??? Quote Link to comment Share on other sites More sharing options...
daveoffy Posted March 12, 2009 Author Share Posted March 12, 2009 http://webzava.com/editor.php is the page. The buttons on the top of the box, when you hover over the image, it does not change. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 13, 2009 Share Posted March 13, 2009 only the first image even loads ??? http://bonrouge.com/~imageswitch#n Quote Link to comment Share on other sites More sharing options...
daveoffy Posted March 13, 2009 Author Share Posted March 13, 2009 I didn't set the other images yet, not until I finish this javascript and get it working. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 13, 2009 Share Posted March 13, 2009 hope that link is what you need ??? Quote Link to comment Share on other sites More sharing options...
daveoffy Posted March 13, 2009 Author Share Posted March 13, 2009 Not really. I have the hover working, but when I hover over the image on that page, the class doesn't change. It says the same. I need to make it so everything in the class when hovered it will change the class also. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.