How to compare two element text in JQuery? - html

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>

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>

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>

Two divs, each within two divs - how can I change the stacking order using CSS (for responsive)?

Preface: I cannot change the HTML.
To better explain my question, I have provided an illustration below. Essentially, I have two rows of divs - the first row has content, and the second row would have a button beneath the content.
I want to make the page responsive, so that the div with a button always is below its corresponding div with content, but the extra container divs are proving a challenge. Any ideas?
What I have (above) and what I want (below).
Here's the HTML code:
<div class="choice-section">
<div id="choice_1" class="choice odd" style="margin-top: 242px;">
<div class="content">asdf</div>
</div>
<div id="choice_2" class="choice even" style="margin-top: 322px;">
<div class="content">asdf</div>
</div>
</div>
<div>
<div class="vote-choice odd">
<a class="vote btn" href="javascript:void(null)" choice="1">Vote</a>
</div>
<div class="vote-choice even">
<a class="vote btn" href="javascript:void(null)" choice="2">Vote</a>
</div>
</div>
I would strongly reccomend looking at Bootstrap's grid and column system with which this can easily be achieved. This framework will make responsive design a breeze for you.
You can see an example of something pretty similar to what you are trying to achieve in a Plunker I very quickly put together
CSS:
.blue {
background-color:blue;
height:200px;
}
.red {
background-color:red;
height:200px;
}
.purple {
background-color:purple;
height:200px;
}
.green {
background-color:green;
height:200px;
} here
HTML:
<div class="container">
<div class="row">
<div class="col-sm-3">
<div class="red">
</div>
</div>
<div class="col-sm-3">
<div class="blue">
</div>
</div>
<div class="col-sm-3">
<div class="purple">
</div>
</div>
<div class="col-sm-3">
<div class="green">
</div>
</div>
</div>

how to display a div only when a button is clicked

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)

Target last div in the group

I need to target each last <div> before each <h3>. Is there a programatic way this can be done in css?
For other reasons, I can not / don't want to target <h3> instead.
<div id="main">
<h3>Title</h3>
<div class="post">…</div>
<div class="post"><!-- Target this div--></div>
<h3>Title</h3>
<div class="post">…</div>
<div class="post">…</div>
<div class="post">…</div>
<div class="post"><!-- Target this div--></div>
<h3>Title</h3>
<div class="post">…</div>
<div class="post">…</div>
<div class="post"><!-- Target this div--></div>
</div>
Actually this isn't possible.
If you are able to wrap the blocks into parent tags you can get each last child by CSS selectors #main .post:last-child:
<div id="main">
<div> <!-- wrap with "h3" WORKS -->
<h3>Title</h3>
<div class="post">…</div>
<div class="post"><!-- Target this div--></div>
</div>
<h3>Title</h3>
<div> <!-- wrap without "h3" also WORKS -->
<div class="post">…</div>
<div class="post">…</div>
<div class="post"><!-- Target this div--></div>
</div>
</div>
Note: The :last-child selector is not supported in IE8 and earlier versions.
What do you mean with: "For other reasons, I can not / don't want to target instead."?
jQuery workaround will work exactly like you want it with your original markup:
var $Main = jQuery('#main');
$Main.find('h3').prev().add( $Main.find(':last') ) //.text('<!-- Target this div-->');
At some point in the future, when selectors level 4 are implemented:
#main div:not(:has(+ div))