WinstonMuijs Posted August 7, 2012 Share Posted August 7, 2012 To get a clear picture of it all i have a couple of questions about javascript in general. example: var quantity = document.getElementById("quantity") --> document.getElementById is a method to call for a form element object? --> var quantity is a reference of that form element object quantity? var quantity = document.getElementById ("quantity").value --> document.getElementById("quantity") is still a form element object? --> var quantity is still a reference of the form element object quantity? --> var quantity gets the property "value" of the form element object quantity? --> So the value of var quantity is the property value? Thank you all Greetz Link to comment Share on other sites More sharing options...
HartleySan Posted August 7, 2012 Share Posted August 7, 2012 document is a predefined object in JS with various properties and methods. One of its methods is getElementById. When you use document.getElementById, a reference to the HTML element (which does not always correspond to a form element) with the specified ID is returned, and can be assigned to a variable for use later, if you wish to do so. In some cases, for example, document.getElementById is used to get a reference to a textarea element, the value property will be made available for the reference returned by document.getElementById. Note that the value property is not available for all HTML element references in JS, just some. Link to comment Share on other sites More sharing options...
margaux Posted August 7, 2012 Share Posted August 7, 2012 <li id="quantity">Qty</li> document.getElementById("quantity") gets ANY HTML element that has an id equal to "quantity, not just a form element. e.g. it would get the above list element. using the above html code example var quantity = document.getElementById("quantity") var quantity would be set to "li" and var quantity = document.getElementById("quantity").value var quantity would be set to "Qty" Link to comment Share on other sites More sharing options...
WinstonMuijs Posted August 7, 2012 Author Share Posted August 7, 2012 If i understand you correctly, then all the questions can be answered with a positive or a true statement? All in the given example... Thanks for your answer...! Link to comment Share on other sites More sharing options...
HartleySan Posted August 7, 2012 Share Posted August 7, 2012 To clarify a bit more, in margaux's example, quantity in "var quantity = document.getElementById("quantity")" is equal to a Javascript DOM object, which through the use of the DOM (an API that is separate from JS but able to interact with JS) can be mapped to an actual HTML element of the specified ID. So in reality, getElementById always returns a JS object, and nothing more, but through the magic of the DOM API and its ability to interact with JS, behind the scenes, that simple JS object returned by getElementById is able to be mapped to the corresponding HTML element, which can be further modified through the use of the returned JS object's associated properties and methods. I made that sound a lot more confusing than it should have been, but I can't think of a better way to explain it. Ugghh! All I really wanted to say is that quantity in "var quantity = document.getElementById("quantity")" is not equal to the string "li", but is a reference to the corresponding li element in the DOM. 1 Link to comment Share on other sites More sharing options...
WinstonMuijs Posted August 7, 2012 Author Share Posted August 7, 2012 Its all about the reference. Not the actual object, but a reference to that object. JS is through the DOM able to create a reference of an object in HTML. Thanks... you're a great explainer. Link to comment Share on other sites More sharing options...
HartleySan Posted August 8, 2012 Share Posted August 8, 2012 Glad I could be of help. As a side note, it's worth noting that objects, arrays and functions are all passed and handled by reference in JS (which is akin to many other programming languages). Obviously, this introduces many important gotchas that you don't ever experience with numbers, strings or Boolean values, which are all passed by value. Link to comment Share on other sites More sharing options...
WinstonMuijs Posted August 8, 2012 Author Share Posted August 8, 2012 Ok! I have another question looking at your side note: Are numbers, strings and boolean values the primitive data in JS? Thanks Link to comment Share on other sites More sharing options...
HartleySan Posted August 8, 2012 Share Posted August 8, 2012 Yes they are, and functions, arrays (which are technically a specialized type of object) and objects are composite values, as I believe they are called. Link to comment Share on other sites More sharing options...
WinstonMuijs Posted August 8, 2012 Author Share Posted August 8, 2012 Ok! Perfect... I'm learning JS from scratch, so i've to build a new network in my mind. It's not about the code, but understanding the stucture, ideas and principles behind it. But i'm beginning to see some light in the dark. Thanks HartleySan Link to comment Share on other sites More sharing options...
HartleySan Posted August 8, 2012 Share Posted August 8, 2012 Good to hear. You seem to be doing just fine. Keep up the good work! Link to comment Share on other sites More sharing options...
Recommended Posts