Main Tutorials

jQuery – How to get the tag value or element content

In jQuery, you can get the tag value or element content from a selected tag.

For example,

1. Select a tag name of ‘p’ and display its tag value.


    $('p').html();

2. Select an element that has a class name of “class1” and display its value. Regardless of the tag name.


    $('.class1').html();

3. Select an element that has an id of “id1” and display its value. Regardless of the tag name.


    $('#id1').html();

jQuery Example


<html>
<head>
<title>jQuery Get Tag Value</title>
 
<script type="text/javascript" src="jquery-1.3.2.js"></script>
 
</head>
 
<script type="text/javascript">
 
$(document).ready(function(){
    var $temp = $('p').html();
    alert($temp);
	
    var $temp = $('.class1').html();
    alert($temp);
	
    var $temp = $('#id1').html();
    alert($temp);

});

</script>
<body>

<h1>jQuery Get Tag Value</h1>

    <p>
    	This is paragrah 1
    </p>
	
	<div class="class1">
		This is class='class1'
	</div>
	
	<div id="id1">
		This is id='id1'
	</div>

</body>
</html>

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Fuckme
5 years ago

Nice but ?

747547
1 year ago
Reply to  Fuckme

ok

amit
1 year ago

useful