I have a feed that is outputting content dynamically to an element. I want to take the text from element A and output it to the console log.
Example:
<div class="elementa">ID5667</div>
Console Output:
ID : ID5667
I've tried a few things, but I'm either getting undefined or the full HTML of that element.
Routine: From the console panel, use a keyboard shortcut (win: Ctrl+f, mac: Cmd+f) to open up the search input UI. Enter any text you'd like to be found in the console.
html on the browser (e.g. chrome), right-click on the browser and click inspect. Click console from inspecting tab, you will see console print out of "Hello Javascript").
Use the textContent property to get the text of an html element, e.g. const text = box. textContent . The textContent property returns the text content of the element and its descendants. If the element is empty, an empty string is returned.
How to print the content of an object in JavaScript ? The JSON.stringify () method is used to print the JavaScript object. JSON.stringify () Method: The JSON.stringify () method is used to allow to take a JavaScript object or Array and create a JSON string out of it.
The first method of printing output is using the Write-Output cmdlet. This cmdlet is used to pass objects in the pipeline to the successive commands. In case of the command being last, the final object is written to the console. To pass on error objects, Write-Error cmdlet is used.
Some of the ways in PowerShell to print are Write-Output, Write-Host, Write-Verbose, Write-Warning and Write-Error. Given below are the different ways of printing output in PowerShell: 1. Write-Output The first method of printing output is using the Write-Output cmdlet. This cmdlet is used to pass objects in the pipeline to the successive commands.
MDN recommends using console.log(JSON.parse(JSON.stringify(obj)))over console.log(obj)for printing objects. This is done to avoid constant updates to the console whenever the value of the object changes. This method won’t work if you try to concat a string with the object, as shown below:
I think below should work for you.
var result = document.getElementsByClassName("elementa")[0].innerHTML;
console.log(result);
For more reference : getElementByClassName
If you are looking for the content of multiple classes you can do this:
var elements = document.getElementsByClassName("className");
for (i = 0; i < elements.length; i++) {
console.log(elements[i].innerHTML);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With