Getting element by id in javascript

To get an element by its id we use getElementById method available with document object. It takes Id of element which we want to target. If element is not found it returns null.

<input type="button" value="Get By Id" onclick="GetByID()" />
<div id="myDiv">
    This is a div
</div>

<script type="text/javascript">

function GetByID() {
 var element = document.getElementById("myDiv");
  if (element != null) {
   alert(element.innerHTML);
  }
}
</script>


Comments

Popular posts from this blog

Using Progress Bar In C#

Check if ViewBag is null or doesn't exist

Jquery serer side datatables in asp.net