?
Solved

Add control using javascript

Posted on 2009-02-17
8
Medium Priority
?
596 Views
Last Modified: 2012-05-06
I would like to add a textbox control on a from using javascript. my code is below and my problem is i cant see the textbox i have created. what am i suppose to do to make it visible.
there are no errors generated.
 var txt5   = document.createElement ("input");
      txt5.setAttribute ('type', 'text');
      txt5.setAttribute ('id', name);
      txt5.value = "defValue";
      txt5.name  = "name";
      txt5.top = 25;
      txt5.left = 5
      txt5.width = 100;
      txt5.height = 100;
Thanks
0
Comment
Question by:Dawie de Villiers
  • 4
  • 3
8 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 750 total points
ID: 23659673
Hello dawie,

ContainerElement.AppendChild(txt5);

You need to actually place the newly created element into another element, either into the document.body or another ContainerElement such as a div.

Regards,

TimCottee
0
 
LVL 16

Assisted Solution

by:sunithnair
sunithnair earned 750 total points
ID: 23659888
As Tim said you need to add the element into a container for it to be visible on the html page. You also need to add top, left, width and height to the style in order for it to be cross browser compatible (your code will not work as required in firefox)
<html>
<head>
<script language='javascript'>
function AddElement(elementName)
{
var txt5   = document.createElement ("input");
      txt5.setAttribute ('type', 'text');
      txt5.setAttribute ('id', elementName);
      txt5.value = "defValue";
      txt5.name  = elementName;
      txt5.style.top=25;
      txt5.style.left=5;
      txt5.style.width=100;
      txt5.style.height=100;
 document.body.appendChild(txt5);
}
</script>
</head>
<body>
<input type="button" value="add" onclick="AddElement()">
</body>
</html>

Open in new window

0
 
LVL 16

Expert Comment

by:sunithnair
ID: 23659896
Sorry forgot to add the parameter
<html>
<head>
<script language='javascript'>
function AddElement(elementName)
{
var txt5   = document.createElement ("input");
      txt5.setAttribute ('type', 'text');
      txt5.setAttribute ('id', elementName);
      txt5.value = "defValue";
      txt5.name  = elementName;
      txt5.style.top=25;
      txt5.style.left=5;
      txt5.style.width=100;
      txt5.style.height=100;
 document.body.appendChild(txt5);
}
</script>
</head>
<body>
<input type="button" value="add" onclick="AddElement('txtName')">
</body>
</html>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Dawie de Villiers
ID: 23667469
Thanks guys but one last thing the txt5.style.top=5; and   txt5.style.left=5; properties are not working.
the textbox is written after everything else has been displayed(there is a table on the form and i wanted txt5 to be written inside the table)
i have tried to add a div and assign it ID DIVID and changed the add line to this:
 document.forms(0).DIVID.appendChild(txt5);
but i get an error that divid is null or not an object.
0
 

Author Comment

by:Dawie de Villiers
ID: 23667483
this didnt work either: document.forms(0).getElementById("DIVID").appendChild(txt5);
object does not support this property or method
0
 

Author Comment

by:Dawie de Villiers
ID: 23667622
hi guys, i finally gto it to work, but the textbox just appears and disappears immediately.

 var txt5   = document.createElement("input");
      var di =document.getElementById("DIVID");
      txt5.setAttribute ('type', 'text');
      txt5.setAttribute ('id', "name");
      txt5.value = "defValue";
      txt5.name  = "name";
      txt5.style.top=5;
      txt5.style.left=5;
      txt5.style.width=100;
      txt5.style.height=100;
      di.appendChild(txt5);
0
 
LVL 16

Expert Comment

by:sunithnair
ID: 23668322
Are you having any style applied to the div for it to get dissappeared?
0
 

Author Comment

by:Dawie de Villiers
ID: 23668382
no im not, here is the div property
                                        <div id="DIVID" style="width: 433px; height: 107px">
                                        </div>
