seany123 Posted May 16, 2015 Share Posted May 16, 2015 <script language="javascript" type="text/javascript"> function updatetestid() { var test = document.getElementById('number').value; total = test + 10; document.getElementById('testid').innerHTML = total; } </script> <input type="text" onKeyUp="updatetestid();" name="text" id="number"/> <span id="testid">here</span> currently this will treat #number as a string, but i want to be able to do some maths with the value what am i doing wrong? thanks Quote Link to comment https://forums.phpfreaks.com/topic/296352-convert-inputtext-value-into-a-number/ Share on other sites More sharing options...
CroNiX Posted May 16, 2015 Share Posted May 16, 2015 All HTML form data is a string. So you just need to convert it to a number in javascript before doing math. var test = parseInt(document.getElementById('number').value); //use parseInt() to convert to an integer. There are others for float, etc. total = test + 10; Quote Link to comment https://forums.phpfreaks.com/topic/296352-convert-inputtext-value-into-a-number/#findComment-1512049 Share on other sites More sharing options...
seany123 Posted May 17, 2015 Author Share Posted May 17, 2015 All HTML form data is a string. So you just need to convert it to a number in javascript before doing math. var test = parseInt(document.getElementById('number').value); //use parseInt() to convert to an integer. There are others for float, etc. total = test + 10; thanks for your response thats exactly what i was looking for! i have another problem, i want to display the result in more than 1 place on my page so i created another <span id="testid"></span> but it only shows the result in the first span, should this happen? is there a way to show it in both? Quote Link to comment https://forums.phpfreaks.com/topic/296352-convert-inputtext-value-into-a-number/#findComment-1512052 Share on other sites More sharing options...
seany123 Posted May 17, 2015 Author Share Posted May 17, 2015 i realised that you cant have 2 ids, so i changed them to class however i was wondering if there was a more efficient way of assigning all class' in the array the same value. currently im doing document.getElementsByClassName('testclass')[0].innerHTML = "result here"; document.getElementsByClassName('testclass')[1].innerHTML = "result here"; surely there must be a better way? Quote Link to comment https://forums.phpfreaks.com/topic/296352-convert-inputtext-value-into-a-number/#findComment-1512054 Share on other sites More sharing options...
requinix Posted May 17, 2015 Share Posted May 17, 2015 jQuery. Or another Javascript framework. You're getting into the realm of things that are awkward and tedious to do in plain Javascript. For example, what you're looking for in jQuery is done with $(".testclass").html("result here"); Quote Link to comment https://forums.phpfreaks.com/topic/296352-convert-inputtext-value-into-a-number/#findComment-1512068 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.