How to get the text from a DIV tag using JavaScript (no jQuery)

Using jQuery you can do many thing easily but sometimes you may not need to use jQuery for a small Application.

Okay then without using jQuery how can i get the text from a DIV tag?.

Its very simple you can get the text from a DIV tag using Javascript without  using any library like jQuery etc.,

Here is the code:


document.getElementById('superman').innerHTML;

You may tried yourself to get the text from a DIV tag using ‘.value’ but actually you should use ‘.innerHTML’

Here is the example:


<html>
<head>
<script type="text/javascript">
function test() {
var t = document.getElementById('superman').innerHTML;
alert(t);
}
</script>
</head>
<body>
<input type="button" value="show text" onclick="test()" />
<div id="superman">This is the text from a div</div>
</body>
</html>

I hope this would have helped you in a critical situation.

Add a Comment

Your email address will not be published. Required fields are marked *