Jump to content
DoyleChris98

Store Input box on button click, store as Variable

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

In most browsers you can press F12 to open developer tools. Error messages are shown there which you can use to figure out why something is not working.

Share this post


Link to post
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

Share this post


Link to post
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);

Share this post


Link to post
Share on other sites

I don't understand.

 

Does Microsoft Flight Simulator use HTML and Javascript?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...