user1544892
user1544892

Reputation:

Get paragraph text inside an element

I want to have the text value from a <p> inside a <li> element.

html:

<ul>
  <li onclick="myfunction()">
    <span></span>
    <p>This Text</p>
  </li>
</ul>

javascript:

function myfunction() {
  var TextInsideLi = [the result of this has to be the text inside the paragraph"];
}

How to do this?

Upvotes: 46

Views: 250405

Answers (10)

Bilal Ahmad
Bilal Ahmad

Reputation: 859

const copyLink = () => {
   console.log('btn click');
   let linkURL = document.getElementById('clipboard').innerText;

}

In p tag:

<p id="clipboard" class="px-1 sm:px-8 py-4 ">
    {`https://offerstreet.in/coupons/coupon/?asin=${asin}`} 
</p>

Upvotes: 0

pepa filip
pepa filip

Reputation: 1

Add an Id property into the P tag with value like text or something:

function gettext() {
    var amount = document.getElementById('text').value;
}

Upvotes: 0

ankit keshari
ankit keshari

Reputation: 159

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Where to JavaScript</title>
    <!-- JavaScript in head tag-->
    <script>
        function changeHtmlContent() {
            var content = document.getElementById('content').textContent;
            alert(content);
        }
    </script>
</head>
<body>
    <h4 id="content">Welcome to JavaScript!</h4>
    <button onclick="changeHtmlContent()">Change the content</button>
</body>

Here, we can get the text content of h4 by using:

document.getElementById('content').textContent

Upvotes: 15

Karol-X
Karol-X

Reputation: 11

If you use eg. "id" you can do it this way:

   (function() {
    let x = document.getElementById("idName");
    let y = document.getElementById("liName");
    
    y.addEventListener('click', function(e) {
        y.appendChild(x);
    });

  
})();
<html lang="en">

<head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
    <p id="idName">TEXT</p>
    <ul>
        <li id="liName">

        </li>
    </ul>
</body>
<script src="js/scripts/script.js"></script>

</html>

Upvotes: 1

Rocky Pulley
Rocky Pulley

Reputation: 23321

Use jQuery:

$("li").find("p").html()

should work.

Upvotes: 1

jay c.
jay c.

Reputation: 1561

Alternatively, you can also pass the li element itself to your myfunction function as shown:

function myfunction(ctrl) {
  var TextInsideLi = ctrl.getElementsByTagName('p')[0].innerHTML;
}

and in your HTML, <li onclick="myfunction(this)">

Upvotes: 59

j08691
j08691

Reputation: 208030

HTML:

<ul>
  <li onclick="myfunction(this)">
    <span></span>
    <p>This Text</p>
  </li>
</ul>​

JavaScript:

function myfunction(foo) {
    var elem = foo.getElementsByTagName('p');
    var TextInsideLi = elem[0].innerHTML;
}​

Upvotes: 1

Tom
Tom

Reputation: 4180

change your html to the following:

<ul>
    <li onclick="myfunction()">
        <span></span>
        <p id="myParagraph">This Text</p>
    </li>
</ul>

then you can get the content of your paragraph with the following function:

function getContent() {
    return document.getElementById("myParagraph").innerHTML;
}

Upvotes: 6

Engineer
Engineer

Reputation: 48813

Try this:

<li onclick="myfunction(this)">

function myfunction(li) {
    var TextInsideLi = li.getElementsByTagName('p')[0].innerHTML;
}

Live demo

Upvotes: 9

casraf
casraf

Reputation: 21694

Do you use jQuery? A good option would be

text = $('p').text();

Upvotes: 16

Related Questions