s_avinash_s Posted October 15, 2018 Share Posted October 15, 2018 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 More sharing options...
dsonesuk Posted October 15, 2018 Share Posted October 15, 2018 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 More sharing options...
s_avinash_s Posted October 15, 2018 Author Share Posted October 15, 2018 (edited) 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 Edited October 15, 2018 by s_avinash_s Link to comment Share on other sites More sharing options...
dsonesuk Posted October 15, 2018 Share Posted October 15, 2018 (edited) 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 October 15, 2018 by dsonesuk Link to comment Share on other sites More sharing options...
s_avinash_s Posted October 15, 2018 Author Share Posted October 15, 2018 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 More sharing options...
dsonesuk Posted October 15, 2018 Share Posted October 15, 2018 (edited) Well it works for me? mind you my browser has away of fixing code, so it does work! amazing that! Edited October 15, 2018 by dsonesuk Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now