0

I have a newbie question.

I am trying to display a background-image for an a-element. On mouseover the picture should change to different picture. The problem is that the pictures are not being displayed. When I had the code in CSS it worked fine so it must be my function that makes problems.

My code:

jQuery(document).ready(function($){
    $(document).ready(function() {
        $(".person").css({background-image: url('<?php echo get_stylesheet_directory_uri(); ?>/img/person.jpg')});
        $(".person").mouseover(function() {
            $(this).css({background-image: url('<?php echo get_stylesheet_directory_uri(); ?>/img/person-hover.jpg')});    
        });      
    });
    var body = $( 'body' );
}); 

HTML:

<div class="silhoulettes">
    <a class="person" href="<?php echo get_permalink(41); ?>"></a>
</div>

There are some php wordpress functions between.

Thank you very much in advance!

5
  • Is the PHP part processed at least (needs to be set inside a PHP file)? Check in browser what URL you get Commented Apr 12, 2016 at 10:01
  • So have you checked what the result is of get_stylesheet_directory_url(), and have you verified that the resulting url is correct? Commented Apr 12, 2016 at 10:01
  • Not related to the question, but to make thinks neater you can use shorthand php tags within HTML. <?=get_permalink(41);?> Commented Apr 12, 2016 at 10:02
  • You've need to put quotes around the value you're setting in the css() method. Check the console as you've probably got a syntax error Commented Apr 12, 2016 at 10:03
  • Yes, the url was correct. scottevans93 - thank you, very handy. Commented Apr 12, 2016 at 10:22

2 Answers 2

2

The correct way of giving css via jQuery is css("propertyname","value"); and css({"propertyname":"value","propertyname":"value",...}); for multiple style.

Try this

$(".person").css({"background-image"," url('<?php echo get_stylesheet_directory_uri(); ?>'/img/person.jpg")});
$(".person").mouseover(function() {
     $(this).css({"background-image", "url('<?php echo get_stylesheet_directory_uri(); ?>'/img/person-hover.jpg")});    
 }); 

Changes Made

$(".person").css({background-image: url('<?php echo get_stylesheet_directory_uri(); ?>/img/person.jpg')}); 

to

$(".person").css({"background-image"," url('<?php echo get_stylesheet_directory_uri(); ?>'/img/person.jpg")});
Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately I got an error: Uncaught SyntaxError: Unexpected token , I've read that I can use css in jquery like this to combine elements together: $('p').css({color:green,font-size:14px});
It works! You've made some mistakes with apostrophes. Thank you. Could you please comment on this part: $('p').css({color:green,font-size:14px});. Is it correct with jQuery?
Can you let me know where I can change the apostrophes ? So that I can improve my answer.
after the url in the brackets there shoud be ' instead of ", and after that closing bracket there should be "
@Dandy You have made a mistake. "is necessary while adding css
0

Try below syntax

var imgUrl = '<?php echo get_stylesheet_directory_uri(); ?>/img/person.jpg';

$(".person").css("background-image","url("+ imgUrl +")");

Comments

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.