Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Getting enter to work in HTML text form and more


  • Please log in to reply
3 replies to this topic

#1 A-placid

A-placid

  • Members
  • 9 posts
  • OFFLINE
  •  
  • Local time:01:21 AM

Posted 10 September 2012 - 11:28 AM

Hello! Though this forum section seems to be somewhat dead, I'll try nevertheless (last post made on 5th September. Thought that peole have questions every day :D).


So we started learning basic HTML and scripts, and our homework was to make a really easy calculator (though if people wanted, they could make sophisticated calculators too, if they're able to). Well, and I made a calculator which will calculate the price of gas.

Here's the code:
http://pastebin.com/F2Q5Z2Se

Here's the output:
http://pastebin.me/9c7119338e0214b998a64807220466dc

Now what I wanted to do, is to get this text form to work with by pressing "Enter". At the moment the function will only work if I press the "Enter" button on the page with my cursor, or I'll use the "Tab" button and then press "Enter" on the keyboard.
I'm sure we will learn about this in the future, but I'm wondering if this is very difficult or not. Of course I did a Google search about it, but I'm not sure how I should edit my code, or where should I paste the code, to get it to work.

One thing that would be also cool to get to work is rounding the answers. Say that I want to buy 10 litres of gas, that's 13.819999999999999 euros (10x1.382). It'd be cool if it'd round it to 13.82. But that could be a difficult thing for a beginner.. because I'm just starting with all this.

Cheers!
A-placid

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:21 AM

Posted 10 September 2012 - 01:29 PM

It is not very difficult, but it requires some javascript. Basically, you need to listen to the keyboard for the event you want. Here is a sample that does not require a 3rd party javascript library.

EDIT: Would help to add the link:
http://www.barattalo.it/2010/02/16/how-to-capture-enter-key-pressed-in-a-form-javascript/

Edited by groovicus, 10 September 2012 - 01:30 PM.


#3 rickinjaxfl

rickinjaxfl

  • Members
  • 1 posts
  • OFFLINE
  •  
  • Local time:01:21 AM

Posted 10 September 2012 - 02:10 PM

I do not have a lot of experience with code writing yet but I did notice starting on line #14 of your Resources or in your code you have the following:

<h1>Gas price calculator</h1> <h4>(Octane number 95 - price as of 10th September)</h4> <div> How many litres you want to buy: <input type="text" id="litres" /> Line#14 <input type="button" value="Enter" onclick="calculate()" /> </div> <div id="answer"> </div> </body> </html>
This seems to be telling the program to require "clicking" on the Enter Button to get the answer. You may have another ElementId or TagName you could use to get the desired result. I tried to use the function=calculate instead of onclick but could not make that work either. Hope this helps as it is the only Line on which I can see a possible fix. Rick Harris

#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:21 AM

Posted 11 September 2012 - 07:36 AM

The onclick attribute specifies what should happen when the button is clicked. That behavior is not the same as listening for a keyboard press; or more specifically, if the enter key was pressed. Since we are listening for keyboard events which have no tags, the only way to do it is via javascript. This ensures that no matter what element on the page has the focus,the correct function will be fired when the 'Enter' key is pressed. The document I linked to gives a very simple example of how to accomplish this.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users