Solved

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

Posted on 2007-03-27
10
381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
need help with share buttons 11 71
Html date format 9 20
Calculating percentage 2 30
Prevent certain words from being typed in a form 6 30
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

733 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