Find a div

PROKA

Junior Contributor
Joined
Sep 3, 2003
Messages
249
Location
Bucharest
  • Hello, I want to insert HTML Code inside a <div id="div1"
  • I want to search for that div, by its ID
  • It was something like element.innerhtml ...... forgot
  • better if u give me a solution w\ Javascript
 
Try this:
Code:
<html>
    <head>
    <title>some title</title>
    <script type="text/jscript">
    function setDivCode(myCode)
    {
     
      var myDiv = document.getElementById('div1');
      myDiv.innerHTML += myCode;
    }
    </script>

    </head>
    <body>
        <input type="text" id="txt" value="<b>here is  my code</b>"/>
        <input type="button" id="btn" onclick="setDivCode(txt.value);" />
        <div id="div1">
            <a href="http://www.google.com" id="lnk">google</a>
        </div>
    </body>
</html>
 
Ok, This is a start.
But I'm kinda nubish with JavaScript.

What I need is something to work with from asp.net, in the .vb code behinde file. That's because I work with sql and I have to insert in that div data from SQL.
 
OK I found out how and I'll post it here for others searching for the same thing:

The trick is to put in the html code runat="server" for the div element:
Code:
<div id="div1" runat="server">

</div>

then in ASP.NET use
Code:
div1.InnerHtml
to insert Html.

Now I wonder if there's a way to delete a div from the html. (not only to set its InnerHtml to nothing)
 
So, if you add that div from Codebehind you'll have no problem to remove it.

This example will show you how to add DIV to TableCell (You can add it to whatever you want ) and how to remove it.

aspx code:

Code:
....
<table>
<tr>
<td id="myTD" runat="server">
</td>
</tr>
</table>
....
aspx.cs code:

Code:
    HtmlGenericControl div2 = new HtmlGenericControl("div");//DIV declaration
    protected void Page_Load(object sender, EventArgs e)
    {
        div2.ID = "div2";
        HtmlButton btn = new HtmlButton(); //put some DIV controls just to see if it's there
        btn.ID = "btn1";
        div2.Controls.Add(btn);
        myTD.Controls.Add(div2);// Add it to the TableCell
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Here is, remove the DIV.  
        myTD.Controls.Remove(div2);
    }
}
 
Back
Top