We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,633 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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>
2
Contributors
6
Replies
12 Hours
Discussion Span
1 Year Ago
Last Updated
7
Views
Question
Answered
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&#353;&#269;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&#353;&#269;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

anyone maybe?

minghags
Junior Poster in Training
55 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Did it.

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

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1423 seconds using 2.71MB