Target last div in the group - html

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))

Related

CSS select element if parent does not have non-adjacent succeeding sibling element with a given class

I have the following situations:
<div class="content">
<div class="background">
...
</div>
</div>
<div class="indicators"></div>
<div class="overlay">...</div>
<div class="border speaking">
<div class="content">
<div class="background">
...
</div>
</div>
<div class="indicators"></div>
<div class="overlay">...</div>
<div class="border">
I would like to select the background div in the former example, but not the latter. The only difference between the two is the presence of the speaking class on the final div.
I tried .content:not( ~ .border:not(.speaking) ) > .background, but this did not work. I am on Firefox Developer Edition, and as such do not currently have access to :has.
Thanks in advance!

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)")

Add the same element name using BEM methodology

I started to use BEM methodology and i have a question according to this.
Example:
<div class="container">
<div class="container__block-1">
<h1 class="container-title">block1</h1>
</div>
<div class="container__block-2">
<h1 class="container__title container-title">block2</h1>
</div>
<div class="container__block-3">
<h1 class="container__title container-title">block3</h1>
</div>
</div>
How you can see i use: container__title element in block 2 and in block3. I need this to add different margin and padding for h1.
Question: Can i use the same element in container__block-2 and container__block-3 according to BEM methodology?
It is okay to use the same element for another block as long as you want to have the same properties of the above blocks.
However, incase you need a variation, that's when the modifier comes into role.
whenever you need to make a change in only a particular element from a group of elements, you use a modifier there. It is denoted as block__element--modifier.
<div class="container">
<div class="container__block-1">
<h1 class="container-title">block1</h1>
</div>
<div class="container__block-2">
<h1 class="container__title container__title--modifier1 ">block2</h1>
</div>
<div class="container__block-3">
<h1 class="container__title container__title--modifier2">block3</h1>
</div>
</div>
For different variants of same class, u can use --
<div class="container">
<div class="container__block-1">
<h1 class="container-title">block1</h1>
</div>
<div class="container__block-2">
<h1 class="container__title container__title--1">block2</h1>
</div>
<div class="container__block-3">
<h1 class="container__title container__title--2">block3</h1>
</div>
</div>

nth-child is not taking the element that´s needed

I got a document that is built like the following code:
<div id="menubox_container_box_body">
<div class="categories">...</div>
<div class="categories">
<div class="cat_icon">...</div>
<div class="cat_link">
<div class="cat_link_container">...</div>
<div class="cat_icon">...</div>
<div class="cat_sub_link">
<div class="cat_link_container">...</div>
</div>
<div class="cat_icon">...</div>
<div class="cat_sub_link">
<div class="cat_link_container">...</div>
</div>
</div>
Now I am trying to select the a in the first cat_link_container with this command:
#menubox_categories_box_body .categories:nth-child(2) .cat_link .cat_link_container:nth-child(1){
margin-bottom: 20px;
}
What it does is giving ALL the cat_link_container elements (even the one's in the cat_sub_link div) a margin bottom. But I selected to only take the FIRST cat_link_container in cat_link and give it a margin-bottom. Someone has an idea what the problem could be?
To select only the element with class='cat_link_container' under div class='cat_link' and not the elements with same class under its descendants, you need to make use of the > (children) selector like in the below code sample:
#menubox_container_box_body .categories:nth-child(2) .cat_link > .cat_link_container:nth-child(1){
color: red;
font-size: 24px;
}
<div id="menubox_container_box_body">
<div class="categories">...</div>
<div class="categories">
<div class="cat_icon">...</div>
<div class="cat_link">
<div class="cat_link_container">...</div>
<div class="cat_icon">...</div>
<div class="cat_sub_link">
<div class="cat_link_container">...</div>
</div>
<div class="cat_icon">...</div>
<div class="cat_sub_link">
<div class="cat_link_container">...</div>
</div>
</div>
</div>
</div>
Explanation:
Your original selector would be interpreted as follows:
#menubox_container_box_body .categories:nth-child(2) .cat_link .cat_link_container:nth-child(1)
Find the element with id='menubox_container_box_body'.
Under this element, find all descendant elements (children, grand-children etc) which are the 2nd child of its parent and also has class='categories'.
Under all the elements obtained in the previous point, find the element with class='cat_link'
Traverse all elements which are fetched in above point, find all descendant elements which are the 1st child of its own parent and also has class='cat_link_container'.
<div id="menubox_container_box_body"> <!-- 1st point in explanation -->
<div class="categories">...</div>
<div class="categories"> <!-- 2nd point in explanation -->
<div class="cat_icon">...</div>
<div class="cat_link"> <!-- 3rd point in explanation -->
<div class="cat_link_container">...</div> <!-- descendant, first-child of its parent and so selected -->
<div class="cat_icon">...</div>
<div class="cat_sub_link">
<div class="cat_link_container">...</div> <!-- descendant of the element referenced by 3rd point, first-child of its own parent and so selected -->
</div>
<div class="cat_icon">...</div>
<div class="cat_sub_link">
<div class="cat_link_container">...</div> <!-- descendant of the element referenced by 3rd point, first-child of its own parent and so selected -->
</div>
</div>
</div>
</div>
The revised selector that I have provided in answer will be interpreted as follows:
#menubox_container_box_body .categories:nth-child(2) .cat_link > .cat_link_container:nth-child(1)
Find the element with id='menubox_container_box_body'.
Under this element, find all descendant elements (children, grand-children etc) which are the 2nd child of its parent and also has class='categories'.
Under all the elements obtained in the previous point, find the element with class='cat_link'
Traverse all elements which are fetched in above point, find the element which is the 1st child of its the element referred to by point 3 and also has class='cat_link_container'. Note how it checks only for direct child and not descendants.
<div id="menubox_container_box_body"> <!-- 1st point in explanation -->
<div class="categories">...</div>
<div class="categories"> <!-- 2nd point in explanation -->
<div class="cat_icon">...</div>
<div class="cat_link"> <!-- 3rd point in explanation -->
<div class="cat_link_container">...</div> <!-- direct child, first-child of its parent and so selected -->
<div class="cat_icon">...</div>
<div class="cat_sub_link">
<div class="cat_link_container">...</div> <!-- not direct link and hence not selected -->
</div>
<div class="cat_icon">...</div>
<div class="cat_sub_link">
<div class="cat_link_container">...</div> <!-- not direct link and hence not selected -->
</div>
</div>
</div>
</div>