views:

41

answers:

4

My goal: Mousehover a link, have a certain element on the page change the .class:before content to something from the link title.

Therefore:

I want to change the content of a .class:before, namely .test:before. However,

$("#test:before").css("content","whatever the content should be");

does not do the job. How do I adress :before classes?

Alternatively, I could use .before(); - Yet, I don't know how to unset .before(); once my mouse is off the link. Might be an alternative solution?

Looked up the jquery documentation - cant find the solution - probably due to my limited jquery knowledge ;)

Help appreciated!

+2  A: 

Interesting question.

$(x) is a selector...you're looking for a particular DOM object. But a CSS pseudo class isn't a DOM object. It's just a declaration in the CSS.

I'm not sure you CAN directly manipulate this via jQuery.

What you could do, though, is just apply a new class via jQuery:

$("#test").hover(function(){$(this).addClass('hover')});

Then in your CSS:

#test.hover:before

Which would then over-ride your default #test:before

DA
+2  A: 

I'm not sure about the approach you're using (as I've never used the :before pseudo class with jQuery manipulation) but this uses the .hover() function and does pretty much the same thing.

$(function(){
    //once the page is ready

    //bind some hover events
    $("#test").hover(
        //hover in
        function(){
            $(this).before("<span id = 'test-before'>Before Content here</span>");
            //insert some content before "this"
        },

        //hover out
        function(){
            $("#test-before").remove();
            //remove the content we added
        }
    );

});

Something like this might do what you're looking for.

Austin Fitzpatrick
this is how I should have done it! See how I did it down there - and it works, as well.
Simon Schmincke
A: 

How about the prev() function? Maybe this would work?

$("#test").prev().css("content","whatever the content should be");
Rocket
Never mind, I seem to have misunderstood the question.
Rocket
A: 

Austin: awesome!

I went nearly the same way:

this.tooltip_link = function(){ 

    $("a.tc_top_link_post").hover(function(e){                                            
        $("#acound_tc_lastfor")
            .css("background-color","#a40000")
            .css("color","white")
            .css("font-weight","bold")
            .css("padding-right","2px")
            .css("padding-left","2px");

        $("#tc_lastfor").replaceWith("<A href='#' id='tc_lastfor'>"+ this.title +"</a>");   
        $("#tc_lastfor").css("color","white");



    },

    function(){

        $("#acound_tc_lastfor")
            .css("background-color","white")
            .css("border-bottom","none")
            .css("font-weight","normal")
            .css("padding-left","0px");

        $("#tc_lastfor").replaceWith("<A href='#' id='tc_lastfor'>LAST 4 MONTHS:</a>");

   });

};

Thanks to the others!

Simon Schmincke