Jump to content

Optimize code?


lindm

Recommended Posts

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.