![]() |
textbox onblur get value do calculation
I am trying to teach myself javascript. I found http://www.webmonkey.com/2010/02/Jav...ial_-_Lesson_1 very valuable - the only tutorial I actually understood.
In a different trial, I have succeeded to capture user data with a prompt and then run two conditional if statements... ...but, prompts seem old and dumb to me so I am trying to capture user data with a textbox instead of a prompt and I'm running into failure. Here is what I am trying to do: 1. user puts data into textbox 2. I get value of textbox 3. I do a calculation with that value 4. I write the answer This is what I've got: Enter your birth year: <input type="text" id="birthyear" onblur="age ()"> <script> function age() { var birthyear = document.getElementById('birthyear').value; var today = 2012; var age = today - birthyear; document.writeln (age); } </script> Please don't be too harsh on me ;) If you have a good tutorial to refer me to that'd be awesome too. |
You never asked a question. Your code is fine, it works.
You should be aware of http://www.w3schools.com/ // http://www.w3schools.com/js/default.asp // http://www.w3schools.com/jsref/default.asp |
My question was meant to be: why isn't this working? What am I doing wrong? But I tried it again and it worked so I don't know what went wrong the first time. Thank you for replying!
I'm slowly getting better. This is my latest thing that worked. I'm thinking there has to be a better way though. Code:
<!DOCTYPE HTML> |
Instead of using a span for the total, use a div
Code:
<div id="almondtotal">Total: </div><br>Code:
div{display:inline;}You should limit the number of digits that can be inputed, you can now do infinite and only allow numbers to be inputted. Get rid of the box around the Grand Total . You can get numbers larger than the box size. Automatically figure the Grand Total and display it constantly. Hint: don't put it in a function. |
It never occurred to me that I could combine those 3 scripts. I shall experiment.
I never knew you could limit the number of digits inputted or exclude letters. I shall google a tutorial on that. Don't I need to keep the Grand Total in an input box so the total gets sent with the form data submission? I found that data in divs doesn't get sent...perhaps because I didn't name them, only id'd them. Hmm. I'll have to experiment with adding ids AND names to divs and see if the data gets sent. My newest version has names added to the input values and it submits the values now. As for displaying a running total, that sounds awesome. I'm intrigued that it doesn't involve a function. So I'm thinking it is a variable that is the sum and a variable that is an innerHTML. I shall experiment with that to test it out. On a side note, can the data sent by a form submitted by mailto contain CSS so that when the data shows up in the email it doesn't look like ...crap? Thank you for your guidance sunfighter |
You can't add names to divs - names are only valid on form fields and are what the server side processing uses as the field names for the data that is passed to it.
There is no need to store any calculated value in a hidden field to pass it to the server as the server is going to need to recalculate it anyway for when the form is submitted by people without JavaScript. You are right that you shouldn't be using prompt. The prompt() confirm() and alert() dialogs were repurposed after Netscape 4 dies and are now suitable for debugging use only. Some browsers add checkboxes to them to make their use for debugging easier as you can check the checkbox to disable the dialogs or JavaScript (depending on the browser). document.write and document.writeln went out of use at the same time since innerHTML is able to update the page after it finishes loading . Embedding any JavaScript in the HTML is now unnecessary as you can easily attach the event handlers from within the JavaScript code. For example get rid of the onblur="age ()" form the HTML and add Code:
document.getElementById('birthyear').onblur=age;Ideally you would wrap each JavaScript inside an anonymous function to reduce the chances of them interfereing with other scripts you might add to the page at a later time. To avoid the possibility of script clashes in trying to use the same event handler on the same element for different purposes you can substitute event listeners - but at the moment that involves slightly more code as IE8 and earlier use a different call to do it). You can't submit a form to email using mailto: as modern browsers don't allow it - they will simply open the email program instead and ignore what was filled out in the form. You should use a form2mail script on the server instead to send the form to an email. |
Shut the front door!
You can have javascript-free HTML? What do I google to learn that?!! I'm using the mailto because I don't have a server to work with and I don't think I am ready to learn how to do that yet. I'm still trying to wrap my head around javascript. Mailto is working from my google chrome browser to my Windows Live Mail. Ya, re:names on divs doesn't work. I experimented with that this afternoon and figured out that doesn't work. Quote:
Quote:
So much to learn. So much to learn. |
Ok, from both of your suggestions, this is what I have now.
Code:
<!DOCTYPE HTML> |
Ok, I know this looks dumb replying to myself but I improved some things:
I got the form to calculate on the go. I got a fieldset to be hidden until checkbox checked. |
| All times are GMT +1. The time now is 09:22 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.