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>
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments