SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast thenk83's Avatar
    Join Date
    Sep 2005
    Location
    Hacienda Heights, CA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding Rows To Table

    I'm a newb so be gentle. Basically my script should pick up images with a certain alt tag. Then put them in a table with its own cell. After every 6th image start a new row. This is just a practice script not production because I'm still learning.

    I've tried so many ways to get a row added but I can't I'm stumped. I've used raw html as strings in a variable and innerHTML and got it working. This method I can't. Here is the code :

    Code JavaScript:
    var productWrapper = document.getElementById("productWrapper");
    var replaceWrapper = document.getElementById("replaceWrapper");
    	if(productWrapper.nodeType == "1"){
    	var productImage = document.getElementsByTagName("img");
    	var productImageLength = productImage.length;
    	var createTable = document.createElement("table");
    		createTable.setAttribute("border", "4");
    		createTable.setAttribute("width", "100%");
    	for (j = 0;j < 6; j++){
    	var createRow = document.createElement("tr");
    	for(i = 0;i < productImageLength; i++){
    		if(productImage[i].getAttribute("alt") == "productRotate"){
    			var productNewImage = productImage[i].getAttribute("src");
    			var createCell = document.createElement("td");
    			var createProductImage = document.createElement("img");
    					createProductImage.setAttribute("src", productNewImage);
    					createProductImage.setAttribute("width", "50");
    					createProductImage.setAttribute("height", "50");
    					createCell.appendChild(createProductImage);
    					createRow.appendChild(createCell);
    			}
    		}
    	}
    	createTable.appendChild(createRow);
    	}
    	else { document.write("Couldn't find productWrapper!"); }
    	replaceWrapper.parentNode.replaceChild(createTable,replaceWrapper);
    Mmmmm .NET...PHP...CF...
    AIM: THENK83
    MSN: thenk83@hotmail.com

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    For starters, it's not an alt tag, it's an alt attribute. Also, you should try to avoid using document.write. Incidentally, where are you putting this script? Since you're using document.write to display an error message, I assume you're putting it in the body. If so, that is not very good and it should be in the head.

    I have little experience with tables, but I do believe tables are special in this case and that createElement with table rows and cells is somewhat dodgy (though I may be mistaken), and that innerHTML with rows doesn't work well in IE. I think that instead you should try using insertRow and insertCell. See this.


Bookmarks

Posting Permissions

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