JavaScript createElement, Jquery on click

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thugbuddha
    New Member
    • Apr 2012
    • 1

    JavaScript createElement, Jquery on click

    Hey,
    I have function where it creates img element and sets id = title,
    When i add to index.html jquery
    Code:
    $('#title').click(function() {
      alert('xx');
    });
    it doesn't work, why?


    Jscript function:

    Code:
     function showUploadedItem (source) {
      		var list = document.getElementById("image-list"),
    	  		li   = document.createElement("li"),
    			ip   = document.createElement("input"),
    	  		img  = document.createElement("img");
    			bild = document.createElement("img");
    			bild.src="pencil.png";
    			bild.style.width="16px";
    			bild.style.height="16px";
    			bild.style.cursor="pointer";	
    			bild.style.border="0";
    			bild.id="title";
      		img.src = source;
      		li.appendChild(img);
    		list.appendChild(li);
    		li.appendChild(ip);
    		li.appendChild(bild);
    
    	}

    Thank you!
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I’d guess that when you call the first code the second is not yet executed. therefore there is no element with the ID "title" to assign an event to (yet).

    Comment

    Working...