0

I have added image & button dynamically to a webpage. I wrote a "onclick" event, by which the image will be changed on clicking respective button. But the code doesn't work.

My code is;

 <style>
 .tableclass{
 height:80px;}
 .imgclass{
 height:50px;
 width:50px;} </style>
 <script>
 var img = new Image();
 img.src = "Images/circle.png";
 img.className = 'imgclass';

 window.onload = function init() {
 for(var i=0;i<5;i++)
 {
 var container = document.getElementById('body');
 var table = document.createElement('table');
 table.className = 'tableclass';
 var tr = document.createElement('tr');
 var td = document.createElement('td');
 var btn = document.createElement('input');
 btn.type = 'button';
 btn.className = 'buttonclass';
 td.appendChild(btn);
 td.appendChild(img);
 tr.appendChild(td);
 table.appendChild(tr);
 container.appendChild(table);
 }
 }
 btn.onclick = function(){
 document.getElementsByClassName('imgclass').src = "Images/square.jpeg";
 }; 
 </script>
 <body id="body"></body>
2
  • Try using JQuery, it makes using Javascript a breeze, along with HTML DOM manipulation! Commented Jul 4, 2013 at 14:31
  • have you got an element with id of body or are you refering to <body> tag Commented Jul 4, 2013 at 14:36

4 Answers 4

2

You are adding an event to...

boton.onclick

But I think your variable is named buton - so you need to use:

buton.onclick

Also, as this is running in the loop, you probably want to add the onclick event to each one you create (so put the event binding inside the loop).

Here is a running version of the script:

function getImage() {
    var img = new Image();
    img.src = "Images/circle.png";
    img.className = 'imgclass';
    return img;
}

function getRow() {
    var img = getImage();
    var tr = document.createElement('tr');
    var td = document.createElement('td');
    var buton = document.createElement('input');
    buton.type = 'button';
    buton.className = 'buttonclass';
    buton.onclick = function(){
        img.src = "Images/square.jpeg";
    };
    td.appendChild(buton);
    td.appendChild(img);
    tr.appendChild(td);
    return tr;
}

var container = document.getElementById('body');
var table = document.createElement('table');
table.className = 'tableclass';
for(var i = 0; i < 5 ; i++) {
    table.appendChild(getRow());
}

container.appendChild(table);

You can see it running on JSFiddle.

Sign up to request clarification or add additional context in comments.

2 Comments

you really need to check your spellings, and try and be as accurate as possible!
@user2546074 check out the fully working example I have added to my answer - it has the spellings fixed, moves the event binding to inside the loop and uses function-scope to make it easier to target just the image in the same table row.
0

getElementsByClassName returns a collection, not a single element.

You may do this :

var imgs = document.getElementsByClassName('imgclass');
for (var i=0; i<imgs.length; i++) imgs[i].src = "Images/square.jpeg";

But it would be probably cleaner to give an id to your image and use getElementById.

Note also that you seem to hesitate on the spelling of botón/boton/buton and that will make it hard for the code to work. You should see errors in the console.

2 Comments

sorry.It's buton.onclick = function()
@user2546074 you have other occurrences of the same error, like td.appendChild(botón);.
0

There boton has an accent the first time you use it...and why are you using a for loop? I think an array of images could work and every time you click it increments a counter which changes the image. Also use .jpg not .jpeg

Comments

0

This is closer to the original -- I just removed the for loop because I didn't see the need for it...

<style>
.tableclass{
height:80px;}
.imgclass{
height:50px;
width:50px;} </style>
<script>

var count = 0;
var images = [];
images[0] = "Pictures/1.jpg";
images[1] = "Pictures/2.jpg";
images[2] = "Pictures/3.jpg";
images[3] = "Pictures/4.jpg";

var img = new Image();
img.src = images[count];
img.className = "imgclass";

window.onload = function init() {

var container = document.getElementById('body');
var table = document.createElement('table');
table.className = 'tableclass';
var tr = document.createElement('tr');
var td = document.createElement('td');
var button = document.createElement('input');
button.type = 'button';
button.className = 'buttonclass';
button.onclick = function(){buttonClick();}
td.appendChild(button);
td.appendChild(img);
tr.appendChild(td);
table.appendChild(tr);
container.appendChild(table);

}

function buttonClick(){
if(count < images.length - 1){
count++;
}else{
    count = 0;
}
img.src = images[count];
}; 
</script>
<body id="body"></body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.