how to display a div only when a button is clicked - html

Hi i want to only display the div id placeholder1, placeholder2, etc once i click different buttons. but i am not sure how to start on it. do help me, thank you :-) below are my codes for the different div ids.
<div id="content">
<div class="demo-container">
<div id="placeholder1" class="demo-placeholder"></div>
</div>
<div class="demo-container" style="height:150px;">
<div id="overview1" class="demo-placeholder"></div>
</div>
<p>Carbon Dioxide Reading</p>
<div class="demo-container">
<div id="placeholder2" class="demo-placeholder"></div>
</div>
<div class="demo-container" style="height:150px;">
<div id="overview2" class="demo-placeholder"></div>
</div>
<p>Gas Reading</p>
<div class="demo-container">
<div id="placeholder3" class="demo-placeholder"></div>
</div>
<div class="demo-container" style="height:150px;">
<div id="overview3" class="demo-placeholder"></div>
</div>
<p>Humidity Reading</p>
<div class="demo-container">
<div id="placeholder4" class="demo-placeholder"></div>
</div>
<div class="demo-container" style="height:150px;">
<div id="overview4" class="demo-placeholder"></div>
</div>
<p>Temperature Reading</p>
<div class="demo-container">
<div id="placeholder5" class="demo-placeholder"></div>
</div>
<div class="demo-container" style="height:150px;">
<div id="overview5" class="demo-placeholder"></div>
</div>
</div>

To show a div when a button is clicked, you can add this to your CSS:
.demo-placeholder {
display: none;
}
Then, for example, this is your button:
<input type="button" id="but1" class="buttons" onclick="showDiv1()">Show Button 1
Then add this to your JavaScript:
function showDiv1(){
document.getElementById("placeholder1").style.display = 'block';
}
NOTE
When you want to do this for multiple buttons, make sure to NOT use the same funciton, but change the numbers to the div you want to show. If you need more help with this, just let me know and I'll update my answer.
I hope this helped you!

This is very basic JQuery code:
$("#ph1").click(function(){
$("[id^='placeholder']").hide();
$("#placeholder1").show();
});
$("#ph2").click(function(){
$("[id^='placeholder']").hide();
$("#placeholder2").show();
});
$("#ph3").click(function(){
$("[id^='placeholder']").hide();
$("#placeholder3").show();
});
$("#ph4").click(function(){
$("[id^='placeholder']").hide();
$("#placeholder4").show();
});
$("#ph5").click(function(){
$("[id^='placeholder']").hide();
$("#placeholder5").show();
});
Here is the JSFiddle demo
I added buttons with ids ph1, ph2 etc...
and clicking on the button performs the follow:
$("[id^='placeholder']").hide(); hides all elements that have placeholder text in their ID
$("#placeholder1").show(); shows the element with the defined ID (in this case placeholder1)

Related

How to Remove HTML element by class name

I'm changing a database using phpmyadmin with several html pages inside it and I would like to remove, from all these pages, all the <div> and other tags that contain a certain class or id.
Example:
Case 1
<div class="undesirable">
<div class="container">
<div class="row">
<div class="col1"></div>
</div>
</div>
</div>
Case 2
<div class="undesirable">
<div class="container">
<div class="row">
<div class="col1"></div>
<div class="col2"></div>
</div>
</div>
</div>
i would like to remove all <div> that contain the class="undesirable". In some cases, there is still the possibility of appearing as class="pre_undesirable", or something similar.
Initially I thought of using regex, but as there are variations in htmls, code breaks are occurring, as there is no way to know when the <\div> will end.
Possibly the answer would be HTML parser, but I can't understand how to use it. Any indication of where to start?
Since you are dealing with html, you probably should use an html parser and search for the removal target using xpath. To demonstrate, I'll change your html a bit:
$original=
'<html><body>
<div class="undesirable">
<div class="container">
<div class="row">
<div class="col1"></div>
</div>
</div>
</div>
<div class="keepme">
<div class="container">
<div class="row">
<div class="col1"></div>
<div class="col2"></div>
</div>
</div>
</div>
<div class="pre_undesirable">
<div class="container">
<div class="row">
<div class="col1"></div>
<div class="col2"></div>
</div>
</div>
</div>
<div class="keepme">
<div class="container">
<div class="row">
<div class="col1"></div>
<div class="col2"></div>
</div>
</div>
</div>
</body>
</html>
';
$HTMLDoc = new DOMDocument();
$HTMLDoc->loadHTML($original);
$xpath = new DOMXPath($HTMLDoc);
$targets = $xpath->query('//div[contains(#class,"undesirable")]');
foreach($targets as $target){
$target->parentNode->removeChild($target);
}
echo $HTMLDoc->saveHTML();
The output should include only the two "keep me" <div>s.
We can make use D3JS to remove or append any the HTML elements by class name or id.
We can make use of Select() and Selectall() for the selection of the particular elements in the HTML. Incase if we want to append any div tag use append('div') to insert the div for the data.
<script>
function remove()
{
d3.select(.undesirable)
.selectAll("li")
.exit()
.remove()
}
</script>

