html - How to bind OnClick function to list of images in Javascript -


i using following code crete images programatically (dynamically in loop):

function showimage(source, id) { var list = document.getelementbyid("display-list");  //image var cellimage = document.createelement("td"); var objimage = document.createelement("img"); objimage.classlist.add("obj"); cellimage.setattribute("align", "center"); cellimage.setattribute("valign", "bottom"); cellimage.appendchild(objimage); list.appendchild(cellimage);  //checkbox var cellcheckbox = document.createelement("td"); var objcheckbox = document.createelement("input"); objcheckbox.id = id; objcheckbox.type = 'checkbox'; cellcheckbox.setattribute("valign", "bottom"); cellcheckbox.appendchild(objcheckbox); list.appendchild(cellcheckbox);  objimage.src = source; } 

now, need bind function on click of each image passing each image id function. how can in jscript?

thanks.

well, i'd (a) attach id image , (b) extract id inside onclick handler.

perhaps little this:

code edit:

var objimage = document.createelement('img'); objimage.setattribute('myid', id); 

code addition:

function myclickhandler(e) {   var clickedimage = this;   var clickedimageid = this.getattribute('myid');   //   // here image &/or id } 

i haven't used .id attribute of images, since haven't set them yourself. have used .id attribute of checkbox. can't have duplicate ids on page, setting .id of objimage same value .id of objcheckbox won't make work.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -