strangerpixel
strangerpixel

Reputation: 828

Add variable to jQuery src attribute

I have a script that I reuse in lots of different places. The script has several image paths specified, and I want to make it easier to implement by setting one 'path' variable at the start then calling the variable in place of the full url. e.g.

jQuery(document).ready(function($){

var path = "http://www.mydomain.com/images/";

$(".menu img.arrow").click(function(){ 

 $(this).attr('src', path + 'arrow_hover.png'); 

    })                              

});

Any pointers?

Upvotes: 1

Views: 4775

Answers (1)

morgancodes
morgancodes

Reputation: 25265

You just want to have a globally-accessible variable that holds your base path? Simply moving

var path = "http://www.mydomain.com/images/";

outside your function will do that. I often create a "settings" object that holds all of that sort of stuff, so I don't use too many global variables.

Upvotes: 4

Related Questions