lindm Posted August 10, 2008 Share Posted August 10, 2008 Got a small piece of code for a tooltip popup window: <a class="h" onmouseover="Tip(t17)" onmouseout="UnTip()">(?)</a> This snip occurs many times on the page with the variable t1 to t100. Is there a way to make the code smaller (save space)? I have been thinking of a few options: 1. Only using one event handler. Perhaps some type of onchange event... 2. Using css to handle the tooltip. Is it possible to call a javascript within css (hover: script(); or something...) Any help appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/119021-optimize-code/ Share on other sites More sharing options...
r-it Posted August 12, 2008 Share Posted August 12, 2008 why don't you set your javascript (the one that handles the tips) like this: function Tip(a) { //and a is a number var val = 't'+a; } this means you will be passing the number instead of having many lines of code Quote Link to comment https://forums.phpfreaks.com/topic/119021-optimize-code/#findComment-614424 Share on other sites More sharing options...
lemmin Posted August 13, 2008 Share Posted August 13, 2008 You can't use javascript in CSS, but you can dynamically set all the events when the page loads: <html> <head> <script type="text/javascript"> function initPage() { var as = document.getElementsByTagName('a'); for (var i=0;i<as.length;i++) { as[i].onmouseover = function() { //tip code } as[i].onmouseover = function() { //kill tip } } } </script> </head> <body onload="initPage()"> <a>test</a><br> <a>test2</a><br> <a>test3</a><br> <a>test4</a><br> <a>test5</a><br> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/119021-optimize-code/#findComment-615922 Share on other sites More sharing options...
TheFilmGod Posted August 13, 2008 Share Posted August 13, 2008 In simple terms, you create the javascript to set a mouse hover and off state from a javascript loop that grabs the appropriate code from the DOM. There is no need to specify it right in the html. Quote Link to comment https://forums.phpfreaks.com/topic/119021-optimize-code/#findComment-616022 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.