Jump to content

Make A Field's Value Not A String


EmperorZenos

Recommended Posts

I was wondering if I can change my text field's values to not be a string.I want to take the numbers in the field, and use them to calculate an answer, here's the script:Note: Trying to make it to solve a logarithm.

Base: <input type="text" id="Log1"> <br />Value: <input type="text" id="Log2"> <br /><input type="button" onClick="log()"><textarea readonly id="VA"> </textarea><br /><script type="text/javascript">document.getElementById("Log1").value=0;document.getElementById("Log2").value=0;B = document.getElementById(Log1).value;V = document.getElementById(Log2).value;function log() {G = V/B;document.getElementById("VA").value = G;}</script>

So Log1 would be the base and Log2 the value so if Log1 = 4 and Log2 = 16 the answer would be 4.Can anyone tell me how to do this? Or if there is an alternative.

Link to comment
Share on other sites

num = Number(num_str);
Still doesn't work.
<script type="text/javascript">document.getElementById("Log1").value=0;document.getElementById("Log2").value=0;var B = document.getElementById("Log1").value;var V = document.getElementById("Log2").value;var Ba = Number(B_str);var Va = Number(V_str); function log() {G = Va/Ba;document.getElementById("VA").value = G;}</script>

Link to comment
Share on other sites

You've taken me WAY too literally. You must experiment more. And you must believe the basic idea that variable names DO NOT appear out of nowhere, like magic. Programming makes sense.When I wrote this:num = Number(num_str);I DID NOT mean that suddenly a variable would exist with "_str" added to the end of it. Why would it? Variables exist when you tell them to exist. All I meant was that, yes, a thing that looks like a number is actually a string. Put it in the function and it will become a real number.Change your code:

var B = document.getElementById("Log1").value;var V = document.getElementById("Log2").value;var Ba = Number(B_str);var Va = Number(V_str);

to this code:

var B = document.getElementById("Log1").value;var V = document.getElementById("Log2").value;var Ba = Number(B);var Va = Number(V);

And see (1) how it MAKES SENSE, and (2) that you should have at least TRIED IT. It's your code, after all, not mine. This board exists to help you discover solutions on your own.

Link to comment
Share on other sites

You've taken me WAY too literally. You must experiment more. And you must believe the basic idea that variable names DO NOT appear out of nowhere, like magic. Programming makes sense.When I wrote this:num = Number(num_str);I DID NOT mean that suddenly a variable would exist with "_str" added to the end of it. Why would it? Variables exist when you tell them to exist. All I meant was that, yes, a thing that looks like a number is actually a string. Put it in the function and it will become a real number.Change your code:
var B = document.getElementById("Log1").value;var V = document.getElementById("Log2").value;var Ba = Number(B_str);var Va = Number(V_str);

to this code:

var B = document.getElementById("Log1").value;var V = document.getElementById("Log2").value;var Ba = Number(B);var Va = Number(V);

And see (1) how it MAKES SENSE, and (2) that you should have at least TRIED IT. It's your code, after all, not mine. This board exists to help you discover solutions on your own.

I changed the code before you made that post to have it just be Number( B ), and such, but still, it didn't work.I just get NaN for 5 and 25 and everything else.
Link to comment
Share on other sites

I should have noticed. The other thing threw me off. Put these statements INSIDE your log() function.var B = document.getElementById("Log1").value;var V = document.getElementById("Log2").value;var Ba = Number(B );var Va = Number(V); Otherwise, you're trying to get the values WHEN THE SCRIPT LOADS, which is probably before those elements even exist, so of course they have no value yet. You need to get the values when the function runs.EDIT. For the same reason, these statements aren't doing anything. You could either set the values in your HTML or put these statements into a window.onload handler:document.getElementById("Log1").value=0;document.getElementById("Log2").value=0;

Link to comment
Share on other sites

I should have noticed. The other thing threw me off. Put these statements INSIDE your log() function.var B = document.getElementById("Log1").value;var V = document.getElementById("Log2").value;var Ba = Number(B );var Va = Number(V); Otherwise, you're trying to get the values WHEN THE SCRIPT LOADS, which is probably before those elements even exist, so of course they have no value yet. You need to get the values when the function runs.EDIT. For the same reason, these statements aren't doing anything. You could either set the values in your HTML or put these statements into a window.onload handler:document.getElementById("Log1").value=0;document.getElementById("Log2").value=0;
That's what was wrong with it, thanks.And for the the latter part, I put those in out of desperation thinking that it would make the field a number and not a string.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...