Reputation:
<!doctype html>
<html>
<head>
<style>
div.abc {
width: 500px;
height: 50px;
background-color: red;
display: none;
}
</style>
</head>
<body>
<button onclick="ok()"> Helloo </button>
<div class="abc">
Your Name <input type="text">
Your Name <input type="text">
</div>
<script>
function ok(){
document.getElementsByClassName("abc").style.display="block";
}
</script>
</body>
</html>
// In above line of code initially I have hidden the div with class name abc,but on clicking the button I want to show that div but onclicking the button desired div is not coming.
Upvotes: 0
Views: 90
Reputation:
document.getElementsByClassName("abc").style.display="block";
TO
document.getElementsByClassName("abc")[0].style.display="block";
Upvotes: 1
Reputation: 3207
I think You need to use ID
in your div because of in page multiple class are present but ID use only once.
See here
<!doctype html>
<html>
<head>
<style>
div#abc {
width: 500px;
height: 50px;
background-color: red;
display: none;
}
</style>
</head>
<body>
<button onclick="ok()"> Helloo </button>
<div id="abc">
Your Name <input type="text">
Your Name <input type="text">
</div>
<script>
function ok(){
var x = document.getElementById("abc");
x.style.display="block";
}
</script>
</body>
</html>
Upvotes: 0
Reputation: 1391
If your project has jquery referenced then you can also use its functions show()
, hide()
Example
$(document).ready(function(){
$("button").click(function(){
$(".abc").hide(1000);
});
});
show()
, hide()
functions take an optional parameter to animate show or hide of div (like 1000 in my example)
Upvotes: 0
Reputation: 1413
getelementbyclassname
would always select every elements with provided class name because conceptually Classes are meant to be applied to multiple elements. so to select first element use [0]
like this getelementbyclassname[0]
<!doctype html>
<html>
<head>
<style>
div.abc {
width: 500px;
height: 50px;
background-color: red;
display: none;
}
</style>
</head>
<body>
<button onclick="ok()"> Helloo </button>
<div class="abc">
Your Name <input type="text">
Your Name <input type="text">
</div><br>
<script>
function ok(){
document.getElementsByClassName("abc")[0].style.display="block";
}
</script>
</body>
</html>
Upvotes: 0
Reputation: 1774
document.getElementsByClassName("abc")
returns an array. So you need to access it like one as:
document.getElementsByClassName("abc")[0]
Edited snippet:
<!doctype html>
<html>
<head>
<style>
div.abc {
width: 500px;
height: 50px;
background-color: red;
display: none;
}
</style>
</head>
<body>
<button onclick="ok()"> Helloo </button>
<div class="abc">
Your Name <input type="text">
Your Name <input type="text">
</div>
<script>
function ok(){
document.getElementsByClassName("abc")[0].style.display="block";
}
</script>
</body>
</html>
Upvotes: 1
Reputation: 13
document.getElementsByClassName("abc")
will return a HTMLCollection array, and therefor it does not have style property.
you can use document.querySelector(".abc")
instead.
It will run on almost all browser nowadays
Upvotes: 1
Reputation: 6059
The function getElementsByClassName()
returns an array of elements. To access you element, you have to either iterate over all elements that are returned by the function, or you can access individual items by specifying the desired id:
function ok() {
document.getElementsByClassName('abc')[0].style.display = 'block';
}
Upvotes: 1
Reputation: 22474
document.getElementsByClassName
returns multiple elements not just one. You'll have to use something like this:
document.getElementsByClassName("abc")[0].style.display="block";
This statement will only affect the first element with class abc
because of the [0]
, if you have multiple elements with that class and you want to show them all, you'll have to iterate through the results of document.getElementsByClassName("abc")
and modify each element.
Upvotes: 3