To get an element by name in JavaScript, you can use the getElementsByName()
method provided by the Document Object Model (DOM).
Here is an example code block that demonstrates how to use this method:
index.tsx172 chars7 lines
In this example, we use the getElementsByName()
method to get the first element with the name "example". We then modify the element by changing its text color to red and setting its inner HTML to "Hello, world!".
Note that getElementsByName()
returns a collection of elements with the same name, so we need to specify the index of the element we want to access (in our case, we're accessing the first element with [0]
).
gistlibby LogSnag