Skip to content Skip to sidebar Skip to footer

Accesing Dom Elements Through Document.x

Where can I see more about this? I thought that you had to do document.getElementById always for interacting with an element. source (Getting data from Google docs into Javascript)

Solution 1:

Use the document.querySelector:

functionData(response) {
  document.querySelector('[name=tName]').value = response.feed.entry[0].gs$cell.$t; 
}

document.querySelector

Returns the first Element within the document that matches the specified selector, or group of selectors.


document.querySelector('[name=tName]')

This will select the element with the attribute name and the value tName

Solution 2:

As other posters have mentioned, there are a few ways to access DOM elements.

If you're just looking at accessing form elements, take a look at HTMLFormElement.elements (MDN).

The HTMLFormElement.elements property returns an HTMLFormControlsCollection (HTML 4 HTMLCollection) of all the form controls contained in the FORM element, with the exception of input elements which have a type attribute of image.

You'll get an neat collection of your form elements, which you can access either directly via their name attributes, or by iterating over the collection.

var formElements = document.querySelector('#some-form').elements;

console.log(formElements['some-form__input-yo'].value); // form element value// ORfor (let element of formElements) {
   console.log(element.value); // form element value
}

https://jsfiddle.net/93nemsfq/

Post a Comment for "Accesing Dom Elements Through Document.x"