How to get div content that has changed to javascript?

I have a div on the page whose contents will be changed by javascript, I want to get its value from C # code. but always returns an empty or initial value, not a changed value.

If I changed from div to hidden, it works well. I do not know why?

here is the code:


<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
        foo = function()
        {
            var d = document.getElementById('divTest');
            d.innerHTML = 'my value';

            var e = document.getElementById('hiddenTest');
            e.value = 'my value';
        }
    </script>
</head>
<body>
    <form id="form1" runat="server" >
    <div>
        <div id="divTest" runat="server"  />
        <input type="hidden" runat="server" id="hiddenTest" />
        <input type="button" value="test" onclick="javascript:foo();" />

        <asp:Button ID="btnTest" runat="server" Text="ASP.NET Button" OnClick="OnbtnTest" />
    </div>
    </form>
</body>

here is the C # code:


   protected void OnbtnTest(object sender, EventArgs e)
    {
        Response.Write( string.Format("alert('{0}');", hiddenTest.Value) );
    }
+3
source share
5 answers

Form input elements inside a form element sent to the server; all other content is static and not sent to the server. This is the basic HTTP rule you can see here.

, div . .

AJAX , .

+2

, .

+2

javascript hiddenTest divTest, . javascript . foo() :

foo = function()
{
   var d = document.getElementById('<%= this.divTest.ClientID %>');
   d.innerHTML = 'my value';

   var e = document.getElementById('<%= this.hiddenTest.ClientID %>');
   e.value = 'my value';
}
0

, ? , , , , script? , ?

, . onchange, " " " " " ".

, AJAX. . , # , , javascript, . jquery ajax, :

    $("input#testinput).post(
                  "myserverscript.aspx", 
                   {question : "Who Loves the Sun?"}, 
                   function(answer) {
                   alert("Answer: " + answer);
                   });

, , #, , -, javascript, asp html.

0

In my case, I used AJAX HTMLEditor instead of the div tag. unlike Div, any changes made to the client-side HTMLEditor will be sent to the server

0
source

Source: https://habr.com/ru/post/1713317/


All Articles