Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print content of element to console

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.

like image 884
UserTMP101010 Avatar asked Jan 18 '15 17:01

UserTMP101010


People also ask

How do I get text from an element in browser Console?

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.

Can you print to Console in HTML?

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").

How do I get text from an element?

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?

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.

How to print output from a PowerShell pipeline?

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.

What are the different ways to print in PowerShell?

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.

How do I print JSON objects in MDN?

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:


2 Answers

I think below should work for you.

var result = document.getElementsByClassName("elementa")[0].innerHTML;

console.log(result);

For more reference : getElementByClassName

like image 59
dReAmEr Avatar answered Oct 18 '22 19:10

dReAmEr


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);
}
like image 34
Francisco Costa Avatar answered Oct 18 '22 19:10

Francisco Costa