I'm trying to display elements on a page by replacing a string in the URL parameter with the width of each element of a certain class. I'm new to JavaScript, so any help would be appreciated!
Here's an example of the HTML for objects on the page:
<div class="getwidth" style="background-image: url('//domain.de/picture1.jpg?w=putwidth');">...
</div>
<div class="getwidth" style="background-image: url('//domain.de/picture2.jpg?w=putwidth');">...
</div>
And here is the jQuery onload function I'm using:
jQuery('.getwidth').each(function() {
var wrapper = jQuery(this);
var width = wrapper.width();
wrapper.html(wrapper.html().replace(new RegExp(/putwidth/, 'g'), width));
});
If an element is 245px wide, the desired output should look like this:
<div class="getwidth" style="background-image: url('//domain.de/picture2.jpg?w=245');">...
</div>
The code successfully retrieves the element's width, but I'm having trouble replacing the string.