I am attempting to create a function where by a button is clicked and the background image is selected for a div. Here is what I have started below but it does not seem to work can anyone point out where Im going wrong… 🙂
<style type="text/css">
#txt{
width: auto;
height: auto;
border: solid #000;
}
</style>
<script type="text/javascript">
function backg1(){
var test = new string ();
test = document.getElementById('txt');
test.style.backgroundImage="url('cloud.jpg')";
}
</script>
</head>
<body>
<div id="txt">
<input type="button" value="BG1" onclick="backg1()"/>
</div>
Since your
<div>originally contains nothing but the button input, it has no size outside the boundaries of that button. You will need to set an explicit width and height (along withposition: relative) to see the background.I would recommend setting them to the same dimensions as your image.
Or if you need to set them dynamically: