Forcing an A:active status on a link via javascirpt

N

newcomsas

Hello.
I'm trying to solve a problem related to JS and CSS.
I produced an HTML page with several links and buttuns.
I then created a CSS file in order to display a yellow background when
a link is clicked by users.

UL.menu1 LI A:active{
BACKGROUND: yellow; COLOR: black

Naturally, having acted on the A:active property, when a link is
clicked, all the others lose the yellow background.
The fact is that my page have to reload frequently. The effect I'd like
to obtain is that, when the page reloads, the link that was 'active'
before reloading continued to show the yellow background.
Having the chance of retrieving the link ID, I tried to force this
status using a simple js line

activeLinkID.click();

at the body onload event. The click was executed but the yellow
background did not appear.

Is there a way to force an 'active' status via JS on the link ?

I thank you in advance for any help.

Newcomsas
 
M

MB

Having the chance of retrieving the link ID, I tried to force this
status using a simple js line

activeLinkID.click();

at the body onload event. The click was executed but the yellow
background did not appear.

I would try:

document.getElementById(activeLinkID).style.backgroundColor = "#ffff00";
 
N

Newcomsas

Well, this was effectly the first solution I thought of. The fact is
that my page have several links; if you set directly the backgorund, it
remains shown even when you click on another link (so when the 'active'
status is passed to another <A HREF tag).
I frankly don't know how to 'tell' the first link to de-activate its
background when another is clicked :-(

Newcomsas




MB ha scritto:
 
M

MB

Newcomsas said:
Well, this was effectly the first solution I thought of. The fact is
that my page have several links; if you set directly the backgorund, it
remains shown even when you click on another link (so when the 'active'
status is passed to another <A HREF tag).
I frankly don't know how to 'tell' the first link to de-activate its
background when another is clicked :-(

Newcomsas

If I understand you correctly, a click on any of those links doesn't cause
the page to reload, but rather some javascript function is called, is that
right? Because then you can store the ID of the link you set the background
color for, in a variable and start by resetting the background color for the
previously clicked link:

if (lastClickedID != "")
document.getElementById(lastClickedID).style.backgroundColor = "";
document.getElementById(activeLinkID).style.backgroundColor = "#ffff00";
lastClickedID = activeLinkID;

Also put this somewhere in the head of your document:
lastClickedID = "";
Otherwise you will get an error when 'if (lastClickedID != "")' runs,
because it would not have been set yet.

That would make sure the link color is only yellow on the link you just
clicked and not on any other link. But this will only work as long as the
page is not reloaded.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top