ionezation Posted January 13, 2014 Share Posted January 13, 2014 This is my HTML <form id="form" method="post"> First Name : <input type="text" id="fname" name="fname"> <br> Last Name : <input type="text" id="lname" name="lname"> <br> Department : <select name="department" id="department"> <option value="accounts">Accounts</option> <option value="hr">HR</option> <option value="it">Information Technology</option> <input type="submit" id="submit" value="Add Employee"> <div id="output"></div> </form> </select> This is my CSS ! function calculate(){ 'use strict'; var fname = document.getElementById('fname').value; var lname = document.getElementById('lname').value; var depart = document.getElementById('department').value; var output = document.getElementById('output'); var employee = { firstname:fname, lastname:lname, department:depart, hireDate:new Date() }; var msg = '<h2>Employee Added</h2> Name : ' + employee.firstname + employee.lastname + '<br>'; msg += 'Department : ' + employee.department + '<br>'; msg += 'Hire Date : ' + employee.hireDate.toDateString(); output.innerHTML = msg; } function init() { 'use stric'; var form = document.getElementById('form'); form.onsubmit = calculate; }; // end init() window.onload = calculate; For unknown reason i cant figured it out what is the problem please help Link to comment Share on other sites More sharing options...
HartleySan Posted January 13, 2014 Share Posted January 13, 2014 Trying moving your </select> tag to after the last </option> tag. Link to comment Share on other sites More sharing options...
ionezation Posted January 14, 2014 Author Share Posted January 14, 2014 It doesn`t work either hartley ! this is from chapter 6 [complex variable type] on page 212, its about working with array. Before this example there also one in this book that is also not working it is on page 196 chapter 6 [complex variable type] and it is for working with objects Link to comment Share on other sites More sharing options...
HartleySan Posted January 14, 2014 Share Posted January 14, 2014 Another thing is that you're not preventing the default form submission. When you submit the form, do you get a page reload? Link to comment Share on other sites More sharing options...
ionezation Posted January 14, 2014 Author Share Posted January 14, 2014 Yes, even after this it is not working. When i click add employee it reloads the page but doesn`t update the output field at all. Link to comment Share on other sites More sharing options...
ionezation Posted January 14, 2014 Author Share Posted January 14, 2014 one mistake i had that the misspell of 'strict' in Function init(); function init() { 'use stric'; var form = document.getElementById('form'); form.onsubmit = calculate; }; // end init() Link to comment Share on other sites More sharing options...
HartleySan Posted January 14, 2014 Share Posted January 14, 2014 That's my point. If the page reloads completely, then you're doing it wrong. JS should not cause a page reload, thus you need to prevent the default form submission to get your JS to work properly. Link to comment Share on other sites More sharing options...
ionezation Posted January 14, 2014 Author Share Posted January 14, 2014 now i have return false; after output.innerHTML = msg;return false; } but still not working :s Link to comment Share on other sites More sharing options...
HartleySan Posted January 14, 2014 Share Posted January 14, 2014 What errors are output to the console? Link to comment Share on other sites More sharing options...
ionezation Posted January 14, 2014 Author Share Posted January 14, 2014 no errors at all hartley, I just seeing name: department:accounts Hiring Date : is the current date time. thats it :x Link to comment Share on other sites More sharing options...
HartleySan Posted January 15, 2014 Share Posted January 15, 2014 Are you saying that the JS is catching the form submit, and then outputting the department and the hiring date, but not the first and last name? Please do me the following favor: In Chrome, load your console up, right-click in the console and select "Preserve log upon navigation", and then add the following line to your code and execute your code: var msg = '<h2>Employee Added</h2> Name : ' + employee.firstname + employee.lastname + '<br>'; msg += 'Department : ' + employee.department + '<br>'; msg += 'Hire Date : ' + employee.hireDate.toDateString(); console.log(employee.firstname, employee.lastname, employee.department, employee.hireDate); output.innerHTML = msg; Please let us know what you find. Thank you. Link to comment Share on other sites More sharing options...
ionezation Posted January 15, 2014 Author Share Posted January 15, 2014 Yes hartleysan, after the script you have submitted it shows this Employee Added Name : undefinedundefined // undefined first and last name. They are still undefinedDepartment : IT // It is changing nowHire Date : Wed Jan 15 2014 // date is this In console it is that // undefined undefined "accounts" Wed Jan 15 2014 06:22:11 GMT-0800 (Pacific Standard Time) i have found one error in my code and that was here in : function init() { 'use strict'; var form = document.getElementById('form'); form.onsubmit = calculate; }; // end init() window.onload = calculate; // Instead of init; i placed here calculate(); Link to comment Share on other sites More sharing options...
HartleySan Posted January 15, 2014 Share Posted January 15, 2014 Well, it sounds like you're not properly capturing the two names. Is that a fair judgment call to make? Link to comment Share on other sites More sharing options...
ionezation Posted January 15, 2014 Author Share Posted January 15, 2014 let me check again ! i dint change the document.getelementbyID(); method at all var fname = document.getElementById('fname').value; Link to comment Share on other sites More sharing options...
HartleySan Posted January 22, 2014 Share Posted January 22, 2014 ionezation, thanks for sending me your code. After getting the real code and being able to test it out myself, I quickly found the problem. You need to change the following line: var msg = '<h2>Employee Added</h2> Name : ' + employee.firstname + employee.lastname + '<br>'; As follows: var msg = '<h2>Employee Added</h2> Name : ' + employee.firstName + employee.lastName + '<br>'; In other words, firstname and lastname need to be changed to their camel case variations firstName and lastName. While those two simple changes will get your code working, there are a number of other things I would do to change your code, but I'll leave it at that for now. 1 Link to comment Share on other sites More sharing options...
ionezation Posted January 25, 2014 Author Share Posted January 25, 2014 Thanks hartleySan, because of your efforts i finally made through the problem and moving forward in my pursue of Javascript Knowledge. Thank you very much for helping me out. Anu Link to comment Share on other sites More sharing options...
HartleySan Posted January 25, 2014 Share Posted January 25, 2014 You're welcome. In the future though, try using the JavaScript console in Chrome, etc. to debug your code. If you don't know how to use such tools, I highly recommend learning, as they are invaluable for front-end development. Link to comment Share on other sites More sharing options...
ionezation Posted January 26, 2014 Author Share Posted January 26, 2014 Thanks again HartleySan, You i cant understand the console errors .. sometimes it crossed me so i copy error and paste it on google to get it know how but few times i stuck. Link to comment Share on other sites More sharing options...
HartleySan Posted January 26, 2014 Share Posted January 26, 2014 With most (if not all) errors in the Chrome console, they also print out a line number in your code. You can click on that line number to view the exact spot where the error occurred in your code. You can also set up breakpoints, etc. in your code from Chrome Dev Tools. Link to comment Share on other sites More sharing options...
Recommended Posts