Change text on hover, then return to the previous text

I have a system of comments, and each comment has a button that normally displays the number of replies to it. I want that when a user hovers over the button, the text changes from "3 replies" to "Reply!", and then, when the mouse leaves the button, the text returns to "3 replies".

Because the number of replies varies for each comment, I cannot do a simple mouseover/mouseout script. One way to go around it would be to pass the number of replies as a variable, and make a small function to take care of it. But there must be a simpler way. I tried using the :hover thing in css to change the content of the tag (with the css property content), but no luck yet.

Any help appreciated, thanks!

Answers:

Answer

Yes, you can use CSS content. To switch between the normal text and "Reply!", put the normal text in a span and hide that when hovering.

HTML:

<button><span>3 replies</span></button>

CSS:

button {width:6em}
button:hover span {display:none}
button:hover:before {content:"Reply!"}

jsFiddle:

http://jsfiddle.net/4xcw7/2/

Edit: this works in IE8, but not in its compatibility mode, so I assume IE7 is out. Would that be a problem?

Answer

I think this would be a straightforward way to go for it. Use two span inside your button, one with content 'x replies', one with content 'Reply!'. Using CSS and :hover, you just switch which span is shown on hover.

HTML:

<button>
    <span class="replies">5 Replies</span>
    <span class="comment">Reply!</span>
</button>

CSS:

button .comment { display: none; }
button:hover .replies { display: none; }
button:hover .comment { display: inline; }

Check the JsFiddle example. This works just fine in about everything, as it uses very basic things to achieve an equally basic goal.

Answer

I would use a combination of jQuery .hover() and jQuery .data():

http://jsfiddle.net/5W4Bd/

HTML:

<div id="myDiv">default text</div>

javascript:

$('#myDiv')
    .data('textToggle', 5)
    .hover(function (e) {
        var that = $(this);

        // get the text from data attribute
        var textToSet = that.data('textToggle');

        // save the current text so it can be reverted
        that.data('textToggle', that.text());

        // set the new text
        that.text(textToSet);
    }, function (e) {
        var that = $(this);

        // get the text from data attribute
        var textToSet = that.data('textToggle');

        // save the current text so it can be reverted
        that.data('textToggle', that.text());

        // set the new text
        that.text(textToSet);
    });

edit: feel free to refactor the anonymous function used as the two callbacks to hover, since they are exactly the same :)

Answer
$('#button_id').hover(
    function(){
        $(this).text('Reply!');
    },
    function(){
        $.ajax({
            url: 'script.php',
            type: 'post',
            data: comment_id,
            success: function(num_replies){
                $('#button_id').text(num_replies + ' replies');
            }
        });
    }
);

I think you could use this kind of function, when you stop hovering, you feed the ajax call your comment id, and it returns the # of replies for that comment. You can decide how you want to store/retrieve your comment ID.

Answer

If anyone wants to try the same on menu links, (different language text on hover) here is the jsfiddle example

html:

<a align="center" href="#"><span>kannada</span></a>

css:

span {
    font-size:12px;
}
a {
    color:green;
}
a:hover span {
    display:none;
}
a:hover:before {
    color:red;
    font-size:24px;
    content:"?????";
}
Answer

Another way to do it, is to create two spans with the button, and have classes for each span.

<button>
  <span class="replies">3 Replies</span>
  <span class="comments"></span>
<button>

Leave the second one empty, and in css, write:

button{ 
  //your decorative code here }

button:hover .replies{
   display: none; }

button:hover .comments:before{
   content:"Reply!"; }

This is similar to first answer, but that answer doesn't always work with a preprocessor like SASS, or LESS.

Answer

As simple as it might be:

$('.controls button.filter').hover(function(){

    var original = $('#current_filter').text();
    var descr = $(this).attr("title");

    $('#current_filter').html( descr );

}, function() {

    $('#current_filter').text(original);
});
Answer

In modern browsers you could also use :after pseudo element for changing the text on hover.

.label             { width:5em }
.label:after       { content:'Hello'; }
.label:hover:after { content:'World'; }
<button class="label"></button>

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.