How to Create Get Content and Attributes in jQuery

In this article, you will learn How to Create Get Content and Attributes in jQuery.

Get Content

  • text()
  • html()
  • val()

Example:-

<html>
<head>
<title>Get Content</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#btn1").click(function(){
    alert("Text: " + $("#test").text());
  });
  $("#btn2").click(function(){
    alert("HTML: " + $("#test").html());
  });
});
</script>
</head>
<body>
<p id="test">In this paragraph having some<b>bolded</b> text.</p>
<button id="btn1">Show Text</button>
<button id="btn2">Show HTML</button>
</body>
</html>

Example 2:-

<html>
<head>
<title>Get Content</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    alert("Value: " + $("#test").val());
  });
});
</script>
</head>
<body>
<p>Name: <input type="text" id="test" value="Hub of Tutorials"></p>
<button>Show Value</button>
</body>
</html>

Get Attributes – attr()

Example:-

<html>
<head>
<title>Get Attributes</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    alert($("#hub").attr("href"));
  });
});
</script>
</head>
<body>
<p><a href="https://huboftutorials.com" id="hub">Hub of Tutorials</a></p>
<button>Show href Value</button>
</body>
</html>

Read on Facebook

Leave a Comment