its inside a table row(im not setting visible property to anyone control on the table )
below is the complete page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="frmSearchGrid.aspx.cs" Inherits="frmSearchGrid" %>
 
<%@ Register Assembly="RadGrid.Net2" Namespace="Telerik.WebControls" TagPrefix="rad" %>
 
<%@ Register Assembly="RadInput.Net2" Namespace="Telerik.WebControls" TagPrefix="rad" %>
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>
<script language ="javascript" type ="text/javascript" >
  function AddButon()
  { 
      var txt5   = document.createElement("input");
      var di =document.getElementById('DIVID');
      txt5.setAttribute ('type', 'text');
      txt5.setAttribute ('id', "name");
      txt5.value = "defValue";
      txt5.name  = "name";
      txt5.style.top=5;
      txt5.style.left=5;
      txt5.style.width=100;
      txt5.style.height=100;
      txt5.style.visibility = 'visible';
      var newdiv = document.createElement('div');
      //var divIdName = my+num+Div;
      newdiv.setAttribute('id',"divIdName");
      newdiv.appendChild(txt5);
      //newdiv.innerHTML = '<input type="text"  name="TextBox' + "txt5" + '" value="TextBox'+ "txt5" +'" >';
      di.appendChild(newdiv);
      alert(txt5.id);     
      //
      //newdiv.innerHTML = <input type=text  name=TextBox+num+ value=TextBox+num+ >;
  }
   function OpenGrid(txt2)
   {
    alert(txt2);
   }
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Search</title>
    <link href="../StyleSheets/StyleSheet.css" rel="stylesheet" type="text/css" />    
</head>
<body enableviewstate="True" style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; clip: rect(0px 0px 0px 0px); width: 100%; height: 100%;">
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
        <table cellpadding="0" cellspacing="0" style="height: 95%; width: 100%; padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; clip: rect(0px 0px 0px 0px); padding-top: 0px; background-color: #eef0f6;" border="0">
            <tr>
                <td class="Header" colspan="2" style="height: 30px">                    
                    <asp:Label ID="lblContacts" runat="server" Text="Search" ForeColor="White"></asp:Label>                    
                    </td>
            </tr>
            <tr>
                <td style="vertical-align: top;">
                </td>
                <td style="padding-left: 10px; height: 40px; border-right: navy 1px solid; border-top: navy 1px solid; border-left: navy 1px solid; border-bottom: navy 1px solid;">
                    <TABLE style="padding-right: 0px; height: 80%;width: 100%; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; background-color: #eef0f6;" cellSpacing=0 cellPadding=0 
border=0 id="tblView" runat="server"><TBODY><TR>
    <td colspan="4" style="padding-right: 5px; padding-left: 5px; font-weight: bold;
        font-size: 11px; padding-bottom: 5px; padding-top: 5px; font-family: tahoma; vertical-align: top; width: 100%; height: 90%;" rowspan="4">
        <table frame="void "
