Solved

On post back the value entered to an element using javascript is gone

Posted on 2011-02-12
3
437 Views
Last Modified: 2012-05-11
I have asp:button, whoch on click calls a javascript function to assign the date to a html (p) element. I can see the datetime for a second, but I think on postback the value of the element is gone. How do I make sure after the user clicks on the button the datetime value is still shown. I have attached the code thnaks
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    
    <script type="text/javascript">
        function displayDate() {
            document.getElementById("demo").innerHTML = Date();
        }
</script>
</head>
<body>
    <form id="form1" runat="server">
    
    
<h1>My First Web Page</h1>

<p id="demo"></p>
<asp:Button ID="btn" Text="disp" runat="server" OnClientClick="return displayDate();" />
</form>
</body>
</html>

Open in new window

0
Comment
Question by:TrialUser
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
BurnieP earned 500 total points
ID: 34879762
Hi,

Add return false in your javascript function.

I have modified your code below :
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    
    <script type="text/javascript">
      function displayDate() {
        document.getElementById("demo").innerHTML = Date();
        return false;
      }
</script>
</head>
<body>
    <form id="form1" runat="server">
    
    
<h1>My First Web Page</h1>

<p id="demo"></p>
<asp:Button ID="btn" Text="disp" runat="server" OnClientClick="return displayDate();" />
</form>
</body>
</html>

Open in new window

0
 

Author Comment

by:TrialUser
ID: 34879922
oh perfect thx
0
 

Author Closing Comment

by:TrialUser
ID: 34879928
thx
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

919 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now