2
$(function(){
    var one = 'twitter';
    var two = 'about';
    var three = 'contact';
    var four = 'exp';
    var five = 'price';

    $('#twitter').hover(function(){
        $(this).append('<p class="hvtext" id="twitterz">'+ one + '</p>');
        $('.hvtext').css({left:'1.7em',
                          bottom: '0.1em'});

    }, function(){
        $('.hvtext').hide();
    });
    $('#about').hover(function(){
        $(this).append('<p class="hvtext">' + two + '</p>');
        $('.hvtext').css({left:'1.7em', bottom:'0.1em'});
    }, function(){
        $('.hvtext').hide();
    });
    $('#contact').hover(function(){
        $(this).append('<p class="hvtext">'+ three + '</p>')
        $('.hvtext').css({left:'1.2em', bottom:'0.1em'});
    }, function(){
        $('.hvtext').hide();
    });
    $('#experience').hover(function(){
        $(this).append('<p class="hvtext">'+ four + '</p>');
        $('.hvtext').css({left:'2.3em', bottom:'0.1em'});
    }, function(){
        $('.hvtext').hide();
    });
    $('#prices').hover(function(){
        $(this).append('<p class="hvtext">'+ five + '</p>');
        $('.hvtext').css({left:'2em',bottom:'0.1em'});
    }, function(){
        $('.hvtext').hide();
    });

    one.click(function(){
        alert('element exists');
    });



});

I'm trying to find a way of calling the .hvtext class as a selector which will be later used as an on click event.

Problem is, the class is added dynamically by .append, i tried using .is(':visible') and couldn't find it?

Another problem: since it's a class I also want to identify them individually, should i just use Id's?

To solve this I tried to distinguish them by adding a var for each of the texts hoping I could call them later as an event, but since they're just variable strings and not an element I can't.

Is there anyway I can achieve this?

7
  • 3
    why can't people use carriage returns...... Commented Dec 13, 2013 at 11:57
  • woops, thanks liam :D Commented Dec 13, 2013 at 11:59
  • I don't understand this part: "I'm trying to find a way of calling the .hvtext" Where in your code are you stuck? I see many $('.hvtext') so please clarify your question. BTW, if it's only for styling, then use CSS rules Commented Dec 13, 2013 at 12:00
  • In your specific example, you are setting "one" as a string, which has no click binding. If your actual code reflects this same error, then you should actually use $('#' + one).on('click', function() {/* code */}); Commented Dec 13, 2013 at 12:02
  • Is there any reason you can't write the .hvtext element in your markup and only select/change it in js? Commented Dec 13, 2013 at 12:02

1 Answer 1

1

Problem is, the class is added dynamically by .append

If it's added dynamically you need to use .on:

$('#prices').on('click', '.hvtext', function() {/*click stuff here*/});

since it's a class I also want to identify them individually, should i just use Id's?

why? this is where $(this) comes in. If you need to pass data I'd do something like:

$('#experience').hover(function(){
    var newHvText = $('<p class="hvtext">'+ four + '</p>');
    newHvText.data('leftEm', '2em');
    $(this).append(newHvText);
}, function(){
    $('.hvtext').hide();
}););

then

$('#prices').on('click', '.hvtext', function() {/*click stuff here*/
       var thisIsTheClickedElement =  $(this);
       var leftEm = thisIsTheClickedElement.data('leftEm');
       $('.hvtext').css({left:leftEm});
});

You seem to be trying to add a tooltip here, have you considered the http://jqueryui.com/tooltip/ instead?

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

1 Comment

Added some more detail as it seems you need to adjust the left and bottom depending on which is hovered.

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.