jQuery <p> get text

Description

jQuery <p> get text

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Get Text Contents of the Elements</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
    $(".btn-one").click(function(){
        var str = $("p").text();
        document.getElementById("demo").innerHTML = str;
    });// w w  w.j a v a  2 s .c  o m
    $(".btn-two").click(function(){
       var str = $("p:first").text();
       document.getElementById("demo").innerHTML = str;
    });
    $(".btn-three").click(function(){
       var str = $("p.extra").text();
       document.getElementById("demo").innerHTML = str;
    });
});
</script>
</head>
<body>
    
    <p id="demo"></p>
    
    
     
    <button type="button" class="btn-one">Get All Paragraph's Text</button>
    <button type="button" class="btn-two">Get First Paragraph's Text</button>
  <button type="button" class="btn-three">Get Last Paragraph's Text</button>
    <p>This is a paragraph.</p>
    <p>This is another paragraph.</p>
  <p class="extra">This is one more paragraph.</p>
</body>
</html>



PreviousNext

Related