Jump to content

Getting time in text box


s_avinash_s

Recommended Posts

Hi

I am getting a time from PC .I have a tab with id "DateTimeSettings".

 

    <div id="DateTimeSettings" class="tabcontent1">
    
    
  <div id="automatic">  
<script>      
function auto_time() {
var d = new Date();
document.getElementById("automatic").value = d;
document.getElementById("automatic1").value = d;
}

</script>

<input type="text" id="automatic1" size="11" style="border: 2px solid black;font-size:15pt;" readonly >
</div>
      </div>

My need is , whenever i open that tab i need a updated time in my text box as mentioned below.

<input type="text" id="automatic1" size="11" style="border: 2px solid black;font-size:15pt;" readonly >

 

How to get it, am always getting a empty value.

Please suggest

Link to comment
Share on other sites

Does Gandalf come along and call auto_time()?

<div id="automatic">  

IS NOT A FORM INPUT ELEMENT, therefore using '.value' to to give it a value WON'T WORK. It represents the attribute 'value=""   which only inputs use', same as '.style' represents the attribute style="border: 2px solid black;font-size:15pt;"  and '.size' represents 'size="11" '

you also need a closing </div> tag before any-other html or script. Else when you update with .innerHTML it will overwrite everything within it! Which includes script and input.

Link to comment
Share on other sites

Hi

I have modified the code as below

    <div id="DateTimeSettings" class="tabcontent1">   
  <input type="hidden" id="automatic">  
<input type="text" id="automatic1" size="40" style="border: 2px solid black;font-size:15pt;" readonly >
      </div>
      <script>
      function auto_time() {
var d = new Date();
document.getElementById("automatic").value = d;
document.getElementById("automatic1").value = d;
}

Now i get the data in box and then i have a junk characters as shown in attached image.

2.One more need is, i have a tab with id = "DateTimeSettings" .The date should update on;y when i click that particular  tab.

3. I need to use form method =post for submitting it.How to use it

How ca i do it, please help

Capture7.PNG

Capture8.PNG

Edited by s_avinash_s
Link to comment
Share on other sites

If you don't save your files as encoded to UTF-8, or set charset to UTF-8, you might well experience this problem.

You've answer your own question, as JavaScript code only runs on events, and luckily 'CLICK' is one of those events.

Edited by dsonesuk
Link to comment
Share on other sites

Hi

when i use like below .am not getting

<div id="DateTimeSettings" onclick="auto_time()"  class="tabcontent1">   
    
    <br>
 <input type="hidden" id="automatic">  
 <form action="/automatic.txt" method="post">
<input type="text" id="automatic1" size="40" name="fname" style="border: 2px solid black;font-size:15pt;" >
<input type="submit" value="submit">  
</form>
      </div>
      <script>
      function auto_time() {
var d = new Date();
document.getElementById("automatic").value = d;
document.getElementById("automatic1").value = d;
}

Instead if i keep 

<button onclick="auto_time()">Click me</button>

i get the time updated.

Is it possible to do with using id="DateTimeSettings"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...