43-JavaScript References
43-JavaScript References
Syntax:
window.document.images[index].src= "path";
window.document.forms[index].elements[index].value=""
Good:
- Accessing with position is easy.
- Accessing in sequential order is easy
- Good for testing.
Bad:
- If you change the position of any element in page, then you have to update
its index in code.
Ex:
<!DOCTYPE html>
<html>
<head>
<title>DOM Hierarchy</title>
<script type="text/javascript">
function bodyload(){
window.document.images[0].src="../public/images/speaker.jpg";
window.document.images[0].width = "200";
window.document.forms[0].elements[0].value="Register";
window.document.forms[1].elements[1].value="Login";
}
</script>
</head>
<body onload="bodyload()">
<div>
<img width="100" height="100" border="1">
</div>
<div>
<form>
<h2>Register</h2>
<input type="button">
<input type="email">
</form>
</div>
<div>
<form>
<h2>Login</h2>
<input type="text">
<input type="button">
</form>
</div>
</body>
</html>
Bad
- Name can be common for multiple elements.
- If name is common then attributes are not applied.
- You can't refer child element directly.
- If child element is defined in a parent, you have to refer parent and child.
Ex:
<!DOCTYPE html>
<html>
<head>
<title>DOM Hierarchy</title>
<script type="text/javascript">
function bodyload(){
pic.src="../public/images/shoe.jpg";
frmRegister.btnRegister.value="Register";
window.document.forms[1].btnLogin.value="Login";
}
</script>
</head>
<body onload="bodyload()">
<div>
<img width="100" name="pic" height="100" border="1">
</div>
<div>
<form name="frmRegister">
<h2>Register</h2>
<input type="email">
<input type="button" name="btnRegister">
</form>
</div>
<div>
<form>
<h2>Login</h2>
<input type="text">
<input type="button" name="btnLogin">
</form>
</div>
</body>
</html>
3. Refer by using ID
- Every element can be defined with ID.
- ID is unique for JavaScript but not for CSS.
Good:
- You can refer any HTML element directly
document.getElementById("id")
- No more hierarchy issues
Bad:
- ID can be used by CSS and JavaScript
- CSS Id can be common for multiple elements
Ex:
<!DOCTYPE html>
<html>
<head>
<title>DOM Hierarchy</title>
<script type="text/javascript">
function bodyload(){
document.getElementById("pic").src="../public/images/speaker.jpg";
document.getElementById("btnRegister").value = "Register";
document.getElementById("btnLogin").value = "Login";
}
</script>
</head>
<body onload="bodyload()">
<div>
<img width="100" id="pic" name="pic" height="100" border="1">
</div>
<div>
<form name="frmRegister">
<h2>Register</h2>
<input type="email">
<input type="button" id="btnRegister" name="btnRegister">
</form>
</div>
<div>
<form>
<h2>Login</h2>
<input type="text">
<input type="button" id="btnLogin" name="btnLogin">
</form>
</div>
</body>
</html>
Good:
- Various technique to refer HTML elements
- You can access by name, id, class etc..
- You can also access by using attribute, pseudo classes..
Bad:
- Common name issues
Ex:
<!DOCTYPE html>
<html>
<head>
<title>DOM Hierarchy</title>
<script type="text/javascript">
function bodyload(){
document.querySelector("img").src="../public/images/jacket.jpg";
document.querySelector("#btnRegister").value = "Register";
document.querySelector(".btn-login").value = "Login";
}
</script>
</head>
<body onload="bodyload()">
<div>
<img width="100" id="pic" name="pic" height="100" border="1">
</div>
<div>
<form name="frmRegister">
<h2>Register</h2>
<input type="email">
<input type="button" id="btnRegister" name="btnRegister">
</form>
</div>
<div>
<form>
<h2>Login</h2>
<input type="text">
<input type="button" id="btnLogin" class="btn-login"
name="btnLogin">
</form>
</div>
</body>
</html>
<input type="email">
<input type="button" id="btnRegister" name="btnRegister">
</form>
</div>
<div>
<form>
<h2>Login</h2>
<input type="text">
<input type="button" id="btnLogin" class="btn-login"
name="btnLogin">
</form>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>DOM Hierarchy</title>
<link rel="stylesheet"
href="../node_modules/bootstrap/dist/css/bootstrap.css">
<script type="text/javascript">
function bodyload(){
x = document.getElementsByClassName("btn-primary");
alert("Total Number of elements using btn-primary: " + x.length);
}
</script>
</head>
<body class="container-fluid" onload="bodyload()">
<div>
<img width="100" id="pic" name="pic" height="100" border="1">
</div>
<div>
<form name="frmRegister">
<h2>Register</h2>
<input type="email">
<input type="button" class="btn btn-primary" id="btnRegister"
name="btnRegister">
</form>
</div>
<div>
<form>
<h2>Login</h2>
<input type="text">
<input type="button" id="btnLogin" class="btn btn-primary"
name="btnLogin">
</form>
</div>
</body>
</html>
Ex: Same Name
<!DOCTYPE html>
<html>
<head>
<title>DOM Hierarchy</title>
<link rel="stylesheet"
href="../node_modules/bootstrap/dist/css/bootstrap.css">
<script type="text/javascript">
function bodyload(){
x = document.getElementsByName("pic");
alert("Total Number of elements using pic name: " + x.length);
}
</script>
</head>
<body class="container-fluid" onload="bodyload()">
<div>
<img width="100" id="pic" name="pic" height="100" border="1">
</div>
<div>
<form name="frmRegister">
<h2>Register</h2>
</form>
</div>
<div>
<form>
<h2>Login</h2>
<input type="text">
<input type="button" id="btnLogin" class="btn btn-primary"
name="btnLogin">
</form>
</div>
</body>
</html>
Summary
1. DOM Hierarchy
2. By Name
3. By ID
4. QuerySelector [CSS Selectors]
5. Multiple
- TagName
- ClassName
- Name