g3rriascn
g3rriascn

Reputation: 35

Get image url using jquery

I'm having problem how to get image url using jquery. I have custom.js inside my js folder and i want to get the image from img folder.. By the way I'm using wordpress. Maybe someone can help me out?.

var $old_img = '../<?php bloginfo('stylesheet_directory');?>'+'/img/qs_slogan_down.png'; 

Upvotes: 0

Views: 535

Answers (1)

rdgd
rdgd

Reputation: 1446

So I am assuming that your "img" directory is in the root of your wordpress theme? So for instance if your theme name is "my custom theme" the path might look something like...

public_html/wp-content/themes/my custom theme/img/qs_slogan_down.png

If that is indeed the case, then I would just assign the variable to the correct path, and you would do that thusly: '<?php bloginfo('template_directory');?>/img/qs_slogan_down.png'.

Template directory refers to the your theme folder inside of wp-content.

That being said, you are still only storing a string in your variable, which you would want to assign to the src attribute of an image.

EDIT:

I think I understand your dilemma. You want a usable filepath, and if you are trying to get that in a function that gets triggered after the document load, then you aren't going to receive any information from the server because the information has already been served.

So you either need to use AJAX (probably a bit too complicated, but good to learn about), or you can do something like the following:

In your .php document, use the bloginfo('template_directory') function to call and store the data you need on the page (as some attribute of some element) during the document load, and then pass it to your function as a parameter, and assign your variable to the corresponding parameter so that you have the explicit file path ready to use.

Hope that helps! :-)

Upvotes: 2

Related Questions