Hi All, new to Javascript and stuck on a small issue, its a little late and my head is not thinking straight.

The scenerio is I have two textboxes and a submit button. All i want is the values to transfer to a function so i can change 2 global variables. I have managed to 'document.write the values from the function, but cant seem to do anything else with them. Below is the code

var Jspeed=200;   //Global variables that I want to change
var CSpeed=200;



function changeSpeed(ClownSpeed, JuggleSpeed) {

 ClownSpeed = Cspeed;
 JuggleSpeed = Jspeed;
}




<form>
  <p id="controlText">Enter Clown Speed: </p> <input type="text" id="ClownSpeed" > <br />
  <p id="controlText">Enter Juggle Speed:</p> <input type="text" id="JuggleSpeed">
  <button type="button" onclick="changeSpeed(ClownSpeed, JuggleSpeed);" >Change Speed</button>
</form>

I would also like to know if this is the right apporoach to things. Im new to this so I would like to keep it as simple as possible.

Thanks to all in advance!

Ryan

Recommended Answers

All 2 Replies

I suggest you do it like this:

var Jspeed=200;   //Global variables that I want to change
var CSpeed=200;

function changeSpeed(ClownSpeed, JuggleSpeed) {
    // This will get the values as string
    CSpeed = document.getElementById('txtClownSpeed').value; 
    Jspeed = document.getElementById('txtJuggleSpeed').value;

    // Convert to int: it will throw error if the value is not an number
    CSpeed = parseInt(CSpeed);
}

<form>
  <p id="controlText">Enter Clown Speed: </p> <input type="text" id="txtClownSpeed" > <br />
  <p id="controlText">Enter Juggle Speed:</p> <input type="text" id="txtJuggleSpeed">
  <button type="button" onclick="changeSpeed();" >Change Speed</button>
</form>

Just so you know, in your code there's a lot of errors, let me show you:

function changeSpeed(ClownSpeed, JuggleSpeed) {
 // This is setting the value of parameter as the value global var, not the opossite
 // Another thing, JavaScript is case sensitive, so CSpeed is different then Cspeed
 ClownSpeed = Cspeed;
 JuggleSpeed = Jspeed;

 // You were expecting CSpeed = ClownSpeed
}




<form>
  <p id="controlText">Enter Clown Speed: </p> <input type="text" id="ClownSpeed" > <br />
  <p id="controlText">Enter Juggle Speed:</p> <input type="text" id="JuggleSpeed">
  //ClownSpeed is not an value, it's a object
  //To use like this you it should be: changeSpeed(ClownSpeed.value, JuggleSpeed.value)
  <button type="button" onclick="changeSpeed(ClownSpeed, JuggleSpeed);" >Change Speed</button>
</form>

Thanks for your fast reply! That information helped me a lot! There are so many ways of acomplishing something but only one professional way. Thanks for putting me in that direction. Everything is working perfect now!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.