Brian
Brian

Reputation: 2051

Getting Value from HTML and storing to variable

I have the following code: http://pastebin.com/61K2J9kR

Can anyone tell me why the value is not getting stored into dest_x ? The input in the textbox should be stored into dest_x once the 'GO!' button is clicked.

Any help would be appreciated.

Upvotes: 0

Views: 445

Answers (1)

Fenton
Fenton

Reputation: 251242

It may be because you have two onclick attributes on the same element.

<INPUT id="btnMove" type="button" value="GO!" onClick="javascript:var t=setInterval('moveRight()', 80)" onClick="getValue()">

You should only have one:

<input id="btnMove" type="button" value="GO!" onclick="var t=setInterval('moveRight()', 80); getValue();">

You also shouldn't have javascript: in the onclick event - that was a technique used to place JavaScript in an href attribute.

You should also move the retrieval of dest_x into your moveRight function:

function moveRight() {
    var dest_x = document.getElementById('txtChar').value;
    //...

Note that it is getElementById, not getElementByID - JavaScript is case sensitive.

I have included these changes in an updated version of your script: http://pastebin.com/aX6mXRhg

These changes make things functional - there are other things that you may want to consider later on - but the fixes above should get your going in the meantime.

  1. Scoping your functions and variables
  2. Attaching the events in JavaScript, rather than HTML attributes
  3. Avoid using eval

Upvotes: 2

Related Questions