Jump to content

Store Input box on button click, store as Variable


DoyleChris98

Recommended Posts

What i am trying to do is create a text box to enter in a Frequency (122.25mhz). Then on a button press have it call a function to store as a variable, drop the 2 decimal places and convert the number. And return another variable after it is all done.

 

I can figure out how to move the decimal, and convert it.

My testing is that I want to display the number to see if the conversions are working right but i cant get it to display.

Here is the code.

Here is the jsfiddle for it. http://jsfiddle.net/2xz6un9b/

 

Example 122.25

FREQE = 122.25

FREQE1 = 12225

FREQE2 = 2225

FREQE3 = 4261

FREQO = FREQE3

 

html

<input type="text" id="FREQB">    <button onclick="FREQ()">Frequency</button><p id="FREQO"></p> 
Javascript
function freq(){    //get value from entery    var FREQE = document.getElementById("FREQB").value;    //dropping 2 decimal places    var FREQE1 = FREQE*100;    //removing the 1 from the front    var FREQE2 = FREQE1 - 10000;     //converting to oct    var FREQE3 = FREQE2.toString(8);        document.getElementById("FREQO").innerHTML = FREQE3;}
Edited by DoyleChris98
Link to comment
Share on other sites

Javascript is case sensitive. You've named your function freq() but you're trying to call FREQ()

 

To drop two decimal places you need to multiply by 100.

Link to comment
Share on other sites

Ok i got it to work and display the Octal number now next can i shorten the math part. I got it down to this.

 

Is there a way to shorten it up to just one line of code.

I guess this part should be in Javascript portion.

{    var FREQE = document.getElementById("FREQB").value;    var FREQE1 = (FREQE*100)-10000))    var FREQE2 = FREQE1.toString(8);        document.getElementById("FREQO").innerHTML = FREQE2;}
Edited by DoyleChris98
Link to comment
Share on other sites

Sure it can be done, but it would be a bad idea. It's difficult to read and maintain that kind of code.

document.getElementById("FREQO").innerHTML = (document.getElementById("FREQB").value*100-10000).toString(8);
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...