document.getElementById problem
i need to sum anything that is input in input form numbers. And this has to be in order like 5,4,3 and then it has to sum like 5+4+3 = 12. Can anyone help? I came as far as the code shows above.
this is the example of how it should be: link
thanks
The code:
<html>
<head>
<title>Vaja 6.2</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type='text/javascript'>
function preveri(elem)
{
var iskanje=prompt("Vnesi iskano stevilo:");
var numericExpression = iskanje;
if (elem.value.match(numericExpression))
{
alert('Stevilo je v seznamu');
}else
{
alert('Stevilo ni v seznamu');
}
}
function sestej()
{
document.getElementById(numbers);
document.write(numbers);
}
</script>
</head>
<body >
<form>
<input type='text' id='numbers'/>
<input type='button' onclick="preveri(document.getElementById('numbers'))", value='Išči' />
</form>
<script>
sestej();
</script>
</body>
</html>
Related Article: document.getElementById innerhtml problem
is a JavaScript / DHTML / AJAX discussion thread by DILO3D that has 4 replies, was last updated 1 year ago and has been tagged with the keywords: doc.getelementbyid, innerhtml, javacript.
minghags
Junior Poster in Training
55 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Please can anyone help? I got this far:
<html>
<head>
<title>Vaja 6.2</title>
<script type='text/javascript'>
function preveri(elem)
{
var iskanje=prompt("Vnesi iskano stevilo:");
var numericExpression = iskanje;
if (elem.value.match(numericExpression))
{
alert('Stevilo je v seznamu');
}else
{
alert('Stevilo ni v seznamu');
}
var str=elem.value;
document.getElementById('span').innerHTML = document.write(str.split(",") + "<br />");;
}
</script>
</head>
<body >
<form>
<input type='text' id='numbers'/>
<input type='button' onclick="preveri(document.getElementById('numbers'));", value='Išči' /><span id="span"></span>
</form>
</body>
</html>
minghags
Junior Poster in Training
55 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html>
<head>
<title></title>
<script>
function calculate(){
var numbers = document.getElementById('txtNumbers').value.replace(' ', '').split(',');
var total = 0;
for(var i = 0; i < numbers.length; i++){
total += parseFloat(numbers[i]);
}
document.getElementById('results').innerHTML = total.toString();
}
</script>
</head>
<body>
<input id="txtNumbers" type="text" /><button onclick="calculate();">Calculate</button><span id="results"></span>
</body>
</html>
stbuchok
Practically a Posting Shark
875 posts since May 2011
Reputation Points: 138
Solved Threads: 124
Skill Endorsements: 2
Thank you very much. That helped me a lot!
minghags
Junior Poster in Training
55 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
If you could help me with one more problem in same program i would really appreciate it. Thanks
Upgrade Javascript so that when you click on the button with the first regular expression to check whether all input box containing the appropriate list of numbers (or a list of correctly spelled and separated by commas). Use the following regular expression: (^ [0-9] (\ s * [0-9 ])+$). To check, use regexp class. In case of wrong input, notify user (in the element span) and the entry and search is not made.
BTW, i did like this:
<html>
<head>
<title>Vaja 6.2</title>
<script type='text/javascript'>
function preveri(elem)
{
var iskanje=prompt("Vnesi iskano stevilo:");
var numericExpression = iskanje;
if (elem.value.match(numericExpression))
{
alert('Stevilo je v seznamu');
}else
{
alert('Stevilo ni v seznamu');
}
}
function izracun(){
var numbers = document.getElementById('numbers').value.replace(' ', '').split(',');
var total = 0;
for(var i = 0; i < numbers.length; i++){
total += parseFloat(numbers[i]);
}
document.getElementById('rezultat').innerHTML = total.toString();
}
</script>
</head>
<body >
<form>
<input type='text' id='numbers'/>
<input type='button' onclick="preveri(document.getElementById('numbers')); izracun();", value='Išči' /><span id="rezultat"></span>
</form>
</body>
</html>
minghags
Junior Poster in Training
55 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
minghags
Junior Poster in Training
55 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
minghags
Junior Poster in Training
55 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by
stbuchok