Richard Young Richard Young - 24 days ago 4x
HTML Question

Change background image of division while changing src of image

I have a style specified in the head of my document. I specify the background image on a division but later on a need to change it if the user clicks on a link but everything I've tried to do refuses to work.

<style type="text/css">
/*Some CSS*/
.large {
background: url('/images/artwork1.jpg') no-repeat;

<div class="large"></div>
<img id="main_image" alt="Image"/>
<img onclick="javascript:document.getElementById('main_image').sr‌​c='/images/artwork2.‌​jpg'; $('.large').css({'background-image':'url(/images/‌​g)';" alt="Change Image"/>

I tried taking the background image out of the style but clicking on a link didn't change it. I've tried JavaScript and jQuery. I was able to change the image until I added in the jQuery.


Yeah, that's because you have error in your syntax.

Try this.

javascript:document.getElementById('main_image').sr‌​c='/images/artwork2.‌​jpg'; $('.large').css('background-image', 'url(/images/‌​g)');