1,105,232 Community Members

document.getElementById problem

Member Avatar
minghags
Junior Poster in Training
55 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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>
Member Avatar
minghags
Junior Poster in Training
55 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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>
Member Avatar
stbuchok
Posting Shark
957 posts since May 2011
Reputation Points: 123 [?]
Q&As Helped to Solve: 142 [?]
Skill Endorsements: 2 [?]
 
1
 
<!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>
Member Avatar
minghags
Junior Poster in Training
55 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you very much. That helped me a lot!

Member Avatar
minghags
Junior Poster in Training
55 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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>
Member Avatar
minghags
Junior Poster in Training
55 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

anyone maybe?

Member Avatar
minghags
Junior Poster in Training
55 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Did it.

Question Answered as of 2 Years Ago by stbuchok
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: