3 Replies - 565 Views - Last Post: 25 October 2013 - 03:55 PM

#1 mil1234  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 109
  • Joined: 01-February 09

Assign id's to dynamically created controls

Posted 25 October 2013 - 02:34 PM

Hi,

as can be seen from the attached code what i am doing is to create a new table row with in it a set of textboxes or checkboxes. My problem is to assign different id's to the mentioned controls, so i can then later retrieve them on server side?? Can anyone please help me with this? Obviously i will need that each control created has a different ID...

 function addRow(tableID)
        {

            var table = document.getElementById(tableID);

            var rowCount = table.rows.length;
            alert(rowCount);
            var row = table.insertRow(rowCount);

           
            var colCount = table.rows[0].cells.length;
            alert(colCount);
            var totcount = rowCount + colCount;
            var count = 0;
            for (var i = 0; i < colCount; i++)
            {
                var newcell = row.insertCell(i);
                
                newcell.innerHTML = table.rows[0].cells[i].innerHTML;
                switch (newcell.childNodes[0].type)
                {
                    case "text":
                        newcell.childNodes[0].value = "";
                        break;
                    case "checkbox":
                        newcell.childNodes[0].checked = false;
                        break;
                }
            }   
        }




Thanks Guys...

Is This A Good Question/Topic? 0
  • +

Replies To: Assign id's to dynamically created controls

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,329
  • Joined: 08-June 10

Re: Assign id's to dynamically created controls

Posted 25 October 2013 - 03:45 PM

Quote

My problem is to assign different id's to the mentioned controls, so i can then later retrieve them on server side?

unfortunately, IDs donít make it to the server.

nevertheless, to assign IDs, use the id property of the element.
Was This Post Helpful? 0
  • +
  • -

#3 mil1234  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 109
  • Joined: 01-February 09

Re: Assign id's to dynamically created controls

Posted 25 October 2013 - 03:49 PM

Thanks for your reply....yes i have used the property id and i created ID but my issue is on how to create different ID's to each control?

Thanks again...

This post has been edited by Dormilich: 25 October 2013 - 03:50 PM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,329
  • Joined: 08-June 10

Re: Assign id's to dynamically created controls

Posted 25 October 2013 - 03:55 PM

if youíre not picky about the IDs, the only thing you need is a bit of text with an autoincremented number. a simple closure will do:
function prepareID(text) {
    var count = 0;
    return function generateID() {
        count += 1;
        return text + count;
    }
}
// usage

// set the text part of the ID
var genID = prepareID("foo");

// generate an ID
someElement.id = genID(); // foo1
// generate another ID
someOtherElement.id = genID(); // foo2


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1