How to access a child element using jquery?

Suppose I have a block of code that looks like this.
<div class="lv1">
<div class="lv2">
<div class="img">
</div>
<div id="text-on-image"> // <-- I want to grab this div element.
</div>
</div>
<div class="lv2">
</div>
</div>
Now I have $(this).eq(0) which refers to the root div element.
<div class="lv1"> //<-- $(this).eq(0) is here.
<div class="lv2">
<div class="img">
</div>
<div id="text-on-image">
</div>
</div>
<div class="lv2">
</div>
</div>
$(this).eq(0).children().eq(0) now refers to first div of lv2 class div.
<div class="lv1">
<div class="lv2"> // <-- $(this).eq(0).children().eq(0) is here
<div class="img">
</div>
<div id="text-on-image">
</div>
</div>
<div class="lv2">
</div>
</div>
$(this).eq(0).children().eq(0).children().eq(1) now refers to the correct div I want.
<div class="lv1">
<div class="lv2">
<div class="img">
</div>
<div id="text-on-image"> // <-- $(this).eq(0).children().eq(0).children().eq(1) is here
</div>
</div>
<div class="lv2">
</div>
</div>
Notice how the selecting child node became very messy for my code.
"$(this).eq(0).children().eq(0).children().eq(1)"
Is there a better way to go about doing the same work?
Use:
$("#parent").find(".children") for all children (deep traverse)
or:
$("#parent").children(".children") for immediate children
Since you use an ID, and an ID must be unique - simply use $("#text-on-image")
Otherwise, use the .find() Method.
If you used id, you can select it right away.
$("#text-on-image")
id must be unique, so if you use class, you can use ".find()"
$(".lv1").find(".text-on-image")
If no id or class is specified, selectors can be used.
$(".lv1 > .lv2 > div:nth-child(2)")

How to compare two element text in JQuery?

