13 Replies - 421 Views - Last Post: 19 June 2013 - 01:08 PM

#1 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Problems with calling functions in html

Posted 18 June 2013 - 08:55 PM

I am trying to create a calculator through javascript. I am having problems passing the numberArr to get the length of the array, that is created from user input. I am not sure what I am missing in the following code. Any help is greatly appreciated.




<!DOCTYPE html>
<html>
    <head>
	  <script language="javascript">
		function calculate()
		{
		var nums=insertValue(number.value);
		var numbers=calcNum(nums);
		document.getElementById("numOfEle").value = numbers;
	    }
	   function insertValue(num)
	    {
			var num =document.getElementById("number").value;
			var inputArr=num.split(" ");
			var numberArr=new Array();
			
			for(var i=0;i<inputArr.length;i++)
			{
			numberArr[i]=parseInt(inputArr[i]);
			display=numberArr;	
			document.getElementById("display").innerHTML="Elements Entered: " + numberArr;
			};	
				return numberArr;
		};
	   function calcNum(arr)//calculates the number of elements in an array
		{
        return arr.length;
		};		
	</script>
    </head>
    <body>
    <form>
		<h1>Enter data</h1>
		<h2>Separate each number with a space.</h2>
		<input id="number" type="text" placeholder="Enter a number" />
		<br>
		<input id "numOfEle" type="text" name= "numElements">Number of the elements
		<br>
		<input type="button" value="Add" onclick="calculate()" />
    </form>
	<div id="display"></div>
    </body>
</html>

This post has been edited by AllHighway: 18 June 2013 - 09:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Problems with calling functions in html

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: Problems with calling functions in html

Posted 18 June 2013 - 10:15 PM

line #37: ain’t there something missin’?


PS: check the Error Console

This post has been edited by Dormilich: 18 June 2013 - 10:16 PM

Was This Post Helpful? 1
  • +
  • -

#3 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Re: Problems with calling functions in html

Posted 19 June 2013 - 03:31 AM

Is it that the value cannot equal null? That is what the error checking said.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: Problems with calling functions in html

Posted 19 June 2013 - 03:47 AM

you have a very simple typo that leads to the element-does-not-exist error.
Was This Post Helpful? 1
  • +
  • -

#5 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Re: Problems with calling functions in html

Posted 19 June 2013 - 04:22 AM

View PostDormilich, on 19 June 2013 - 03:47 AM, said:

you have a very simple typo that leads to the element-does-not-exist error.


<input id="numbers"

I see it. I'll try it when I get home. Thanks
Was This Post Helpful? 0
  • +
  • -

#6 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 440
  • View blog
  • Posts: 1,867
  • Joined: 30-April 10

Re: Problems with calling functions in html

Posted 19 June 2013 - 07:30 AM

What you need is an equal sign on line 37
<input id="numOfEle"

Was This Post Helpful? 1
  • +
  • -

#7 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Re: Problems with calling functions in html

Posted 19 June 2013 - 07:47 AM

I got it. Thanks for the input.
Was This Post Helpful? 0
  • +
  • -

#8 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Re: Problems with calling functions in html

Posted 19 June 2013 - 11:05 AM

I made the correction, but the code is still not executing.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,866
  • Joined: 12-December 12

Re: Problems with calling functions in html

Posted 19 June 2013 - 11:57 AM

View PostAllHighway, on 19 June 2013 - 06:05 PM, said:

I made the correction, but the code is still not executing.

Your code works fine for me, after inserting the missing equals-sign. Perhaps you should re-post your current code.
Was This Post Helpful? 1
  • +
  • -

#10 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Re: Problems with calling functions in html

Posted 19 June 2013 - 12:02 PM

In the calcNum(arr). I am trying to get the length of the user input array to print in the text box numOfEle.
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,866
  • Joined: 12-December 12

Re: Problems with calling functions in html

Posted 19 June 2013 - 12:07 PM

View PostAllHighway, on 19 June 2013 - 07:02 PM, said:

In the calcNum(arr). I am trying to get the length of the user input array to print in the text box numOfEle.

Yes, as I say, it works for me (in Google Chrome).
Was This Post Helpful? 1
  • +
  • -

#12 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Re: Problems with calling functions in html

Posted 19 June 2013 - 12:27 PM

I got it working now. LOL I was working on some older code and not the revised one. Thanks everyone for the help.

<!DOCTYPE html>
<html>
    <head>
	  <script language="javascript">
		function calculate()
		{
		var nums=insertValue(number.value);
		var numbers=calcNum(nums);
		document.getElementById("numOfEle").value = numbers;
	    }
	   function insertValue()
	    {
			var num =document.getElementById("number").value;
			var inputArr=num.split(" ");
			var numberArr=new Array();
			
			for(var i=0;i<inputArr.length;i++)
			{
			numberArr[i]=parseInt(inputArr[i]);
			//display=numberArr;
			
				
			document.getElementById("display").innerHTML=" Elements Entered: " + numberArr;
			};	
				return numberArr;
		};
	   function calcNum(arr)//calculates the number of elements in an array
		{
		
		return arr.length;
		};		
	</script>
    </head>
    <body>
    <form>
		<h1>Enter data</h1>
		<h2>Separate each number with a space.</h2>
		<input id="number" type="text"  placeholder="Enter a number" />
		<br>
		<input id="numOfEle" type="text" name= "numElements">Number of the elements
		<br>
		<input type="button" value="Add" onclick="calculate()" />
    </form>
	<div id="display"></div>
    </body>
</html>


Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,866
  • Joined: 12-December 12

Re: Problems with calling functions in html

Posted 19 June 2013 - 12:53 PM

View PostAllHighway, on 19 June 2013 - 07:27 PM, said:

I got it working now. LOL I was working on some older code and not the revised one. Thanks everyone for the help.

Actually, this did occur to me, but I thought "no, surely not" ;)
Was This Post Helpful? 0
  • +
  • -

#14 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Re: Problems with calling functions in html

Posted 19 June 2013 - 01:08 PM

Yep, the old code left the return out. smh!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1