Results 1 to 5 of 5

Thread: Dynamic getElementById()

  1. #1
    Join Date
    Jun 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Dynamic getElementById()

    Hey guys, I know i posted this in another post but thought i'd explain it in more depth in its own topic.

    Basicly I have the following HTML

    Code:
    <table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td align="center" bgcolor="#ffffee"><strong>Row Title</strong> <br>(Appears in the left side of the table, HTML accepted) </td><td align="center" bgcolor="#ffffee"><strong>Row Description</strong> <br>(Appears in the right side of the table, HTML accepted) </td></tr><tr><td align="center" valign="top"><textarea name="rowtitle_0" style="width: 98%;"></textarea></td><td align="center" valign="top"><textarea name="rowdesc_0" style="width: 98%;"></textarea></td></tr>
    
    <tr><td align="center" valign="top"><textarea name="rowtitle_1" style="width: 98%;"></textarea></td><td align="center" valign="top"><textarea name="rowdesc_1" style="width: 98%;"></textarea></td></tr>
    <tr><td colspan="2" align="right"><input value="2" name="numberofrows" id="numberofrows" type="hidden"><input name="generate" value="Generate HTML" onclick="generateproddeschtml()" type="button">&nbsp;<input name="preview" value="Preview HTML" onclick="alert(document.edit.rowtitle[]);" type="button"></td></tr></tbody></table>
    That is generated by another script in PHP now what i need to do is this, i need to loop through the number of rows and create some HTML code based upon the information in the boxs... this is what I came up with

    Code:
    function generateproddeschtml(){
    	<!-- ok generate the product description and place it into the description field -->
    	var NUMBEROF = document.getElementById('numberofrows').value;
    	THEFIELDS = Array();
    	for (var i = 0; i < NUMBEROF; i++){
    		THEFIELDS[i] = 'rowtitle_'+i;
    		alert(i);
    	}
    	var HTML = '<table width="460" cellpadding="0" cellspacing="0">';
    	for (var i_temp = 0; i_temp < THEFIELDS.length; i_teno++){
    		HTML = HTML + '<tr><td width="17"><img src="img/doughnut_red.gif" width="15" height="15" /></td><td height="21" width="200">'+document.getElementById(THEFIELDS[i_temp]).value+'</td><td width="250">'+document.getElementById('rowdesc_'+i)+'</td></tr>';
    	}
    	
    	HTML = HTML + '</table>'
    	document.getElementById("description").value = HTML;
    
    }
    What happens when i call this script? well I get the following error message....

    Code:
    Error: document.getElementById(THEFIELDS[i_temp]) has no properties
    Line: 84
    Its doing my head in now and I have untill tonight to get it done any suggestions?

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    If you have a table with an ID:
    Code:
    <table id="tbl">
    it's a simple matter to get all cells in that table:
    Code:
    var e = document.getElementById("tbl").getElementsByTagName("td");
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jun 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    If you have a table with an ID:
    Code:
    <table id="tbl">
    it's a simple matter to get all cells in that table:
    Code:
    var e = document.getElementById("tbl").getElementsByTagName("td");
    However this will not bring back the values of the text fields

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You can use innerHTML or getElementById() on the cells to obtain their contents or the contents of another element inside them.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jun 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    You can use innerHTML or getElementById() on the cells to obtain their contents or the contents of another element inside them.
    I am already using innerHTML for something else.

    I have uploaded a cut down version of the file to my site so you can all see what i am doing

    http://www.paramiliar.com/testjs.html

    It is still saying the getElementById() is incorrect but the fields excist and they have data

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
  •