0

I have a list of friends, (BOB, TINA, LISA, TERRY, MIKE) and each listed person has a picture identifying them on hover. When you click on that person their image stays on the screen, until you click a different person. Currently I am getting the same persons picture on click regardless the name I select.

How to I create a dynamic click function in js? When I click on a listed friend, I would like the click function to recognize which person I selected and display the image associated with that person.

Can someone please help me?

CSS

.BOB,.TINA,.LISA,.TERRY,.MIKE { background: url("../theme/images/blank.png") no-repeat 0 0; }

.BOB:hover { background: url("../theme/images/BOB.png") no-repeat 0 0; }
.TINA:hover { background: url("../theme/images/TINA.png") no-repeat 0 0; }
.LISA:hover { background: url("../theme/images/LISA.png") no-repeat 0 0; }
.TERRY:hover { background: url("../theme/images/TERRY.png") no-repeat 0 0; }
.MIKE:hover { background: url("../theme/images/MIKE.png") no-repeat 0 0; }

.highlight { background: url("../theme/images/BOB.png") no-repeat 0 0; display: block;}

JAVASCRIPT

$(document).ready(function(){
$('a').click(function(){
    $(".highlight").removeClass('highlight')
    $(this).addClass('highlight');
});

2 Answers 2

1
  1. Your $(document).ready() function is not closed properly.
  2. Your $("a").click() function is not preventing the default link action (this is only a concern if you have a href attribute defined for the link).
  3. Your .highlight class has a single background image, which is identical to that of your first person's background image. This is causing you to see the same background image for each one.

You can do this to fix number 3:

$("a").click(function(e){
    e.preventDefault(); // prevent link from directing to a different url
    $(this)
        .parent() // assuming the link is in the div you want to "highlight"
        .css({ // modify parent's css
            'background':
                'url(../theme/images/' + $(this).attr("class") + '.png no-repeat 0 0',
                // take class name (bob, tina, etc) and fetch image accordingly
            'display': 'block'
        });
});
Sign up to request clarification or add additional context in comments.

Comments

0

The .highlight class is using he same image always.

Can you post the html code too? It is difficult to find out what's happening without seeing your html code.

7 Comments

here is a link to my issue. When you hover over one of the ratings, you will see the hover effect. I removed the highlight code since it was problematic and useless. I am trying to get that rating to stay when clicked. thanks for the help. [link]cuuzo.com/language.html
I copied your code into jsfiddle and modified it. Here is the link to it. jsfiddle.net/c9Fxg Let me know if this is what you are looking for.
It's a lot better than what I originally had. But, only the selected should be highlighted at a time. Right now the jsfiddle never deselects the item when another is selected.
absolutely perfect! thank you so much. I had been struggling with this for a few days literally.
Just updated the fiddle jsfiddle.net/c9Fxg/3, as it was not showing your target image. Glad I could help :)
|

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.