In my project, I am using Jquery. so I have two sections each section have multiple elements with different content, for example, left-content, and right-content. so left-content and right-content have the same class with multiple elements and right-content is dynamic and left-content is fixed one.
so if right-content text and left-content text are equal, then left-content that particular element we have add one new class.
I tried some of the ways but it not working as per my expectations. please help me fix this. if anything is wrong please correct me.
jQuery(document).ready(function(){
let headingElement = jQuery('#articleTitle').text();
jQuery('.ellipsis a').filter(function(){
return jQuery(this).text() === headingElement;
}).addClass('active');
});
.active{
color:yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div class="left-content">
<div class="ellipsis">
Terminology one
</div>
<div class="ellipsis">
Terminology two
</div>
<div class="ellipsis">
Terminology three
</div>
</div>
<div class="right-content">
<h2 class="heading" id="articleTitle">Terminology two</h2>
</div>
I hope I understood your task correctly.
Was such a result necessary?
jQuery(document).ready(function(){
let headingElement = jQuery('#articleTitle').text();
jQuery('.ellipsis a').each(function(){
if (!$(this).text().indexOf(headingElement)) {
$(this).addClass('active');
}
});
});
.active{
color:yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div class="left-content">
<div class="ellipsis">
Terminology one
</div>
<div class="ellipsis">
Terminology two
</div>
<div class="ellipsis">
Terminology three
</div>
</div>
<div class="right-content">
<h2 class="heading" id="articleTitle">Terminology two</h2>
</div>
You can use :contains this will check if the element has required text or not then addClass to that element.
Demo Code :
jQuery(document).ready(function() {
let headingElement = jQuery('#articleTitle').text();
jQuery('.ellipsis a:contains(' + headingElement + ')').addClass('active');
});
.active {
color: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div class="left-content">
<div class="ellipsis">
Terminology one
</div>
<div class="ellipsis">
Terminology two
</div>
<div class="ellipsis">
Terminology three
</div>
</div>
<div class="right-content">
<h2 class="heading" id="articleTitle">Terminology two</h2>
</div>

Web-Inspector: How can I find a DOM-Element in the hierarchy that sets a background-color?

is there any function or extension that allows to find the element that's styles for example the background-color in a DOM-Hierarchy using the Web-Inspector of any browser?
Let's think about a DOM-Tree that might look like this:
.div3 {
background-color: green;
}
<div class="div0">
<div class="div1">
<div class="div2">
<div class="div3">
<div class="div4">
<div class="div5">
<div class="div6">
<div class="div7">
<div class="div8">
Some Text
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
The element select tool will select .div8. Now you have to go up the tree and manual search the first div that's setting the green color. Is there any tree-search available? e.g. Find first div that applies style "background-color" up the tree? The searchbox only finds styles that are applied the current DOM-Element.
Select all element parents, loop them and check background-color. In web inspector write code in console tab
$(document).ready(function() {
var parents = $('.div8').parents();
$.each(parents, function() {
if ($(this).css('background-color') != 'transparent' && $(this).css('background-color') != 'rgba(0, 0, 0, 0)') {
console.log($(this).attr('class') + ':' + $(this).css('background-color'));
return false;
}
});
});
.div3 {
background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div0">
<div class="div1">
<div class="div2">
<div class="div3">
<div class="div4">
<div class="div5">
<div class="div6">
<div class="div7">
<div class="div8">
Some Text
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

Need to set width depends upon its content

I need to set width for div depends upon its content..
Below is my HTML flow.
Flow 1:
<div id="header-cart" class="block block-cart skip-content skip-active">
<div class="minicart-wrapper">
<p class="block-subtitle"></p>
<p class="empty">You have no items in your shopping cart.</p>
</div>
</div>
Flow 2:
<div id="header-cart" class="block block-cart skip-content skip-active">
<div class="minicart-wrapper">
<p class="block-subtitle"></p>
<div>
<div id="minicart-widgets"> </div>
<div class="block-content"></div>
<div class="minicart-actions"></div>
</div>
</div>
</div>
I have two HTML flows explained above..
What i need is to set
width as 220px and 300px
for
<div id="header-cart">
.
When there is an occurance of <p class="empty"> in <div id="header-cart">
<div id="header-cart"> should be in
width 220px
.
and if not it should be in 300px.
I dont know how to set flow for this cascade.
Can any one help me?? Thanks in advance..
You can use JavaScript for this.
if(document.querySelectorAll("div#header-cart p.empty").length==0){
document.getElementById('header-cart').style.width="300px";
}else{
document.getElementById('header-cart').style.width="220px";
}
By looking at your above code i feel there is no need to go for any script for this. You can acheive the same with existing css classes.
Flow 1:
Default class is applying. so, give the base width for this class.
.skip-content { width: 220px; }
Flow 2:
Active class is applying. So, give the active class width for this class.
.skip-active { width: 300px; } // This will be applied in active time.
i think this can be done by stylign inline
<div id="header-cart" class="block block-cart skip-content" style="width:220px;">
<div class="minicart-wrapper">
<p class="block-subtitle"></p>
<p class="empty">You have no items in your shopping cart.</p>
</div>
</div>
<div id="header-cart" class="block block-cart skip-content skip-active" style="width:300px;">
<div class="minicart-wrapper">
<p class="block-subtitle"></p>
<div>
<script type="text/javascript">
<div id="minicart-widgets"> </div>
<div class="block-content"></div>
<div class="minicart-actions"></div>
</div>
</div>
or make a another div for flow 1 and 2 like this
<div id="flow1">
<div id="header-cart" class="block block-cart skip-content">
<div class="minicart-wrapper">
<p class="block-subtitle"></p>
<p class="empty">You have no items in your shopping cart.</p>
</div>
</div>
</div>
<div id="flow2">
<div id="header-cart" class="block block-cart skip-content skip-active">
<div class="minicart-wrapper">
<p class="block-subtitle"></p>
<div>
<div id="minicart-widgets"> </div>
<div class="block-content"></div>
<div class="minicart-actions"></div>
</div>
</div>
</div>
</div>
CSS:
#flow1 {width:220px;}
#flow2 {width:300px;}
sample - http://jsfiddle.net/wd54paav/