Jump to content
Larry Ullman's Book Forums

Recommended Posts

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

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

<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

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.

  • Upvote 1
Link to comment
Share on other sites

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

 Share

×
×
  • Create New...