Richard Young Richard Young - 1 year ago 88
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.

Answer Source

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)');

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download