Solved

change the background color of a link using css

Posted on 2011-03-13
8
180 Views
Last Modified: 2012-05-11
Hi, I have a page with an iframe and links. When I click on the link the background of that link change, but when I click anywhere in the document the background changes back.
How can I keep the :active background until another link is clicked no matter what happens in the iframe or elsewhere in the document?

my links look like this:
<span><a class="index_vertical_links" href="javascript:jumpto('<?php echo "$main_output"; ?>')" >Page</a></span>
<span><a class="index_vertical_links" href="javascript:jumpto('<?php echo "$display_form"; ?>')" >some other page</a></span>
<span><a class="index_vertical_links" href="javascript:jumpto('<?php echo "$add_new?cat_add=1"; ?>')" >other page</a></span>

my css looks like this:
a.index_vertical_links:link,a.index_vertical_links:Visited
{
width:235px;
height:20px;
padding-left:20px;
float:left;
color:#ffffff;
background-color:#3f5d83;
border-right:1px solid #264266;
border-top:1px solid #3f5d83;
border-bottom:1px solid #3f5d83;
text-align:left;
text-decoration:none;
letter-spacing:1pt;
word-spacing:0pt;
font-weight:normal;
font-size:12px;
font-family:MS Sans Serif,Arial,sans-serif;
line-height:18px;
}

a.index_vertical_links:Hover
{
width:234px;
height:20px;
border-left:1px solid #264266;
float:left;
color:#e4ab13;
background-color:#787b6a;
border-right:1px solid #787b6a;
border-top:1px solid #264266;
border-bottom:1px solid #264266;
text-align:left;
text-decoration:none;
letter-spacing:1pt;
word-spacing:0pt;
font-weight:normal;
font-size:12px;
font-family:MS Sans Serif,Arial,sans-serif;
line-height:18px;
}

a.index_vertical_links:active,a.index_vertical_links:focus
{
width:235px;
height:20px;
padding-left:20px;
float:left;
color:#e4ab13;
background-color:#787b6a;
border-right:1px solid #787b6a;
border-top:1px solid #264266;
border-bottom:1px solid #264266;
text-align:left;
text-decoration:none;
letter-spacing:1pt;
word-spacing:0pt;
font-weight:normal;
font-size:12px;
font-family:MS Sans Serif,Arial,sans-serif;
line-height:18px;
}
0
Comment
Question by:ikon32
  • 5
  • 3
8 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 35124783
try this simple thing

a.index_vertical_links_visited
{
width:235px;
height:20px;
padding-left:20px;
float:left;
color:#ffffff;
background-color:#3f5d83;
border-right:1px solid #264266;
border-top:1px solid #3f5d83;
border-bottom:1px solid #3f5d83;
text-align:left;
text-decoration:none;
letter-spacing:1pt;
word-spacing:0pt;
font-weight:normal;
font-size:12px;
font-family:MS Sans Serif,Arial,sans-serif;
line-height:18px;
}

<span><a class="index_vertical_links" href="javascript:this.setAttribute('class','index_vertical_links_visited');jumpto('<?php echo "$main_output"; ?>')" >Page</a></span>
0
 

Author Comment

by:ikon32
ID: 35124872
thanks for the comment!  I tried and when I clicked on the link it rerurn this error:
Webpage error details

Message: Object doesn't support this property or method
Line: 1
Char: 1
Code: 0
URI:  index.php

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 35124934
remove the echo part and try once

<span><a class="index_vertical_links" href="javascript:this.setAttribute('class','index_vertical_links_visited');">Page</a></span>



0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:ikon32
ID: 35124955
same error
0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 35124972
ok, then try this solution

http://stackoverflow.com/questions/2490627/how-can-i-reliably-set-the-class-attr-w-javascript-on-ie-ff-chrome-etc

<span><a class="index_vertical_links" href="javascript:this.className = 'index_vertical_links_visited';">Page</a></span>
0
 

Author Comment

by:ikon32
ID: 35125034
tried this without error but again when i click anywhere the background is changing back.
<span><a class="index_vertical_links" href="javascript:this.className = 'index_vertical_links_visited';jumpto('<?php echo "$display_form"; ?>')">Page</a></span>

Going over to check that question, if you can think of anything else please post!  
0
 

Author Comment

by:ikon32
ID: 35125184
cant get it right... can it be done with just css?
0
 

Author Closing Comment

by:ikon32
ID: 35224228
thank you
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CONVERT JAVASCRIPT BUTTON TO HYPERLINK TEXT 6 25
how can i count words? 2 40
ASP.NET gridview select textbox on focus 2 33
Help with removing item from Session Array 3 22
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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 the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

813 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

14 Experts available now in Live!

Get 1:1 Help Now