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

Share this post


Link to post
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.

Share this post


Link to post
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"

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...