SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Mar 2014
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    function return value

    Hi guys i am new to js, trying to do simple code and i cant find what is wrong with it. All i want to do is write function answer into label text. How do i do that and why this dont work? i saw alot of tutorials on web where this one works fine, but not to me... help

    HTML Code:
    <html>
    <head>
    <title>Skaiciuokle</title>
    <link rel="stylesheet" type="text/css" href="1.css">
    <script type="text/javascript">
    function calc()
    {
    var moketi, 
        suma = document.getElementById("suma").value*1, 
    	laik = document.getElementById("laikotarpis").value*1;
    
    moketi = suma+laik;
    
    return moketi;
    }
    
    </script>
    </head>
    <body>
        <div id="main">
    	   <form action="" method="post">
    	      skolinama suma:<br>
    	      <input type="number" id="suma"><br>
    		  laikotarpis:
    		  <select id="laikotarpis">
    
    <           <script type="text/javascript">
    		     for(var i=1;i<20;i++)
    			 {
    			 document.write("<option value="+i+">"+i+"</option>")
    			 }
    			 </script>
    		   </select><br>
    		  
    		  <input type="submit" value="apskaiciuoti" onclick="calc()">
    	   </form>		
    	   Moketi per menesy: <label id="label1"><script>document.write(calc()) </script></label> 
    	  
    	 
    	 
    	</div>
    </body>
    </html>
    i tried this one too
    Code JavaScript:
    function calc()
    {
    var moketi, 
        suma = document.getElementById("suma").value*1, 
    	laik = document.getElementById("laikotarpis").value*1;
     
    moketi = suma+laik;
     
    document.getelementbyid(label1).innerHTML= moketi;
    }

    it shoud work> right?????
    Last edited by Mittineague; Mar 16, 2014 at 14:11. Reason: reformatting bbcode tags

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,002
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    Welcome to the forums

    This:

    Code:
    document.getElementById("laikotarpis").value
    will return a string, which you cannot multiply by one (it will return NaN)

    You need to do:

    Code:
    Number(document.getElementById("suma").value)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •