jquery - Find and replace within attribute values -
i have html code:
<div class='images'> <img class='thumbnail' src='background/01.png' /> <img class='thumbnail' src='background/02.png' /> <img class='thumbnail' src='background/03.png' /> <img class='thumbnail' src='background/04.png' /> <img class='thumbnail' src='background/05.png' /> </div>
i looking change every "background" "category". using jquery snippet:
jquery(document).ready(function(){ $('.thumbnail').attr('src', $('.thumbnail').attr('src').replace('background', 'category')); });
but result :
<div class='images'> <img class='thumbnail' src='category/01.png' /> <img class='thumbnail' src='category/01.png' /> <img class='thumbnail' src='category/01.png' /> <img class='thumbnail' src='category/01.png' /> <img class='thumbnail' src='category/01.png' /> </div>
please note : can't change img class. going wrong here?
thanks
firstly note it's better practice use prop()
this. secondly, can pass prop()
function handle each specific element in matched set , update value. try this:
$('.thumbnail').prop('src', function(i, val) { return val.replace('background', 'category'); });
Comments
Post a Comment