Using javascript/jquery, what is the best way to update the src of a set images? -


i have bunch of images on page this:

<img src="/content/images/icons/theme1/title.png" class="slideexampleimage"> <img src="/content/images/icons/theme1/test.png" class="slideexampleimage"> <img src="/content/images/icons/theme1/bill.png" class="slideexampleimage"> 

as can see, of images have class="slideexampleimage" , of files in directory name of current theme.

i have folder different themes , want allow users change theme have radio buttons each theme name can assume can selected theme name local variable

var theme = getthemenamefromradiobutton(); 

am trying figure out best way allow user select different theme (lets "theme2") , result in images looking this

i have different themes , want way change theme

<img src="/content/images/icons/theme2/title.png" class="slideexampleimage"> <img src="/content/images/icons/theme2/test.png" class="slideexampleimage"> <img src="/content/images/icons/theme2/bill.png" class="slideexampleimage"> 

so somehow go in , update src , replace 1 subfolder under images (theme1 in case) new theme picked (theme2 in case)

what way achieve using jquery / javascript? note don't have control on theme name users can same theme name

function changetheme(newtheme) {   // change "src" attribute   $(".slideexampleimage").attr("src", function(_, oldsrc) {     // split path     var parts = oldsrc.split(/\//);     // replace "theme" part of path new 1     parts.splice(parts.length - 2, 1, newtheme);     // return/set new path     return parts.join("/");   }); } 

fiddle


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -