stupid JavaScript question

General discussions and other topics.
7 posts Page 1 of 1
by thomasb » Tue Dec 18, 2012 12:56 pm
I don't know anything about JavaScript and would like to buy a vowel...

Why is this function concatenating instead of adding?

Code: Select all

function DmsToDecLat(FormName) {
    if (FormName == 'plotCircle') {
        var d = document.plotCircle.lat_d.value;
        var m = document.plotCircle.lat_m.value;
        var s = document.plotCircle.lat_s.value;
        document.plotCircle.lat.value = (d+(m/60)+(s/3600));
    } else {
    }
}
Thanks very much!

Thomas
by klui » Tue Dec 18, 2012 1:52 pm
Here's your o.

Try some of the parse functions in Javascript that translates your strings to int/float.
by thomasb » Tue Dec 18, 2012 2:07 pm
Thanks, but I'm still not getting the results I expected.

From what I'm seeing, it's doing the division but concatenating instead of adding. I tried

Code: Select all

    (ParseInt(d)+ParseInt(m/60)+ParseInt(s/3600));
but that didn't work.


Here's a stripped down version of the JavaScript and HTML all on one page in case that triggers anything for anyone: (If you enter 30 in the seconds box, the box at the top displays "380.50" - a concatenation of "38", "0.5" and "0")

Code: Select all

<html>

<body>

<script type="text/javascript">

  function DmsToDecLat() {
      var d = document.plotCircle.lat_d.value;
      var m = document.plotCircle.lat_m.value;
      var s = document.plotCircle.lat_s.value;
      document.plotCircle.lat.value = (d+(m/60)+(s/3600));
  }

  </script>

<form action="/cgi/do_something" method="GET"
  name='plotCircle'>

Lat: <input name="lat" size="18" value="38"><br />

<br /><hr>Use the boxes below to convert degrees, minutes and seconds to 
      fractions of a degree:
Lat: 
<input name="lat_d" size="4" value=38 onBlur="DmsToDecLat('plotCircle')">° 
<input name="lat_m" size="4" value=0 onBlur="DmsToDecLat('plotCircle')">'
<input name="lat_s" size="4" value=0 onBlur="DmsToDecLat('plotCircle')">"

</form>

</html>
by thomasb » Tue Dec 18, 2012 9:26 pm
OK, this is totally retarded but it works:

Code: Select all

document.plotCircle.lat.value = (d - (-1)*(m/60) - (-1)*(s/3600));
(subtracting the negative instead of adding...)


Love to know where I went wrong with

Code: Select all

document.plotCircle.lat.value = (ddeg+(dmin/60)+(dsec/3600));
(why it concatenates instead of adding...)
by rlvh » Wed Dec 19, 2012 10:15 am
You would want to make sure that it is being stored as an int in the variable. Something like:

Code: Select all

var d = ParseInt(document.plotCircle.lat_d.value);
by klui » Wed Dec 19, 2012 3:03 pm
thomasb wrote:Love to know where I went wrong with

Code: Select all

document.plotCircle.lat.value = (ddeg+(dmin/60)+(dsec/3600));
(why it concatenates instead of adding...)
More appropriate if you ask at http://www.reddit.com/r/javascript/ Use the resources at the right side of that subreddit.
by thomasb » Thu Dec 20, 2012 10:04 am
Thanks for the suggestions everyone. Part of my original issue was that I typed "ParseInt" when what I really wanted was "parseInt"

Code: Select all

document.plotCircle.lat.value = (parseInt(d)+(m/60)+(s/3600));    //works

Code: Select all

document.plotCircle.lat.value = ((d/1)+(m/60)+(s/3600));    // also works
(dividing d by one forces a numeric instead of a string)

Code: Select all

d
by itself always seems to get treated as a string...
7 posts Page 1 of 1

Who is online

In total there are 11 users online :: 0 registered, 0 hidden and 11 guests (based on users active over the past 5 minutes)
Most users ever online was 999 on Mon May 10, 2021 1:02 am

Users browsing this forum: No registered users and 11 guests