Monday, 17 July 2023

getElementById in JavaScript_2

getElementById
  • The getElementById() method returns an element with a specified value.
  • The getElementById() method returns null if the element does not exist.
  • The getElementById() method is one of the most common methods in the HTML DOM. It is used almost every time you want to read or edit an HTML element.
  • If two or more elements with the same id exist, getElementById() returns the first.

Example:
<!DOCTYPE html>
<html>
<head>
<title>Untitled Document</title>
<script type="text/javascript">
function notEmpty()
{
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}
</script>
</head>
<input type='text' id='myText' />
<input type='button' onclick='notEmpty()' value='Form Checker' />
<body>
</body>
</html>

0 comments

Post a Comment