I want to hide all the elements on the page, but only show the contents of div.k1. There are many more elements on the page. How do i do it in pure CSS?
<div>1-this will hidden</div>
<div class="k1">
2-this div will displayed
<p>3-this will displayed</p>
<p>4-this div will displayed</p>
<div>
5-this will displayed
<p>6-this will displayed</p>
<div>
7-this will displayed
<p>8-this will displayed</p>
</div>
</div>
</div>
<div>9-this will hidden</div>
<div>10-this will hidden</div>
<div>1-this will hidden</div>
<div class="k1">
2-this div will displayed
<p>3-this will displayed</p>
<p>4-this div will displayed</p>
<div>
5-this will displayed
<p>6-this will displayed</p>
<div>
7-this will displayed
<p>8-this will displayed</p>
</div>
</div>
</div>
<div>9-this will hidden</div>
<div>10-this will hidden</div> 11,12,13..
If all the elements you want to hide are div's that are directly within the body you can do something like the following.
var items = document.querySelectorAll("body>div:not(.k1)");
for (var i = 0; i < items.length; i++) {
items[i].style.display = 'none';
}
Basically what this does is select all the div elements that are directly within the body that do not have the class k1. Then it does a for loop on those items and sets each item to not display.
For a CSS solution you could just do something similar if the conditions are the same as I mentioned above.
body>div:not(.k1) {
display: none;
}
If you are interested in learning more about CSS selectors I'd encourage you to take a look at the W3 schools page on it.
Here's a crude way of doing this for divs nested up to 2 layers deep (as in your example). As you can see here, the problem is hiding all divs based on the tagName ('div'), unless they either have the className "k1" or are children of a div with that className. So we actually have to check at least 3 conditions before applying the hidden property. You can, of course, go deeper, if needed, by adding parentNode.parentNode.parentNode... and so on. But I would almost certainly approach this instead by assigning a class to the elements I want hidden, with an ID on the one I want to reveal. This is just a way of doing the job without changing any of your html.
const allDivs = document.getElementsByTagName('div');
for (let i = 0; i < allDivs.length; i++) {
if(allDivs[i].className !== "k1" && allDivs[i].parentNode.className !== "k1"){
if (allDivs[i].parentNode.parentNode.className !== "k1"){
allDivs[i].hidden = true;
}
}
};
<div class="k1">
2-this div will displayed
<p>3-this will displayed</p>
<p>4-this div will displayed</p>
<div>
5-this will displayed
<p>6-this will displayed</p>
<div>
7-this will displayed
<p>8-this will displayed</p>
</div>
</div>
</div>
<div>9-this will hidden</div>
<div>10-this will hidden</div>
Cheers!
Related
In case of a nested div, without any ids whatsoever, is there any way to get the outermost <div>?
<div>
Outermost div
<span>
<img />
<div>
Second level div
<div>
Third level div
</div>
</div>
</span>
</div>
I wouldn't be able to add ids or classes or any of those.
Long story:
I will be creating a Vue component that will be used multiple times on a single page (imagine a quadrant with all 4 sections are actually the same component):
This component will need to append text to a div. And this div must be created dynamically. Ideally, I shouldn't add an id to the outermost div since there will be conflict if I do something like this:
<div id="logs">
</div>
const logsDiv = document.getElementById('logs')
function appendLog (log) {
logsDiv.innerHTML += log
}
Since there will be multiple instances of the same component, it will all write to the div with the id "logs". Another option is if I accept a prop and I can append that to the id (like 'logs' + id = 'logs1'). But this feels a bit hacky. I prefer just getting the outermost div if that is possible.
So my thought is just get the outermost div and create a child div there. Something like:
<div>
<!-- some other elements -->
</div>
const outermostDiv = codeToGetOuterMostDiv()
const logsDiv = document.createElement('div')
outermostDiv.appendChild(logsDiv)
function appendLog (log) {
logsDiv.innerHTML += log
}
.
I have a div tag which contains other elements like form, button etc. div is stretching to entire width of the screen. So I am told that display="inline-block" would fix the issue. But I am also using display attribute to display the div based on some condition
<div display={some condition}...>
So how to handle this situation?
I hope if this helps you
HTML
<div id="selected">
Ahmed
</div>
Javascript
if(your condation){
const div = document.getElementById('selected').style.display ="inline-block"
}else if(your condation){
//code
}
.
.
.
else{
//code
}
I have 8 hidden sections with display:none; on my site that are triggered with jQuery to show when a select id is selected. When i try to start building the structure inside the hidden element #Restaurant (like this)
<div id="common">
<div id="Restaurant" class="common_reveal">
<div class="common_title">test</div>
</div>
</div>
It doesn't work. However, if i just insert plain text or <span>, it does...
Is there a reason for why it wont display additional <div>'s within the hidden element?
JS
$('#business_type').change(function(){
$("#common div").each(function() {
$(this).attr("style", "display: none;");
})
$("#" + $(this).val()).attr("style", "display: block;");
})
CSS
#common {
}
.common_reveal {display: none;}
You are picking all divs inside your #common element, try this:
$("#common >div").each(...)
I have several div tags containing simple html/css content and I need a simple way for a viewer to click a link 'Slide 1' and the first div tag for that slide become visible, then they click slide 2 and the second div tag is visible, and so on.
i tried using timelines in Dreamweaver but that seems like overkill and far too complex. So is pulling in the whole YUI library. Just a basic, brute force, make visible/invisible when a link is clicked is all we need.
so the setup is
<LINK_SLIDE1> <LINK_SLIDE2> <LINK_SLIDE3>
<div class="slide" id="slide1">
<p>Welcome etc etc etc</p>
</div
<div class="slide" id="slide2">
<p>Overview etc etc etc</p>
</div
<div class="slide" id="slide3">
<p>Summary etc etc</p>
</div
And I'm looking for some tips on how to implement the LINKs to toggle the visiblity of each div tag on/off. One problem I'm worried about is if a user doesnt have javascript enabled, can we handle that case too.
The basic flow when the user clicks the link should be:
Hide all divs with the class 'slide'.
Show the div with the id "slideX" (where X is determined by the link clicked).
You can do this by adding event listeners to each link, then figuring out by which link was clicked, which slide to show. For problems like this using the rel attribute in the link is a quick way to store this info.
Here's an example using no framework whatsoever, and I haven't tested it. Just for an example.
var slides = document.getElementsByClassName('slide');
function showSlide(e) {
var toShow = e.target.getAttribute('rel');
for (var i = 0, len = slides.length; i < len; i++) {
slides[i].style.display = 'none';
}
document.getElementById(toShow).style.display = 'block';
e.preventDefault();
return false;
}
var links = document.getElementsByClassName('slide-link');
for (var i = 0, len = links.length; i < len; i++) {
links[i].addEventListener('click', showSlide, false);
}
and example HTML to go with it:
Welcome
Overview
<div class="slide" id="slide1">
<p>Welcome etc etc etc</p>
</div>
<div class="slide" id="slide2">
<p>Overview etc etc etc</p>
</div>
Hopefully that can get you on the right track.
There's an awesome, pre-built, open-source implementation called S5. Take a look, you'll probably really like it.
I'm struggling with this bit of code, and I'm not sure if it's even possible. I have a list of divs within a single parent element, and I need to collapse and expand certain sets. Here's an example:
<div id="parent">
<div class="alway_show">ALWAYS SHOW ME</div>
<div class="collapse_me">COLLAPSIBLE</div>
<div class="collapse_me">COLLAPSIBLE</div>
<div class="collapse_me">COLLAPSIBLE</div>
<div class="alway_show">ALWAYS SHOW ME</div>
<div class="collapse_me">COLLAPSIBLE</div>
<div class="collapse_me">COLLAPSIBLE</div>
<div class="collapse_me">COLLAPSIBLE</div>
</div>
So, in the initial state, .collapse_me will be display:none. There will be a link in always show to expand ONLY the collapsed divs below that particular .always_show div. I know this would be ten million times easier if the collapsible divs were in their own div, but I don't have control over the code. I have to make it work as is using jquery. Possible?
$('div.always_show').nextAll().each(function() {
if($(this).is('.collapse_me')) {
$(this).toggle();
}
else {
//this will halt the each "loop", stopping before the next .always_show
return false;
}
});
Of course you should not use my initial selector 'div.always_show', but rather supply it the actual element, which will be the parent of the clicked link. For example:
$('#expand_anchor').parent().parent().nextAll()...
var fncdiv = function(){
var el = this;
do{
el = $(el).next();
if ($(el).hasClass("collapse_me") )
$(el).toggle();
else
break;
}while (true)
};
$('#parent div.alway_show').bind("click", fncdiv);
You shouldn't need to use jQuery. It only requires some clever CSS:
#parent
{
/* normal parent css here */
}
#parent div
{
display: block;
}
#parent.collapsed
{
display: block;
}
#parent.collapsed div
{
display: none;
}
Selectors are applied in order of specificity. Since '#parent.collapsed div' is more specific than '#parent div', it should override. Now, all you need to do, is set the parent div's class, and you're done. You can use javascript to add/remove the 'collapsed' class to the DIV at runtime to toggle expansion without any additional effort:
// Mootools:
$('parent').addEvent('click', function()
{
$('parent').toggleClass('collapsed')
});