0

I have an html document that contains 4 paragraphs.

I want to take the concatenation of all paragraph's innerHTML and put it in the paragraph with "demo" id. The problem is that it also prints the function's signature...

How can I print only the string that my function returns?

<!DOCTYPE html>
<html>
<body>

<p>Hello World!</p>

<p>The DOM is very useful.</p>
<p>This example demonstrates the <b>getElementsByTagName</b> method</p>

<p id="demo"></p>

<script >
    var paragraphs = document.getElementsByTagName("p");


    function getText() {
        var text=null;
        for (var i = 0; i < paragraphs.length; i++) {
            getText = getText + paragraphs[i].innerHTML;
        }
        return getText;
    }

    document.getElementById("demo").innerHTML=getText();

</script>

</body>
</html>

I get this as a result:

Hello World!

The DOM is very useful.

This example demonstrates the getElementsByTagName method

function getText() { var text=null; for (var i = 0; i < paragraphs.length; i++) { getText = getText + paragraphs[i].innerHTML; } return getText; }Hello World!The DOM is very useful.This example demonstrates the getElementsByTagName method

2
  • use a different variable name for the text inside the function Commented Sep 8, 2016 at 10:23
  • try to avoid innerHTML. It adds an unnecessary serialization and parsing step to your task (overhead), and if for example some other script would have added event-listener to some of the processed nodes/markup, they would have been gone now; they don't serialize. Better would be to accumulate the childNodes you want in a DocumentFragment (the accumulation prevents multiple render-steps), and to just move/append them to the target Node (demo). Commented Sep 8, 2016 at 10:46

3 Answers 3

3

It's because you're returning the function, not the text.

// Main function declaration
function getText() {

    // Setting the initial string (text) and all paragraphs (<p>)
    var text = "";
    var paragraphs = document.getElementsByTagName('p')

    // Loop over all paragraphs
    for (var i = 0; i < paragraphs.length; i++) {
        // use the .concat() function to combine the strings (with a space behind it)
        text = text.concat(paragraphs[i].innerHTML + " ");
    }

    // Return the string in variable text as a result of the function.
    return text;
}

// Enter the string in the demo p.
document.getElementById("demo").innerHTML = getText();

Here's a codepen (working) example.

Sign up to request clarification or add additional context in comments.

4 Comments

This is slightly better, but it'll still prepend "null" to the return value.
Yeah, i'll edit the answer in a second, this isn't favorable anyway.
Now it'll prepend "undefined".
I haven't finished my coffee @Juhana, thanks for correcting. Should be fine now, updated + comments!
2

Replace your function with :

function getText() {
        var text="";
        for (var i = 0; i < paragraphs.length; i++) {
            text= text+ paragraphs[i].innerHTML;
        }
        return text;
    }

In your case, you were returning getText which is name of your function that's why the method is also getting appended

3 Comments

Explanation will help the OP understand the issue, instead of just providing answer
@Pugazh I also provided the explanation of his issue with answer
Great, this would help!
0

You have to return the variable 'text' not getText();

<!DOCTYPE html>
<html>
<body>

<p>Hello World!</p>

<p>The DOM is very useful.</p>
<p>This example demonstrates the <b>getElementsByTagName</b> method</p>

<p id="demo"></p>

<script>
    var paragraphs = document.getElementsByTagName("P");

    var text = "";

    function getText() { 
        var i; 
        for (i = 0; i < paragraphs.length; i++) {
           text += paragraphs[i].innerHTML;
        }
        return text;
    }

    document.getElementById("demo").innerHTML = getText();

</script>

</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.