0

I am building a WYSIWYG page styling editor with jquery/javascript. I'm trying to provide a way to modify the :hover state of links so my users can change the color, size, weight, etc of links. I know how to apply styling to elements for their default state, but can't find any documentation on how to apply styling to an element :hover state.

Any help out there?

To currently apply anything I am doing the following:

$('#content a').css('color','#ffcc00');

I need to do something similar for a:hover. ie:

$('#content a:hover').css('color','#000');
5
  • 1
    possible duplicate of How do you add pseudo classes to elements using jQuery? Commented Aug 21, 2013 at 19:45
  • How are you applying ANY style at the moment? Commented Aug 21, 2013 at 19:46
  • @putvande See my edit above. Just simple jquery css Commented Aug 21, 2013 at 19:48
  • Isn;t that already the answer? (The second line of code) Commented Aug 21, 2013 at 19:50
  • Maybe that will work with the event (on)hover for such. jsfiddle.net/pDuWN Commented Aug 21, 2013 at 19:52

2 Answers 2

1

If you want to make the change using javascript you can attach it to jQuery.hover(). Here's a full working example

$('a.link').hover(
       function(){

            $(this).css('color',$('input').val());
       }
     );

I built a WYSIWYG editor and I store the user defined settings in a db so instead of reloading the page after I save their change to the form I update the behavior with javascript.

Sign up to request clarification or add additional context in comments.

4 Comments

If you haven't looked at TinyMCE you should. It's an amazing WYSIWYG editor you can use in your projects.
Just have a go at your answer to see if it does the job. I'm aware of TinyMCE, but this is a bit more complex of a project. The question is a simplified version of the actual project (WYSIWYG template builder). storing the styling in the db was the plan :)
Hm, it's pretty damn close. Hover doesn't switch off. I really don't want to go down the class route as I'll be dynamically creating the CSS.
This is the closest answer. Couple of changes I did: 'a.link' to 'a' and added a second function(){} for when a user mouses out.
1

you can use css like assert your element´s id is

"element1"

css :

#element1:hover {
    background-color:pink;
}

1 Comment

@johnSmith Sorry, this is a CSS only approach. I've clarified the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.