Augus
Augus

Reputation: 493

Changing image src with javascript with an variable in it

I made an template in HTML and now i am putting it in as an wordpress template. But in my .js file i got this:

    $('.tab_home').find('img').attr('src', '/images/home.png');

This is part of an click function and changes the image when i click on something. And it works fine in HTML but when i want to put this is in wordpress it is broken because the src url is wrong ofcourse. Now i did find this thread: WordPress path url in js script file and this would be part of my solution except that i can't put "+templateUrl+"in my .attr('src', '/images/home.png')Cause then i get an link like:

<img alt="" src=""+templateUrl+"/images/home.png">

Does some1 know what im doing wrong here, and how to fix it?

Thanks in forward.

Upvotes: 0

Views: 1066

Answers (1)

JohnB
JohnB

Reputation: 13743

You must do it as follows:

$('.tab_home').find('img').attr('src', templateUrl + '/images/home.png');

And don't forget to set templateUrl in the header of the template, as described in post you're referring to:

<script type="text/javascript">
var templateUrl = '<?= get_bloginfo("template_url"); ?>';
</script>

Upvotes: 2

Related Questions