![]() |
![]() OCAU News - Wiki - QuickLinks - Pix - Sponsors |
|
|||||||
| Notices |
|
Sign up for a free OCAU account and this ad will go away! Search our forums with Google: |
![]() |
|
|
Thread Tools |
|
|
#16 |
|
Member
Join Date: Nov 2010
Posts: 292
|
I hate to be the one to say this, but the advice so far hasn't been good.
JavaScript doesn't have associative arrays. If you want to do associative, use objects. Additionally, putting stuff like onclick in the HTML is terrible practice. It was commonplace 10 years ago but really isn't the way to do things. The same goes for putting style attributes in your tags too btw. Here's an example of the way you should be doing it. HTML: Code:
<!DOCTYPE html> <meta charset="utf-8"> <title>Currency Converter</title> <section> <form> <label for="inputVal">AU$</label> <input id="inputVal" type="text"> <br> <label for="foreign">To:</label> <select id="foreign"> <option>US <option>UK <option>HK <option>SP <option>JP <option>NZ <option>AU </select> <p> Result: <span id="result"></span> </p> </form> </section> <script src="convert.js"></script> Code:
(function x() {
var exchange = {
US: 1.054,
UK: 0.672,
HK: 8.170,
SP: 1.310,
JP: 82.48,
NZ: 1.300,
AU: 1.00
};
document.getElementById('inputVal').addEventListener('keyup', convert);
document.getElementById('foreign').addEventListener('change', convert);
function convert() {
var input = +document.getElementById('inputVal').value;
if (!isNaN(input)) {
document.getElementById('result').innerHTML = (input * exchange[document.getElementById('foreign').value]).toFixed(2);
}
}
}());
The convert function checks that a number has been input, then multiplies it by the required exchange rate before writing the output to the results span. I've wrapped the lot in a function just so that the global object isn't getting hammered with stuff, but this of course is optional. Last edited by asher001; 31st August 2012 at 11:07 PM. |
|
|
|
| Join OCAU to remove this ad! |
|
|
#17 | |
|
Member
Join Date: Jan 2010
Location: Western Sydney
Posts: 912
|
Thanks for contributing, great stuff to think about and look over. My knowledge of JS is basically none, would like to know more.
Just a quick question, what program do you use to write up your code?
__________________
Quote:
Last edited by Leigh M; 31st August 2012 at 11:10 PM. |
|
|
|
|
|
|
#18 | |
|
Member
Join Date: Nov 2010
Posts: 292
|
Quote:
JS is a cool language, lots of crap in it, but plenty of really good parts too. The biggest error people make with JS is approaching it like it is just another C-family language because it shares the syntax. It is not another C-family language, it is really different. |
|
|
|
|
|
|
#19 | ||
|
Member
Join Date: Jan 2010
Location: Western Sydney
Posts: 912
|
Quote:
__________________
Quote:
|
||
|
|
|
|
|
#20 |
|
Member
Join Date: Nov 2010
Location: Wee Waa, NSW
Posts: 305
|
|
|
|
|
|
|
#21 |
|
Member
Join Date: Nov 2010
Posts: 292
|
Sure, both work, but there are best practices. Ideally you want your site structure, layout, and code separate; it makes it much easier to make changes and is far more maintainable.
|
|
|
|
|
|
#22 | |
|
Member
Join Date: Jan 2010
Location: Western Sydney
Posts: 912
|
Also for those that know about making web pages etc, how you think this is going so far, put it together today with just the simple html and css that I know, any suggestions or help on what I'm doing wrong/what I could do instead etc.
Still messing around with it and content just there for filler at the moment. http://shabutie.com/site/index.html (best viewed in google chrome or firefox)
__________________
Quote:
Last edited by Leigh M; 5th September 2012 at 1:05 AM. |
|
|
|
|
|
|
#23 | |
|
Member
Join Date: Nov 2010
Posts: 292
|
Quote:
|
|
|
|
|
|
|
#24 | |
|
Member
Join Date: Jan 2010
Location: Western Sydney
Posts: 912
|
sweet thanks, fixed up a couple of my no px in css but not sure what those errors are from the > stuff
__________________
Quote:
|
|
|
|
|
|
|
#25 |
|
Member
Join Date: Dec 2004
Location: Sydney
Posts: 926
|
You need to end your break tags like this:
<br /> Also you have <h4> inside <p> where it should be instead of <p> |
|
|
|
|
|
#26 |
|
Member
Join Date: Nov 2010
Posts: 292
|
|
|
|
|
|
|
#27 |
|
Member
Join Date: Dec 2004
Location: Sydney
Posts: 926
|
|
|
|
|
![]() |
| Bookmarks |
|
Sign up for a free OCAU account and this ad will go away! |
| Thread Tools | |
|
|