Jump to content

Colin G

Members
  • Posts

    4
  • Joined

  • Last visited

Colin G's Achievements

Newbie

Newbie (1/7)

0

Reputation

  1. Noted. Sticking with "document.getElementByID" with every element having a specific id. <form> gives a convenient way to clear field elements within it so don't have to set them individually, but will use "getElement" to obtain individual values. I don't support browser specific behaviour, I try and stick to standards.
  2. Playing with forms and getting input. Am using a <form> to contain elements for the convenience of being able to click <input> type "clear" to wipe <input> values ready for re input. Now if I use "getelementbyid" I can get a value from anywhere with a matching id, don't need a form specifically. Then found some code that submits a form to javascript by calling myFunction(form) function myFunction(form) { myvar = form.elementName.value ; I presume the whole form 'object' is passed to the function. The code is using element name not id, but if you delete the "name" in an element and replace with "id" and do the same in the function it still works. Obviously the above is shorter than the whole "document.getelementbyid..." etc. But then found that myvar = input1.value; where "input1" is the element id works as well and there is no need to pass "form" to the function or have a long dot description. Just trying to keep code as short and as readable as possible, but still be valid javascript and there seems to be a number of options! Also would have more than one "form" on same page. Of course "id" should be unique for each element anyway so is a lot of the long 'naming' unnecessary? Comments?
  3. "input99" is validated as type "number" Found the "var decPlaces = document.getElementById("input99"); " is missing the '.value' at the end, so result can be unpredictable . Have fixed it now, tested inputting number of decimal places as 3.99 and it still comes out as 3 dp, so must be some integer validation in the method providing it is sent an actual number. Fixed issue now in http://www.alchymy.com/Astrolab/cCalc/cCalc009TScope.html Thanks
  4. Was testing toFixed() in 'Tryit' , basically want to be able to input the number of decimal places to display in the output. Have been using this with "toExponential" in many pages with no issues, but seemed not to work with "toFixed". The code below can be swapped by changing the comment lines to test, so either you ask for the number of dp or it can be set by "var decPlaces". However now results seem erratic, it may or may not work in either method - the dp may be shown or it gets rounded to 6 and sometimes it work for the input dp or the declared dp?? Also does 'var num' not have to be declared as a 'new Number' to get the '.toXXX' methods or does declaring var = 1; automatically create a number object? Its like the dec places is sometimes being picked up as a number and sometimes not. <!DOCTYPE html> <html> <body> <p>Click the button to display the fixed number.</p> <input type="number" id="input99" size="5" value="2" /> // input number of decimal places <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var decPlaces = document.getElementById("input99"); // var decPlaces = 4; var num = 5.56789; var n = num.toFixed(decPlaces); // var n = num.toExponential(decplaces); document.getElementById("demo").innerHTML = n; } </script> </body> </html>
×
×
  • Create New...