JavaScript Ajax beginner confused about DOM reference

I'm learning Ajax and I'm confused about something. In a tutorial, these two lines are included

document.myForm.time.value = ajaxRequest.responseText;
//code
<form name='myForm'>
Name: <input type='text' onChange="ajaxFunction();" name='username' /> <br />
Time: <input type='text' name='time' />
</form>

This code works. I try changing this code to the following

document.tree.innerHTML = document.tree.innerHTML + ajaxRequest.responseText;
//code
<div name='tree' id='tree'></div>

But I get an error "document.tree is undefined". Why?

Answer:1

The document object has a collection of all of the forms on the page, and you can access them by name, which is why document.myForm works. But this does not apply to all elements on the page -- forms are special.

To access your div by id, you can use

document.getElementById("tree")

so your code would become

document.getElementById("tree").innerHTML = document.getElementById("tree").innerHTML + ajaxRequest.responseText;
Answer:2

The reference document.tree tells the browser to look for:

<form name="tree">

What you're looking for instead is

document.getElementById("tree")
Answer:3

document._anything_ is a DOM0 model and it don't have support for all named elements, only forms, images, frames and something more. Now main model is DOM2. Main difference DOM2 from DOM0 is using methods like:

  • document.getElementById
  • document.getElementsByTagName
  • document.getElementsByClassName

instead of document tree walking:

document.anyChild._childOfAnyChild_

In your case better use document.getElementById("tree") instead of document.tree

Answer:4

Change:

document.tree

To:

document.getElementById("tree")

Or better yet, change:

document.tree.innerHTML = document.tree.innerHTML + ajaxRequest.responseText;

To:

var tree = document.getElementById("tree");
tree.innerHTML = tree.innerHTML + ajaxRequest.responseText;
Answer:5

The first method is a throwback to the "dom 0" days before the modern DOM existed. It works because you can reference forms directly by name, but you can't reference other DOM objects the same way. Regardless, this method should be avoided. The better way is to use document.getElementsById('tree'); or, with jQuery, $('#tree').

I strongly recommend reading https://eloquentjavascript.net/

Answer:6

In Sharepoint, when we load the sp.js file after that when we use given code then it gives the current site context. But how can i get the site context using url. var context = new SP.ClientContext....

In Sharepoint, when we load the sp.js file after that when we use given code then it gives the current site context. But how can i get the site context using url. var context = new SP.ClientContext....

  1. clientcontext using c#
  2. using clientcontext sharepoint c#
  3. using clientcontext sharepoint
  4. using client context

I am working with PHP to display my log in form. Right now, the form shows "Username" but the text doesn't go away when the user starts to type. How can I have the text go away when the user starts to ...

I am working with PHP to display my log in form. Right now, the form shows "Username" but the text doesn't go away when the user starts to type. How can I have the text go away when the user starts to ...

  1. show text inside input field
  2. show text inside svg
  3. show text inside div
  4. show text inside password input
  5. show text inside css
  6. show text inside html
  7. tableau show text inside bar
  8. show image inside text box

I'm trying to pass all the values from some selected divs, where the ids of the divs are the values I want to get and aggregate to a textbox, but I'm not getting any result. Basically I want the id ...

I'm trying to pass all the values from some selected divs, where the ids of the divs are the values I want to get and aggregate to a textbox, but I'm not getting any result. Basically I want the id ...

  1. aggregate values in sql
  2. aggregate values in r
  3. aggregate values in excel
  4. aggregate values in tableau
  5. aggregate values in pivot table
  6. aggregate values in power bi
  7. aggregate values in pandas
  8. aggregate values in python
  9. aggregate values in power query
  10. aggregate values in teradata
  11. aggregate values in alteryx
  12. aggregate values in sql server
  13. oracle aggregate values from one column
  14. aggregate of values

I'm trying to refactor a chunk of code that acts on each variable found in the variables array through by means of putting the variable name and its corresponding callback into an object that is ...

I'm trying to refactor a chunk of code that acts on each variable found in the variables array through by means of putting the variable name and its corresponding callback into an object that is ...