Disha Goyal
Disha Goyal

Reputation: 624

how to assign unique id to elements that are created dynamically - Javascript

Hello im trying to generate dynamic divs with a textbox and delete button in it. On form submit i want all dynamically generated textboxes values, how can i assign unique id to the textbox element. The ID's / Name of textbox should be unique.. to get all values properly. Pls suggest..

My Code:

<HTML>
<HEAD>
<TITLE> Add/Remove dynamic elements</TITLE>
<SCRIPT language="javascript">
    function addRow(divId) {
        count = 0;
        count++;

        var parentDiv = document.getElementById(divId);

        // create a div dynamically
        var eleDiv = document.createElement("div");
        eleDiv.setAttribute("name", "olddiv");
        eleDiv.setAttribute("id", "olddiv");

         // create a textbox dynamically
        var eleText = document.createElement("input");
        eleText.setAttribute("type", "text");
        eleText.setAttribute("name", 'textbox' + count);
        eleText.setAttribute("id", "textbox");

        // create a delete button dynamically
        var eleBtn = document.createElement("input");
        eleBtn.setAttribute("type", "button");
        eleBtn.setAttribute("value", "delete");
        eleBtn.setAttribute("name", "button");
        eleBtn.setAttribute("id", "button");
        eleBtn.setAttribute("onclick", "deleteRow('button')");

        // append new div to parent div 
        parentDiv.appendChild(eleDiv);

        // append textbox & button to new div 
        eleDiv.appendChild(eleText);
        eleDiv.appendChild(eleBtn);

    }

    function deleteRow(tableID) {
            var div = document.getElementById('olddiv');
            if (div) {
                div.parentNode.removeChild(div);
            }
    }

</SCRIPT>

<form name="objForm" action="test1.php">
    <INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
    <div id="dataTable" width="350px"><INPUT type="text" name="txtData" /></div>
    <input  type="submit" value="Submit"/>
</form>

Upvotes: 2

Views: 6880

Answers (3)

Randy the Dev
Randy the Dev

Reputation: 26710

To answer your question directly you could try:

var textboxCount = 0;

/* In your function */
// create a textbox dynamically
var eleText = document.createElement("input");
eleText.setAttribute("type", "text");
eleText.setAttribute("name", 'textbox' + count);
eleText.setAttribute("id", "textbox" + textboxCount);
textboxCount += 1; //Increment the count

That will create textbox's with id's textbox0, textbox1, textbox2 etc.

Or, for a more efficient option you may store all textboxs you create in an array, like so:

var textboxes = new Array();

/* In your function */
// create a textbox dynamically
var eleText = document.createElement("input");
eleText.setAttribute("type", "text");
eleText.setAttribute("name", 'textbox' + count);

textboxes.push(eleText);

Now instead of calling e = document.getElementById('textbox4'); you may call e = textboxes[4];

Upvotes: 2

Petah
Petah

Reputation: 46040

This function will generate a unique ID:

function uniqueId() {
    var uid;
    do {
        uid = 'uid-' + Math.random();
    } while (document.getElementById(uid));
    return uid;
}

Upvotes: -1

PoeHaH
PoeHaH

Reputation: 1936

You can keep a global count variable (outside of function divId). Each time you create a new div, you add 1 to the counter and use it as a unique ID for your textbox.

<script language="javascript">
    var globalcount=0;
    function addRow()
    {
         globalcount++;
         //the rest
    }

</script>

And the rest is much like you already do for the name.

Upvotes: 5

Related Questions