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

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -