Jump to content

changing link through javascript through css class


bleustreak12

Recommended Posts

i want to change the link of a menu item when another menu item is active in another menu

 

ie.suppose i have menu a with link home and menu b with link map

 

now if i am on the home page and the home link is active i want the color of the map link to change and maybe some other property of the link.

 

Now i want to change this not by using

 

<script type="text/JavaScript">

 

{

document.getElementById("theImage").style.height="100%";

document.getElementById("theImage").style.width="auto";

}

 

but by trying to access the class of the div(not the id)

 

 

how can i achieve this

Link to comment
Share on other sites

It's not as straightforward unfortunately (actually, there is a getElementsByClass function, but I don't think it's supported by all browsers).

 

var divs = document.getElementByTagName("div")
for(var i = 0; i < divs.length; i++)
{
  if(divs[i].getAttribute('class') == 'targetClassName')
  {
    a = divs[i].getElementsByTagName('a')
    a[0].setAttribute('href', 'target/href.html')
  }
}

 

(this will change the href of the first a tag in any div with a class name of "targetClassName" to target/href.html.

Link to comment
Share on other sites

I should add, you could use getElementsByClassName() for browsers that support it, and the code above for other browsers (IE):

 

if(getElementsByClassName)
{
  // use that function
}
else
{
  // use the code in the post above this one
}

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.