PDA

View Full Version : Writing to an input with JS



Nifty62
01-03-2013, 07:14 PM
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:



<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);
}
}

vwphillips
01-03-2013, 11:10 PM
<!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>

Nifty62
01-04-2013, 12:32 AM
Thanks for the quick reply!

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

Beverleyh
01-04-2013, 12:37 AM
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?

Nifty62
01-04-2013, 01:35 AM
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?

Beverleyh
01-04-2013, 08:08 AM
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.