Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Form Calculation

    I am using a small script i found online, which counds the words in a file i attach to a form and it all works perfectly.

    However i am trying to extend the script to take the value of the word count and make a simple calculation and then display it, however it doesn't work. Sometimes it was showing the value as 'NaN'.

    My only javascript related experience comes from java so i aplogise for poor code.



    Code:
    <script type="text/javascript">
    		function loadDemo() {
    
    		WordCount.words("file_attach", function(words) {
            document.getElementById("words").value = words;
    		});
    
    		var elt = document.getElementById("words");
    		var totalwords = elt.textContent;
    		
    
    		totalwords = parseInt(totalwords);
    
    		var totalprice = totalwords * 12;
    		
    		document.getElementById("tprice").value = totalwords;
    		
    
    
    }
    </script>
    Code:
    <label for="name"><span>Name</span>
    					<input type="text" name="name" id="name" placeholder="Enter Your Name" />
    					</label>
    					
    					<label for="email"><span>Email Address</span>
    					<input type="email" name="email" id="email" placeholder="Enter Your Email" />
    					</label>
    					
    					<label for="file"><span>Attachment</span>
    					<input type="file" name="file_attach" id="file_attach" />
    					</label>
    					
    					<label for="words"><span>No. of Words</span>
    					<input readonly type="number" id="words"/>
    					</label> <br/>
    					
    					<label for="tprice"><span>Price</span>
    					<input readonly id="tprice"/>
    					</label> <br/>
    Anyone with any help or pointers would be great!

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    966
    Thanks
    19
    Thanked 211 Times in 209 Posts
    function loadDemo() {
    WordCount.words("file_attach", function(words) {
    document.getElementById("words").value = words;
    }
    ) ; // should be };

  • #3
    New to the CF scene
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I replaced the }); with }; and the work count stopped working?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by MostDope View Post
    I replaced the }); with }; and the work count stopped working?
    Read and apply what Davey Erwin has told you.

    totalwords = parseInt(totalwords);
    should be
    totalwords = parseInt(totalwords,10);

    Do you intend that
    document.getElementById("tprice").value = totalwords;
    should be
    document.getElementById("tprice").value = totalprice;
    Last edited by Philip M; 05-29-2014 at 07:07 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New to the CF scene
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Below is the code i have now. tprice still shows 'NaN'.

    Code:
    <script type="text/javascript">
    		function loadDemo() {
    
    		WordCount.words("file_attach", function(words) {
            document.getElementById("words").value = words;
    		});
    
    		var elt = document.getElementById("words");
    		var totalwords = elt.textContent;
    		
    
    		totalwords = parseInt(totalwords,10);
    
    		var totalprice = totalwords * 12;
    		
    		document.getElementById("tprice").value = totalprice;
    		
    
    
    }
    		</script>

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    One more time -

    });

    should be }};

    Have you loaded your script after the HTML?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New to the CF scene
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    }}; broke the word count, and yes im using onLoad inside the body?

  • #8
    New to the CF scene
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I dont know if it helps, but i have uploaded the page.

    Proofreading | Distinctive Writers


  •  

    Posting Permissions

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