Methods and properties chaining in JavaScript
Let us have an input:
html<input id="elem" value="text">
Let's display the input text:
javascriptlet elem = document.querySelector('#elem');
console.log(elem.value); // shows 'text'
As you can see, we first get an element
by its id, write this element to
the variable elem, and then display
the property value from this variable
In fact, you can not enter the variable
elem, but build a chain of dots
in this way:
javascriptconsole.log( document.querySelector('#elem').value ); // shows 'text'
In the same way - in a chain - you can overwrite attributes:
javascriptdocument.querySelector('#elem').value = 'www';
Задание 1
Given the following code:
html<img id="image" src="avatar.png">javascriptlet image = document.querySelector('#image');
console.log(image.src);
Modify the code above to use a chain
instead of introducing the variable
image.