I have an object that contains the main data for my script. For example:
Expand|Select|Wrap|Line Numbers
- function Invoice() {
- this.name="bob"
- this.email="abc@example.com"
- }
I've been browsing online but haven't been able to find an example - or least not one I recognise due to my OO and JS newbie-ness. Here's some sample code:
Expand|Select|Wrap|Line Numbers
- function process_input(element) {
- // This function checks input and gives and assigns valid input to a
- // namesake property of the Invoice object
- var regex = new Patterns(); // an object full of regexs to match against input
- var order = new Invoice();
- var value = document.getElementById(element).value;
- var i;
- for (i in regex) {
- if (element === i && value.match(regex[i])) {
- order[element] = value;
- return true;
- }
- }
- return false;
- }
- function Invoice() {
- // Class with properties created by process_input
- }
- function main() {
- var myForm = document.getElementById('orderform');
- for (var i = 0; i < myForm.elements.length; ++i) {
- var e = myForm.elements[i];
- // Ignore element unless it is a 'text input' box or a 'select' combo box
- if (e.type === "text" || e.type === "select-one") {
- if (process_input(e.id)) {
- e.setAttribute("class", "valid");
- }
- }
- }
- }
Should I be looking at making process_order() a method of the Invoice object? Or do I need the Invoice object to be accessible globally?
Hopefully, I'm making sense :-)