style="background-color: #eef0f6; width: 100%; height: 80%; padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; clip: rect(0px 0px 0px 0px); padding-top: 0px;" cellpadding="0" cellspacing="0" border="0" id="MyTable">
            <tr>
                <td style="font-size: 11px; font-family: Tahoma; height: 4%">
                    <table border="0" cellpadding="0" cellspacing="0" class="table" style="height: 80%" id="MyTable">
                        <tr>
                            <td>
                                <asp:Label ID="lblLook" runat="server" Text="Look For:" style="font-weight: normal; font-size: 9pt; font-family: tahoma" ForeColor="Chocolate"></asp:Label></td>
                            <td>
                                <telerik:RadTextBox ID="tboSearch" runat="server" Width="260px" Skin="Outlook">
                                </telerik:RadTextBox></td>
                            <td>
                                <asp:Button ID="btnSearch" runat="server" Text="  Find  " OnClick="btnSearch_Click" Height="18px" Width="80px" style="font-size: 11px; font-family: tahoma" />
                                <asp:Button ID="btnRefresh" runat="server" Text="Refresh List" OnClick="btnRefresh_Click" Height="19px" Width="6px" style="font-size: 11px; font-family: tahoma" Visible="False" />
                                <asp:TextBox ID="tboLength" runat="server" Width="0px"></asp:TextBox></td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td style="font-size: 11px; font-family: Tahoma">
                </td>
            </tr>
                    <tr>
                        <td style="font-size: 11px; font-family: Tahoma;">
                            <hr />
                            </td>
                    </tr>
                    <tr>
                        <td>
                                        <div id="DIVID" style="width: 433px; height: 107px">
                                        </div>
                                    
                    <tr>
                        <td style="font-size: 11px; font-family: Tahoma;">
                            <table style="width: 100%; height: 90%;" border="0" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td style="height: 40px">
                                        <asp:Button ID="btnEdit" runat="server" Text="Edit" OnClick="btnEdit_Click" OnClientClick = "AddButon()" Width="70px" Height="20px" />
                                        <asp:Button ID="btnNew" runat="server" Text="New" Width="70px" OnClick="btnNew_Click" Height="20px" /></td>
                                    <td style="height: 40px">
                                        </td>
                                    <td style="height: 40px">
                                        <asp:TextBox ID="TextBox2" runat="server" Width="0px"></asp:TextBox>
                                    </td>
                                    <td style="height: 40px">
                                        <asp:TextBox ID="TextBox1" runat="server" Width="0px"></asp:TextBox></td>
                                </tr>
                                <tr>
                                    <td style="height: 1%" colspan="4"><hr/>
                                        <asp:GridView ID="rdgAccounts" runat="server" AutoGenerateColumns="False" OnRowDataBound="rdgAccounts_RowDataBound" AllowPaging="True" AllowSorting="True" OnRowCommand="rdgAccounts_RowCommand" OnPageIndexChanged="rdgAccounts_PageIndexChanged" OnRowEditing="rdgAccounts_RowEditing" OnSelectedIndexChanged="rdgAccounts_SelectedIndexChanged" OnPageIndexChanging="rdgAccounts_PageIndexChanging" OnSelectedIndexChanging="rdgAccounts_SelectedIndexChanging">
                                            <Columns>
                                                <asp:BoundField DataField="ID" />
                                                <asp:BoundField DataField="Name" />
                                                <asp:BoundField DataField="Date" />
                                                <asp:TemplateField>
                                                    <AlternatingItemTemplate>
                                                        <asp:TextBox ID="txt" runat="server" Text='<%# Eval("ID") %>' ></asp:TextBox>
                                                    </AlternatingItemTemplate>
                                                    <ItemTemplate>
                                                        <asp:TextBox ID="txt" runat="server" Text='<%# Eval("ID") %>'></asp:TextBox>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:CommandField ShowEditButton="True" />
                                            </Columns>
                                        </asp:GridView>
                                        </td>                                    
                                </tr>
                                <tr>
                                    <td>
                                    </td>
                                    <td>
                                    </td>
                                    <td style="width: 30%">
                                        &nbsp; &nbsp;&nbsp;&nbsp;
                                    </td>
                                    <td style="padding-right: 0px; padding-left: 60px" align ="right">
                                        &nbsp;<asp:Button ID="btnOk" runat="server" Text="OK" OnClientClick = "SaveToParent()" OnClick="btnOk_Click" Width="70px" Height="20px"/>
                                        <asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" Width="70px" Height="20px" /></td>
                                </tr>
                            </table>
                        </td>
                    </tr>
    <tr>
    </tr>
    <TR></TR>
    <tr>
    </tr>
</TBODY>
                </table>
                </td>
    </TR>
    </TABLE>
                    
                </td>
            </tr>
        </table>
            </ContentTemplate>
        </asp:UpdatePanel>
    </form>
</body>
</html>

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question