What is a get elements by id:-
The getElementById() method returns the element that has the ID attribute with the specified value.
This method is one of the most common methods in the HTML DOM, and is used almost every time you want to manipulate, or get info from an element on your document.
Returns null if no elements with the specified ID exists.
An ID should be unique within a page. However, if more than one element with the specified ID exists, the getElementById() method returns the first element in the source code.
Code:-
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>getByElementID</h1>
<input type="text" value="20" id='txt1'>
<button type="button" onclick="get_txt();">Get Textbox value</button>
<script>
function get_txt()
{
var txt_val; //Initialize variable
txt_val = document.getElementById('txt1').value;
alert(txt_val);
}
</script>
</body>
</html>
JavaScript:- what is a get elements by id
Reviewed by
Network security
on
May 30, 2019
Rating:
5
No comments: