1

I am trying to make a jquery text toggle. If you rollover the element, the text appears next to it and vice versa, it dissappears when you leave the element area.

My code does not work yet. Also, i would like to include different texts for different links. If there is a simpler way please suggest.

HTML

<div id="container"></div>
<a href="#" id="ovr">clickclickclick</a>

JS

$("#ovr").mouseenter(function() {
    $(#container).html("wazaap");
  }).mouseleave(function() {
    $(#container).html();
  });
2

3 Answers 3

1

You are forgetting the quotes in the jQuery selector:

$("#ovr").mouseenter(function() {
    $("#container").html("wazaap");
}).mouseleave(function() {
    $("#container").html("");
});

Edit

If you want different sentences, you can do this:

JS

var description=new Array();
description["one"]="Here comes the first";
description["two"]="And here the second";
description["three"]="Now let's have the third";
description["four"]="Finally the last one, fourth";
$("a.link").mouseenter(function(){
    $("span#description").text(description[$(this).attr("id")]);
}).mouseleave(function(){
    $("span#description").text("");
})​

HTML

<a href="#" class="link" id="one">one</a>
<a href="#" class="link" id="two">two</a>
<a href="#" class="link" id="three">three</a>
<a href="#" class="link" id="four">four</a>

<span id="description"></span>​

Check working here http://jsfiddle.net/Wpe2B/

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

2 Comments

Yes :D That are the different sentences i was talking about. I was thinking something like that. What am i doing wrong? jsfiddle.net/ssDT2/2
Sorry i couldn't hardly understand the code you just sent me. You didn't set the sentences, I don't know what you mean with $('.cText').text($(this.attr('id')));
1

Try to do it with hover() function. Code will be cleaner.

Basic example:

jQuery:

$("#container").hover(
function() {
  $('.cText').text("click");
},
function() {
    $('.cText').text("");
});

CSS:

#container {
      position: relative;
      background-color: #EEEEEE;
      width: 100px;
      height: 100px;
      position: relative;
      display: block;
  }

HTML:

div id="container"></div><span class="cText"></span>

Regards

Comments

1

Update

Base on OP's comments wanting to use several links to show text I tried this:

http://jsfiddle.net/cZCRh/4/

It doesn't quite work with a class because all links get the same text


This works http://jsfiddle.net/cZCRh/1/

<div id="container"></div>
<a href="#" id="ovr">clickclickclick</a>

$("#ovr").mouseenter(function() {
    $('#container").html("wazaap");
}).mouseleave(function() {
    $("#container").html("");
});
​

The problem was the mouseleave was in the wrong place as well as missing quotes around the element IDs

5 Comments

Is there a possibility to have different texts for different links or di o just have to use code as many times?
I have different icons (links). If you hover those icons, an icon description text shows up. Now i would like to know if there is a way to solve this with variables, or should i just multiply code as many times for all the icon IDs.
Well all of your links can have a class on them and instead of using the ElementID you can use the class name <div class="container"></div> and then $(".container").html("wazaap"); so that all links with that class call the same event
@user6613 I updated my answer with a test using a class opposed to each link having it's own individual customization... didn't quite work like I was thinking... have a look at the fiddle and see jsfiddle.net/cZCRh/4
Yes, but yours is interesting too, because it grabs text from the alt attribute, so i dont have to edit javascript code. I think im just gonna build a new code, using best solutions from all the answers. Thanks guys ;)

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.