Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Accessing dynamically created elements

    Hello,

    I'm trying to write a script for a website that reads from a database and makes a separate table for each entry in the database. Since the number of entries can change, I want to dynamically create a div for each one, which I can later hide or display based on user selection. However, when I try to access the dynamically created elements by their ID, they return null.

    Is what I'm trying to do here actually possible?

    Code:
    	for(var i = 0; i < tables.length; i ++)
    	{
    		var newDiv = document.createElement("div");
    		newDiv.setAttribute("id", tables[i].name);
    		newDiv.setAttribute("name", tables[i].name);
    		newDiv.setAttribute("class", "hidden");
    		newDiv.innerHTML += tables[i].name;
    		newDiv.innerHTML += "<table id = 'table"+tables[i].name+"' border = '1'>";
    		newDiv.innerHTML += "<tr>";
    		
    		for(var rows = 0; rows < table.arrayOfFields.length; rows++)
    		{
    			newDiv.innerHTML+="<th>"+table.arrayOfFields[rows].title+"</th>";
    			numOfEntries = table.arrayOfFields[rows].arrayOfEntries.length;
    		}
    		
    		newDiv.innerHTML+="<tr>";
    
    		for(var cols = 0; cols < numOfEntries; cols++)
    		{
    			
    			for(var rows = 0; rows < table.arrayOfFields.length; rows++)
    			{
    				newDiv.innerHTML+="<td>"+table.arrayOfFields[rows].arrayOfEntries[cols].data+"</td>";
    			}
    			
    			newDiv.innerHTML+="<tr>";
    		
    		}
    		
    		newDiv.innerHTML+="</table>";
    		document.body.appendChild(newDiv);
    
    	}

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    innerHTML is not a standard DOM method. Moreover, in some cases (for instance in IE when it's about TABLE or TABLE elements) innerHTML is readonly.

    use DOM methods to create the table, same as you did with your div (createElement, setAttribute, and so on)

    Some notes regarding your code:
    1. The name attribute is to be used only in case of some elements( mainly forms or forms elements). DIV is not among them. Nor TABLE. A DIV or a TABLE can not bear a name, Anyway, what's the use? id should be enough.

    2. IE has some particular behaviors regarding setting some attributes with setAttribute(). For instance: class.To avoid a double code, use DOM 0 crossbrowser syntax:
    Code:
    newDiv.className='hidden';
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •