I am calling a function in js to replace the element labelled as ‘test’ in a HTML document, ‘test’ is just a paragraph that reads "Hello world".
function doSomething(){
document.getElementById("test")
.style.color = "magenta";
.textContent = "Goodbye";
It highlights the ‘.’ before textContent, if I remove the ‘.’ the text changes colour but it still says "Hello world"
Very new to JS so ik this probably has some easy fix but I can’t find it anywhere.
4
Answers
I think you have to target the element twice, or this is not your main problem?
.textContent = "Goodbye";
The result of an assignment (
obj.a = b
) is the value that was assigned, not the object you were using (and the;
would have terminated the statement anyway). Instead, save the object reference to a variable/constant, then do two separate assignments:The issue in your JavaScript code is the placement of the period (‘.’) before
.textContent
. You should chain the property assignments without separating them with a period.Sounds like you’re looking for this: