JQuery each issue - div wrapped twice - javascript

I'm trying to solve an issue that I'm facing with JQuery about each.
I have a list of items (class .list) that I need to target and wrap in a div (class .wrap). It works if the list but when there is a second one the loop wrap twice the list in the new div.
The idea is that every time there is a class called list, the loop wrap that specific class in a div called wrap.
Here is the code
HTML
<div class='parent'>
<div class='child'>
<div class='list'>
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</div>
</div>
<div class='parent'>
<div class='child'>
<div class='list'>
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</div>
</div>
JQuery
jQuery(".parent").each(function () {
jQuery(".list").wrap("<div class='wrap'></div>");
});
This is the result with the error.
The class list is wrapped twice as you can see from the example below.
<div class="parent">
<div class="child">
<div class="wrap"><div class="wrap"><div class="list">
<div>1</div>
<div>2</div>
<div>3</div>
</div></div></div>
</div>
</div>
And this is what I'm trying to achieve.
<div class="parent">
<div class="child">
<div class="wrap">
<div class="list">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</div>
</div>
</div>
Thank you in advance!

You want to select the list in the parent, not select all lists
jQuery(this).find(".list").wrap()

Related

How to use JQuery to Wrap a specific list of div children

I am doing AB testing in google optimize. I need to wrap div from number 5 to 10 but I have not been able to do it:
<div class="container active" data-order="1">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<button>11</button>
<div>12</div>
</div>
I am using this code to do it but it start wrapping from div number one but I want to start with div number 5 until 10
$('.gsw_cf.step.active > div:lt(#)').wrapAll('<div class="wrapped 5to10"/>');
anyone knows what should I add?
Thank you
You can use jQuery's slice();
$('.active > div').slice(5,10).wrapAll('<div class="wrapped 5to10"/>');
$('.active > div').slice(5,10).wrapAll('<div class="wrapped 5to10"/>');
.wrapped{
background:red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container active" data-order="1">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<button>11</button>
<div>12</div>
</div>
I found a solution that works where i use this function:
$('div[data-order="1"]').each(function(){
$(this).children(".input_wrapper.text").wrapAll("<div class='wrapper' />");
});
I found that solution in http://jsfiddle.net/QWHYK/
and Wrapping sets of elements from a list in DIVs using jQuery
There is also, another solution in How to wrap multiple divs

jQuery: find all divs with given class after a specific div, but not sibilings at same level

I have my html like this:
<div id="d1" class="parent">
<div class="hello"></div>
<div class="hello"></div>
</div>
<div id="d2" class="parent">
<div class="ciao"></div>
<div class="hello"></div>
</div>
<div id="d3" class="parent">
<div class="hello"></div>
<div class="hello"></div>
</div>
<div id="d4" class="parent">
<div class="hello"></div>
<div class="ciao"></div>
</div>
And I need to select all the divs that have "hello" class, starting from the one included in d2.
if I use this code, ti doesn't hop the next div to find the other "hello"
$('#2 .hello').nextAll().doSomething();
how do i solve this? thanks!
With my limited knowledge of jQuery:
$('#d2').find('.hello').parent().nextAll().addBack().children('.hello').html('changed')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="d1" class="parent">
<div class="hello">1</div>
<div class="hello">2</div>
</div>
<div id="d2" class="parent">
<div class="ciao">3</div>
<div class="hello">4</div>
</div>
<div id="d3" class="parent">
<div class="hello">5</div>
<div class="hello">6</div>
</div>
<div id="d4" class="parent">
<div class="hello">7</div>
<div class="ciao">8</div>
</div>
Jquery Traversing Api Documentation
I think this link may be useful.
I do not know the answer you are looking for but i might be of help.
Regards.
I Hope this helps you out.

CSS selector to select first and last element under parent, when html structure is not deterministic

I have following HTML structure. Is it possible to select first and last my-item element under parent element purely by CSS?
row in which my-item element is can be wrapped in other divs and (Angular) component elements. Also component elements can be nested within each other. This is HTML structure is generated based on current page.
In JavaScript I would accomplish this by selecting all my-item elements under parent as a flat list and selecting a first and last of them.
UPDATE:
Update HTML template to more resemble my actual page content.
var parentElement = document.querySelector('.parent');
var myItems = parentElement.querySelectorAll('.my-item');
var firstItem = myItems[0];
var lastItem = myItems[myItems.length-1];
firstItem.classList.add('red-background');
lastItem.classList.add('red-background');
.red-background {
background-color: red;
}
<div class="parent">
<person-component>
<div class="template">
<div class="row">
<component-row-template>
<div class="col">
<div class="my-item">Select me!</div>
</div>
<div class="col">
<div class="my-item">Not me</div>
</div>
</component-row-template>
<component-row-template>
<div class="col">
<div class="my-item">Not me</div>
</div>
</component-row-template>
</div>
<div class="row-separator"></div>
<div class="row">
<component-row-template>
<div class="col">
<div class="my-item">Not me</div>
</div>
<div class="col">
<div class="my-item">Not me</div>
</div>
</component-row-template>
</div>
</div>
</person-component>
<div class="row-separator"></div>
<organization-component>
<div class="template">
<div class="row">
<component-row-template>
<div class="col">
<div class="my-item">Not me</div>
</div>
</component-row-template>
</div>
<div class="row-separator"></div>
<div class="row">
<component-row-template>
<div class="col">
<div class="my-item">Not me</div>
</div>
<div class="col">
<div class="my-item">Not me</div>
</div>
</component-row-template>
</div>
<div class="row-separator"></div>
<person-component>
<div class="template">
<div class="row">
<component-row-template>
<div class="col">
<div class="my-item">Not me</div>
</div>
<div class="col">
<div class="my-item">Not me</div>
</div>
</component-row-template>
</div>
<div class="row">
<component-row-template>
<div class="col">
<div class="my-item">Not me</div>
</div>
<div class="col">
<div class="my-item">Not me</div>
</div>
</component-row-template>
</div>
</div>
</person-component>
</div>
</organization-component>
<div class="row-separator"></div>
<div class="row">
<row-template>
<div class="col">
<div class="my-item">Not me</div>
</div>
<div class="col">
<div class="my-item">Not me</div>
</div>
</row-template>
</div>
<div class="row-separator"></div>
<div class="row">
<row-template>
<div class="col">
<div class="my-item">Not me</div>
</div>
<div class="col">
<div class="my-item">Select me!</div>
</div>
</row-template>
</div>
</div>
The below code can work with the assumption that every child div of the parent has the desired ".my-item" and the every ".my-item" is inside a ".col".
It will select the first and the last div from the direct siblings of ".parent" and then will try to find the first and last ".col" and apply the rule to the ".my-item" inside them.
.parent > div:last-of-type .col:last-of-type .my-item,
.parent > div:first-of-type .col:first-of-type .my-item {
background-color: red;
}
If that's not the case and you really don't know anything about the html structure at all, then I am afraid that CSS cannot help you..
Assuming that all .my-items are always wrapped inside a .col that is in turn wrapped inside at least another div (section) and that each section has only one .row (assumption taken from your example), yes it is possible to achieve what you want using a purely CSS approach. The approach is:
Select .parent, as we're only interested only in selecting .my-item inside this element
As all .my-items always have a div container (whether it be a nested container with class section or simply inside a row container), we can specifically determine which wrapper containers are the first and last. These first and last containers, respectively, must be the container in which the first and last my-item are contained.
As all .section container only has one .row, you can easily get the first and last .col of each .section. This means that to get the first .my-item, simply select the first .col and from the first .section wrapper and apply the style to .my-item. Same logic works for your last .my-item.
Here is a minimal working example below:
let firstChild = document.querySelector('.parent > div:first-child .col:first-child .my-item')
let lastChild = document.querySelector('.parent > div:last-child .col:last-child .my-item')
console.log(firstChild, lastChild)
.parent > div:first-child .col:first-child .my-item {
background: #000;
color: #FFF;
}
.parent > div:last-child .col:last-child .my-item {
background: red;
color: #FFF;
}
<div class="parent">
<div class="main-section">
<div class="section-wrapper">
<div class="row">
<div class="col">
<div class="my-item">Select me!</div>
</div>
<div class="col">
<div class="my-item">Not me</div>
</div>
</div>
</div>
</div>
<div class="section-wrapper">
<div class="row">
<div class="col">
<div class="my-item">Not me</div>
</div>
<div class="col">
<div class="my-item">Not me</div>
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="my-item">Not me</div>
</div>
<div class="col">
<div class="my-item">Select me!</div>
</div>
</div>
</div>
As mentioned, this code works if the above assumption is true. One easy example of when this code will break would be when there may be more that two .rows existing in one section-like wrapper.

event to modify sibling element

In a web page, I have a grid of divs. In each div are 3 divs, the 2nd is hidden, I want it so that when the user hovers over the 3rd div, the 1st div becomes hidden and the 2nd is displayed.
I'm using jquery.
<div class="container">
<div class="hello">hello</div>
<div class="who">sailor
</div>
<div onmouseover="whoOn();" onmouseout="whoOff();" class="hover">hover me</div>
</div>
<div class="container">
<div class="hello">hello</div>
<div class="who">dolly
</div>
<div onmouseover="whoOn();" onmouseout="whoOff();" class="hover">hover me</div>
</div>
<div class="container">
<div class="hello">hello</div>
<div class="who">kitty
</div>
<div onmouseover="whoOn();" onmouseout="whoOff();" class="hover">hover me</div>
</div>
Here's a Codepen
Your whoOn and whoOff methods can be combined like this:
<div class="container">
<div class="hello">hello</div>
<div class="who">sailor
</div>
<div onmouseover="whoBoth(this);" onmouseout="whoBoth(this);" class="hover">hover me</div>
</div>
Javascript:
function whoBoth(target) {
$(target).siblings(".hello, .who").toggle();
}

Jquery: hide div that has no visible divs inside

I have the fallowing markup:
<div class="container">
<div style="display:none">1</div>
<div style="display:none">2</div>
<div style="display:none">3</div>
</div>
<div class="container">
<div style="display:none">1</div>
<div>2</div>
<div style="display:none">3</div>
</div>
<div class="container">
<div>1</div>
<div>2</div>
<div style="display:none">3</div>
</div>
<div class="container">
<div style="display:none">1</div>
<div style="display:none">2</div>
<div style="display:none">3</div>
<div style="display:none">4</div>
</div>
How do I hide all the divs with class 'container' that have only hidden divs inside using jQuery selectors? In given case this would be 1st and 4th ones.
$(document).ready(function() {
// how to hide all the divs with class 'container' that have no visible divs inside?
});
See markup at jsfiddle: http://jsfiddle.net/tfY58/
Thanks!
Like this:
$('.container:not(:has(:visible))')

Categories