user1139525
user1139525

Reputation: 11

appending a table in div tag using javascript

I have a javascript like

function btnclick()
{

  \\creating a div dynamically
  div[divclick,300,200, 'ok', cancel, btnok_click, btncancel_click]

}

<div style display:none>
<table id = 'tblcontainer'>
<tr>
<td>
add:
</td>
<asp:label id = lblgo>
</asp:label>
</tr>
</table>

Now on executing the btnclick fucntion.I need to append the tblcontainer to divclick(dynamically created)

Upvotes: 0

Views: 11360

Answers (3)

sureshunivers
sureshunivers

Reputation: 1753

Syntax for creating new element is,

var element= document.createElement(TAG_NAME);

TAG_NAME is a string example values are "DIV", "H1", "SPAN", etc,.

Following use to add attributes for the new create element,

element.setAttribute('attributeName','value'); // or element.attributeName = 'value';

attributeName => attribute name of a element that is class, id, style, align or etc,.

Add content into created element:

var content = document.getElementById('tblcontainer').outerHTML;
element.innerHTML = content;

For the content variable directly we can provide element string values.

Attach the created element on body or any other div element,

var _div = document.getElementById("divId");
_div.appendChild(element);
//or
var _body = document.getElementsByTagName('body')[0];
_body.appendChild(element);

Upvotes: 1

Manann Sseth
Manann Sseth

Reputation: 2745

You can define one div tag with ID : "divID" or whatever

Then,

var adddivAll = document.getElementById("divID");
adddivAll.innerHTML = showData();

Function

function showData()
{
   var str;

   str = "<table width='300' border='0' cellspacing='0' cellpadding='0' align='left'>";

   str += "<tr><th width='55'>Rang</th><th width='80'>Name</th></tr>";

   for (i = 0; i < array_count; i++) {
       str += "<tr><td width='55'>" + array_count[i] + "</td><td width='85'>" + array_count_Name[i] + "</td></tr>";
   }

   str += "</table>";

   return str;
}

For array_count : You can take an array for dynamic values. same as array_count_name.

Thanks.

Upvotes: 1

ecco88
ecco88

Reputation: 636

your javascript is not real and your html is malformed, so I am assuming it to be pseudocode.

function btnclick(){
        \\creating a div dynamically
        var div = document.createElement("DIV");
        \\ you can do the other stuff not sure where you got the following...
        \\  div[divclick,300,200, 'ok', cancel, btnok_click, btncancel_click]
        div.appendChild(document.getElementById('tblcontainer'));
}

<div style display:none>
<table id = 'tblcontainer'>
<tr>
<td>
add:
</td>
<asp:label id = lblgo>
</asp:label>
</tr>
</table>

Upvotes: 1

Related Questions