views:

47

answers:

4

Is there a way (possibly using Javascript?) of changing CSS element details when a User clicks an HTML link?

My aim here is to grey out a series of links defined as:

<a href="#" title="MyLink"><span>Link</span></a>

and a class defined as:

.Document
{
    background:#000;
}

What I am after is, when the User clicks MyLink, I would like the Document class to change its background to something else.... say #CCC. I would also like it to revert back to its original state when another link is selected e.g. MyLink2.

Is this even possible? If so does anyone know where to look for at least the beginnings of a solution?

+2  A: 
<a href="#" title="MyLink" onclick='document.body.style.background="#CCC";'>Link</a>
dejavu
+4  A: 

jQuery! - http://jquery.com/

$("your-selector").click(function(){
    $("your-destination").css("border-color","#CCC");
});

Apply for each link, and it should do it!

Tom
+1 For using jQuery and avoiding the `onclick` event.
derekerdmann
@derekerdmann: uh, it doesn't avoid the `onclick` event at all, it just adds it in a different way.
DisgruntledGoat
@DisgruntledGoat: What I meant is that it doesn't use use the `onclick` attribute in the HTML structure of the element.
derekerdmann
TK
@TK: You will need to include the jQuery library to use the code Tom posted. Either download it from jquery.com or simply add this before the above code: `<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>`
DisgruntledGoat
@DisgruntledGoat: Sorry I should have added that I already have added that within the <head> portion of my HTML page.
TK
Would `"background-color"` need to become `backgroundColor` (without quotes)? Now that I think about it, jQuery CSS might be camel-cased.
derekerdmann
@derekerdmann: Hmm... After a little bit of research, it really looks like it's changed.@TK: For that border-color applies the same I think, instead of `"border-color"` use `borderColor`, and same for every tag, which has dash as seperator. At least I think so, don't have much time to do tests at the moment.
Tom
Got it! the following appears to work as expected: '$("a[name='OCD']").css("border-color","#F00");' Thanks for all your help!
TK
Glad you got it working! And looks like both values are working for jQuery - `borderColor` and `"border-color"`. So, you can use camelCase and standard values.
Tom
+1  A: 

You could use the :focus CSS pseudo-selector:

a:focus {
  background-color: #ccc;
}

Now when the user clicks on a link, the background will go grey.

DisgruntledGoat
A: 

I assume the .Document classname is applies to a number of other elements & not the link itself.

In this case, the best practice is to create another classname (for example, .document-active), and change the classname on all the elements that .Document is applied to when MyLink is clicked.

Using your markup above (and jQuery):

$(function(){
  $("a[title='MyLink']").click(function(){
    $('.Document').removeClass('Document').addClass('document-active');
    return false;
  });
});
Jayphen