Solved

change link style onmousedown, revert to previous style onmousedown of new link

Posted on 2007-03-27
10
366 Views
Last Modified: 2013-11-19
In the menu below, I would like the style of each link to change to italics onmousedown. I know I can implement this by using onmousedown="this.style.fontStyle=italic", but once a client clicks on another link the menu, how do I revert the link that was previous clicked to normal whilst the most recently clicked link switches to italics? The menu is constant. I don't move to new pages, I just load new content into a layer. Here's the menu code:
            <p>
                <a href="javascript:void(0)" class="menu"
                onmousedown="pageManager('load_page','content/page1.html')">
                Developer</a>&nbsp;&nbsp;
            <a href="javascript:void(0)" class="menu"
                onmousedown="pageManager('load_page','content/page2.html');>Current Project</a>&nbsp;&nbsp;
            <a href="javascript:void(0)" class="menu"
                onmousedown="pageManager('load_page','content/page3.html')">Conferences Db</a></p>
0
Comment
Question by:GessWurker
  • 6
  • 4
10 Comments
 
LVL 7

Expert Comment

by:RealSnaD
ID: 18801177
What you can do is put an id on each link.

<p>
   <a id="link1" href="javascript:void(0)" class="menu"
       onmousedown="pageManager('load_page','content/page1.html')">
        Developer</a>&nbsp;&nbsp;
   <a id="link2" href="javascript:void(0)" class="menu"
       onmousedown="pageManager('load_page','content/page2.html');>Current             Project</a>&nbsp;&nbsp;
   <a id="link3" href="javascript:void(0)" class="menu"
onmousedown="pageManager('load_page','content/page3.html')">Conferences Db</a></p>
0
 
LVL 7

Expert Comment

by:RealSnaD
ID: 18801205
Oh sorry, accidentally clicked submit.

Ok so you put id on each link as shown above and then in your pageManager function  at the top do the following:

var i;
var howManyLinks = 3;
for(i = 1; i < (howManyLinks - 1); i++) {
   document.getElementById("link" + i).style.fontStyle = "normal";
}

0
 
LVL 7

Expert Comment

by:RealSnaD
ID: 18801238
Sorry, I mean:

var howManyLinks = 3;
for(var i = 1; i < (howManyLinks + 1); i++) {
   document.getElementById("link" + i).style.fontStyle = "normal";
}
0
 

Author Comment

by:GessWurker
ID: 18801658
RealSnaD: It seems your code has the effect of changing all the links to normal, even the one that was just clicked. I want the one that was just clicked to remain italicized.
0
 
LVL 7

Expert Comment

by:RealSnaD
ID: 18801710
Take a look at this code. It does what you want:

<script language="JavaScript">
      function pageManager(obj) {
            var howManyLinks = 3;
            for(var i = 1; i < (howManyLinks + 1); i++) {
               document.getElementById("link" + i).style.fontStyle = "normal";
            }
            obj.style.fontStyle = "italic";
      }
</script>
<p>
      <a id="link1" href="javascript:void(0)" class="menu"
      onmousedown="pageManager(this)">Developer</a>&nbsp;&nbsp;
      <a id="link2" href="javascript:void(0)" class="menu"
      onmousedown="pageManager(this)">Current Project</a>&nbsp;&nbsp;
      <a id="link3" href="javascript:void(0)" class="menu"
      onmousedown="pageManager(this)">Conferences Db</a>
</p>

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:GessWurker
ID: 18801801
No. doesn't work in my context. Here's the pagemanager code with your modifications. Unfortunately, the modifications make the code, which is also run onload, fail.

Here's the broken code:
function pageManager(obj) {
  var args = pageManager.arguments;
  switch(args[0]) {
    case "load_page" :
      content.top = (w3dom) ? 60  + "px": 60;
      if(ie4) {
        document.frames["frameData"].document.location = args[1];
      }
      if(w3dom) {
        document.getElementById("frameData").src = args[1];
      }
      if(ns4) {
        content.load(args[1],page.width);
      }
    break;
    case "page_is_loaded" :
      if(ns4) {
        sizeAnimator(content,page.width,content.htm.height);
      }
      clearTimeout(page_timer);
      page_timer = setTimeout("pageManager('display_page')",300);
    break;
    case "display_page" :
      if(ie4) {
        content.htm.innerHTML = document.frames['frameData'].document.body.innerHTML;
      } else if(w3dom) {
        content.htm.innerHTML = window.frames.frameData.window.document.getElementById('body').innerHTML;
      }
    break;
  }
var howManyLinks = 3;
for(var i = 1; i < (howManyLinks + 1); i++) {
   document.getElementById("link" + i).style.fontStyle = "normal";
}
obj.style.fontStyle = "italic";
}

Here's a sample page: that uses the code:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   
<head>
<title> Dynamic HTML External Loading </title>
 <script type="text/javascript" src="overlib/overlib.js"><!-- overLIB (c) Erik Bosrup --></script>
<script type="text/javascript" src="externalLoader.js">
</script>
<link rel="stylesheet" type="text/css" href="content/example4.css" />
<meta http-equiv="imagetoolbar" content="no" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
    <body onload="defineObjects()">
    <div id="overDiv" style="position:absolute; visibility:hidden; z-index:2;"></div>
        <div id="contentLYR" style="text-align:center"></div>
        <iframe frameborder="0" width="100" height="100" id="frameData" name="frameData" scrolling="No" src=""></iframe>
        <div id="Layer1" class="c1" style="text-align:center">
            <p>
                <a id="link1" href="javascript:void(0)" class="menu"
                onmousedown="pageManager('load_page','content/page1.html')">
                Tom - Developer</a>&nbsp;&nbsp;<a  id="link2" href="javascript:void(0)" class="menu"
                onmousedown="pageManager('load_page','content/page2.html');this.style.fontStyle='italic'">Current Project</a>&nbsp;&nbsp;
            <a id="link3" href="javascript:void(0)" class="menu"
                onmousedown="pageManager('load_page','content/page3.html')">Conferences Db</a>&nbsp;&nbsp;
            <a id="link4" href="javascript:void(0)" class="menu"
                onmousedown="pageManager('load_page','content/page4.html')">Intranet Catalog</a>
                        </p>
    </div>
    </body>
</html>
0
 

Author Comment

by:GessWurker
ID: 18801983
Then I tried adding a separate function

function italicizeMe(obj){
var howManyLinks = 4;
for(var i = 1; i < (howManyLinks + 1); i++) {
   document.getElementById("link" + i).style.fontStyle = "normal";
}
obj.style.fontStyle = "italic";
}

and altering adding an onmousedown event like so:

onmousedown="pageManager('load_page','content/page4.html');italicizeMe(obj)

No luck. Didn't work
0
 
LVL 7

Expert Comment

by:RealSnaD
ID: 18802002
First of all, don't forget to change

var howManyLinks = 3;

to the amount of links you have. In the example you posted it would be 4:


var howManyLinks = 4;

Secondly, function call should look like this for each link (you can change the parameters but don't touch the last one which is THIS):

onmousedown="pageManager('load_page','content/page1.html', this)"

Finally, change

function pageManager(obj) {

to whatever you had before but add obj as the last parameter.

Then it will work.
0
 
LVL 7

Accepted Solution

by:
RealSnaD earned 250 total points
ID: 18802013
As a separate function you almost right:

Change

onmousedown="pageManager('load_page','content/page4.html');italicizeMe(obj)"

to

onmousedown = "pageManager('load_page','content/page4.html');italicizeMe(this)"
0
 

Author Comment

by:GessWurker
ID: 18802033
Eureka! Thanks. Points to you!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now