Solved

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

Posted on 2011-02-12
3
434 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

758 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

19 Experts available now in Live!

Get 1:1 Help Now