Results 1 to 6 of 6

Thread: Writing to an input with JS

  1. #1
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Writing to an input with JS

    Hey all,

    I'm trying to get my script to write the contents of a variable to my textbox but I have had no luck despite trying several different methods I have found on Google.

    Here is my current code:

    Code:
    <script type="text/javascript" src="jquery.js"></script>
    
    function getTime () {
      	var date = new Date ();
      	startTime = date.getTime();  	
    }
    	
    function getSpeed () {
      	var date = new Date ();
      	var endTime = date.getTime();
      	var milli = endTime - startTime;  		
    	var text = document.getElementById('rSpeed');//textbox id
    	
      	if (milli < 6000){
      		 text.value += '< 1 Min.';
      		//alert ('Less than one minute.');		
      	} else if (milli >= 6000) {
      		var speed = milli / 60000;
      		var wpm = Math.round(597 / speed);
      		text.value += wpm;
      		//alert (wpm);
      	}
    }

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <input id="rSpeed"  /> <input type="button" name="" value="Show Time" onmouseup="getSpeed();"/>
    <script type="text/javascript">
    /*<![CDATA[*/
    
    var startTime;
    
    function getTime () {
      	var date = new Date ();
      	startTime = date.getTime();
    }
    
    getTime (); // to initialise startTime
    
    function getSpeed () {
      	var date = new Date ();
      	var endTime = date.getTime();
      	var milli = endTime - startTime;
    	var text = document.getElementById('rSpeed');//textbox id
    alert(milli)
      	if (milli < 6000){
      		 text.value += '< 1 Min.';
      		//alert ('Less than one minute.');
      	} else if (milli >= 6000) {
      		var speed = milli / 60000;
      		var wpm = Math.round(597 / speed);
      		text.value += wpm;
      		//alert (wpm);
      	}
    }
    
    /*]]>*/
    </script>
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the quick reply!

    I put it in just like you posted and it's not doing anything for me.

  4. #4
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Vic's code works fine here. Maybe it's the browser you're using? Or maybe there is some extra code on the page that is killing the script? (Maybe you copied and pasted something from outside the code box?)

    Can you post a link to the page so we can see how your source code is set up please?
    Last edited by Beverleyh; 01-04-2013 at 12:48 AM.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  5. #5
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Well, I found the problem, which leads me to another question.

    When I deleted the script tag that I had at the top of the page it worked.

    So this brings me to the question, is there a way for me to 'include' other js files in this file so that I can use things like jquery or whatever when writing my scripts?

  6. #6
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    In the page code that Vic provided?

    Yes - that's a fully formatted web page so you'd just continue adding whatever you need to it in the usual place. For example, a link to the jQuery library would go in the <head> section, so too would any CSS (either internal or as a link to an external CSS file) and the rest of your HTML markup would go in the <body> section.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

Similar Threads

  1. Replies: 5
    Last Post: 05-24-2011, 04:20 AM
  2. Replies: 1
    Last Post: 12-15-2010, 05:25 AM
  3. jquery copy input to other input
    By nicmo in forum JavaScript
    Replies: 4
    Last Post: 09-26-2010, 01:52 PM
  4. Replies: 1
    Last Post: 12-04-2008, 09:11 AM
  5. writing to xml using php
    By adilfarid in forum PHP
    Replies: 1
    Last Post: 05-04-2008, 04:20 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •