I am having trouble setting the background image of specific elements based off their child img’s src.
I am unable to just set the background image in the source code – I’m attempting to modyify a set layout of a WordPress plugin.
Basic structure:
<li class="choice has-thumbnail">...</li>
<li class="choice has-thumbnail active">
<button type="button" class="choice-item" style="">
<span class="choice-text--container">
<i class="mkl-pc-thumbnail">
<span>
<img src="https://www.src.website/image.png" alt="" style="display: none;">
</span>
</i>
<span class="text choice-name">Text</span>
</span>
</button>
</li>
<li class="choice has-thumbnail">...</li>
My plan is to set the button’s background image as the src image from the child img.
My code so far doesn’t work for me:
$('li.has-thumbnail button').each(
function(index, element){
$(this).css('background-image', 'url(image/' & $('img',this).prop('src') & ')');
}
);
What am I doing wrong?
2
Answers
Here is a script that will remove the content of the button too. Otherwise there is not much gained
Use the
.css()
method direcly with a return value of the descendant IMG src: