Select and wrap two divs with jquery - javascript

I am trying to wrap divs in a 'row' wrapper, so that there are two divs in each.
My code looks like this:
<div id="content" class="center cutepicscontainer cf">
<div class="product cf">product 1</div>
<div class="product cf">product 2</div>
<div class="product cf">product 3</div>
<div class="product cf">product 4</div>
</div>
And I would like my products to be sorted like this:
<div id="content" class="center cutepicscontainer cf">
<div class="row">
<div class="product cf">product 1</div>
<div class="product cf">product 2</div>
</div>
<div class="row">
<div class="product cf">product 3</div>
<div class="product cf">product 4</div>
</div>
</div>
I searched and found a piece of jquery code, but this doesn't work exactly like I want.
$('.cutepicscontainer .product').each(function(){
$(this).next().andSelf().wrapAll('<div class="row"/>');
});
This is the outcome : http://i.imgur.com/Jx0r3NO.png
Thanks in advance!
Victor
EDIT added .product:even, now it works!

$('.cutepicscontainer .product:even').each(function(){
$(this).next().andSelf().wrapAll('<div class="row cf"/>');
});
This works!

Related

How to remove gap between column ordering in Bootstrap

I have created 3 cards with 2 widgets in the sidebar. To make it responsive I have used bootstrap 4 column ordering.
Now the issue I'm facing is if I have 2 columns in one row and the right column is larger than the left column it shows space in-between card 1 and card 2 because card 1 is smaller than widget 1 or 2.
Can anyone help me how can I remove this gap?
Codeply link for better responsive viewing:
https://www.codeply.com/go/8cNp9dUyE5
My code preview is below:
.left-panel {
background: gray;
}
.box1,
.box2,
.box3 {
background: white;
height: 50px;
border: 1px solid red;
margin-bottom: 20px;
}
.right-sidebar, .bg-pink {
background: pink;
}
<div class="container">
<div class="row">
<div class="col-12 bg-pink">
<div class="row d-md-block">
<div class="left-panel col-md-8 order-0 float-left">
<div class="box1 mt-2"><p>Box 1</p></div>
</div>
<div class="right-sidebar col-md-4 order-3 float-left">Widget 1</div>
<div class="right-sidebar col-md-4 order-1 float-left">
<p>Widget 2</p><p>Continue Widget 2</p><p>Widget 2 About to End</p><p>Widget 2 Ends</p>
</div>
<div class="left-panel col-md-8 order-3 float-left">
<div class="box2 mt-2 ">Box 2</div>
</div>
<div class="left-panel col-md-8 order-5 float-left">
<div class="box3 mt-2">Box 3</div>
</div>
</div>
</div>
</div>
</div>
Real-life example: https://prnt.sc/ouscan
enter image description here
It's not totally clear to me what you are trying to do here, but simplifying your classes will remove the gap:
<div class="container">
<div class="row">
<div class="left-panel col-md-8">
<div class="box1 mt-2"><p>Box 1</p></div>
<div class="box2 mt-2">Box 2</div>
<div class="box3 mt-2">Box 3</div>
</div>
<div class="right-sidebar col-md-4">
<p>Widget 1</p><p>Widget 2</p><p>Continue Widget 2</p><p>Widget 2 About to End</p><p>Widget 2 Ends</p>
</div>
</div>
</div>
You can re-introduce the ordering classes if needed, but this will at least remove the gap. Depending on what the contents of the boxes and the widgets are, you may want to include rows within the two columns also.
Edit
If you were to create a re-usable component that represents your right side bar this would be a bit cleaner, but here is an option:
<div class="container">
<div class="row">
<div class="left-panel col-md-8">
<div class="box1 mt-2"><p>Box 1</p></div>
<div class="right-sidebar d-md-none">
<p>Widget 1</p><p>Widget 2</p><p>Continue Widget 2</p><p>Widget 2 About to End</p><p>Widget 2 Ends</p>
</div>
<div class="box2 mt-2">Box 2</div>
<div class="box3 mt-2">Box 3</div>
</div>
<div class="right-sidebar d-sm-none d-md-block col-md-4">
<p>Widget 1</p><p>Widget 2</p><p>Continue Widget 2</p><p>Widget 2 About to End</p><p>Widget 2 Ends</p>
</div>
</div>
</div>
To achieve this you might want to simplify your html and wrap your elements sort of like this:
<div class="container">
<div class="row">
<div class="col-md-8">
BOXES GO HERE
</div>
<div class="col-md-4">
SIDEBAR GOES HERE
</div>
</div>
</div>
The floats are battling what the bootstrap grid does automatically.
NEW This might be more what you are looking for using row-eq-height:
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row row-eq-height" style="background-color: blue;">
<div class="col-md-8">
<div class="box1 mt-2">
<p>Box 1</p>
</div>
</div>
<div class="col-md-4">
<p>Widget 1</p>
</div>
</div>
<div class="row row-eq-height" style="background-color: red;">
<div class="col-md-8">
<div class="box1 mt-2">
<p>Box 2</p>
</div>
</div>
<div class="col-md-4">
<p>Widget 2</p>
<p>Continue Widget 2</p>
<p>Widget 2 About to End</p>
<p>Widget 2 Ends</p>
</div>
</div>
<div class="row row-eq-height" style="background-color: yellow;">
<div class="col-md-8">
<div class="box1 mt-2">
<p>Box 3</p>
</div>
</div>
<div class="col-md-4">
<p>Widget 3</p>
</div>
</div>
</div>

jQuery convert element ids into comma seperated string

I have a simple html and jquery script like below, I am trying to get a comma seperated list of data-item-id so would look like...
1,2,3,4,5
var items = $('.container').children();
console.log(items);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="item" data-item-id="1">Item 1</div>
<div class="item" data-item-id="2">Item 2</div>
<div class="item" data-item-id="3">Item 3</div>
<div class="item" data-item-id="4">Item 4</div>
<div class="item" data-item-id="5">Item 5</div>
</div>
I have grabbed the items but how can I convert this into the comma seperated string?
Map each item to its item-id, then join by commas:
var items = $('.container')
.children()
.map(function() { return $(this).data('item-id') })
.get()
.join(',');
console.log(items);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="item" data-item-id="1">Item 1</div>
<div class="item" data-item-id="2">Item 2</div>
<div class="item" data-item-id="3">Item 3</div>
<div class="item" data-item-id="4">Item 4</div>
<div class="item" data-item-id="5">Item 5</div>
</div>
But there's no need for a big library like jQuery to accomplish something this trivial, if you want:
const items = Array.from(
document.querySelectorAll('.container > .item'),
div => div.dataset.itemId
)
.join(',');
console.log(items);
<div class="container">
<div class="item" data-item-id="1">Item 1</div>
<div class="item" data-item-id="2">Item 2</div>
<div class="item" data-item-id="3">Item 3</div>
<div class="item" data-item-id="4">Item 4</div>
<div class="item" data-item-id="5">Item 5</div>
</div>
Because who wouldn't want to add destructuring as well ..... (Kudos to CertainPerformance)
const items = Array.from(
document.querySelectorAll('.container > .item'),
({ dataset: { itemId: i } }) => i
)
.join(',');
console.log(items);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="item" data-item-id="1">Item 1</div>
<div class="item" data-item-id="2">Item 2</div>
<div class="item" data-item-id="3">Item 3</div>
<div class="item" data-item-id="4">Item 4</div>
<div class="item" data-item-id="5">Item 5</div>
</div>
Another alternative with JQuery could be using .each() to traverse the items while grabbing the data-item-id with .data() and adding it to a string.
$(document).ready(function()
{
var itemList = "";
$('.container .item').each(function()
{
itemList += $(this).data('item-id') + ",";
});
console.log("Ids:", itemList.slice(0,-1));
});
.as-console {background-color:black !important; color:lime;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="item" data-item-id="1">Item 1</div>
<div class="item" data-item-id="2">Item 2</div>
<div class="item" data-item-id="3">Item 3</div>
<div class="item" data-item-id="4">Item 4</div>
<div class="item" data-item-id="5">Item 5</div>
</div>
You can also apply some features of ES5 and ES6 for a shorter solution. And without jQuery.
let itemsIds = [...document.getElementsByClassName("item")]
.map(elem => elem.getAttribute("data-item-id"))
.join(',');
console.log(itemsIds);
<div class="container">
<div class="item" data-item-id="1">Item 1</div>
<div class="item" data-item-id="2">Item 2</div>
<div class="item" data-item-id="3">Item 3</div>
<div class="item" data-item-id="4">Item 4</div>
<div class="item" data-item-id="5">Item 5</div>
</div>

How toggle multiple element with one .toggle()

I know this question is kind of weird but i am not able to find out this
For Example : there is some div with ids
HTML
<div id="test1">test 1</div>
<div id="test2">test 2</div>
<div id="test3">test 3</div>
<div id="test4">test 4</div>
<div id="test5">test 5</div>
<div id="test6">test 6</div>
and i want to toggle some specific div
like
test1, test4 and test6
so i have to do like this
$('#test1').toggle();
$('#test4').toggle();
$('#test6').toggle();
but i want to know is there any way to do that like
$('#test1', '#test4', '#test6').toggle();
I know it can be done if i just give same class on the div which i want to toggle.
but i want to know this for that reason i asked
Sure you can, simply comma separate your ID selector:
$('#test1, #test4, #test6').toggle();
$("#toggle").on("click", function(){
$('#test1, #test4, #test6').toggle();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="toggle">TOGGLE 1, 4, 6</button>
<div id="test1">test 1</div>
<div id="test2">test 2</div>
<div id="test3">test 3</div>
<div id="test4">test 4</div>
<div id="test5">test 5</div>
<div id="test6">test 6</div>
A much reusable code would allow you to toggle any desired elements without copy/pasting your JavaScript functions.
Here's an example where the desired selector to toggle is stored within the data-toggle attribute of the action element:
$("[data-toggle]").on("click", function(){
$(this.dataset.toggle).toggle();
});
.hidden{display:none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button data-toggle="#test1,#test6">Toggle 1, 6</button>
<button data-toggle="#test3,#test4">Toggle 3, 4</button>
<div id="test1">test 1</div>
<div id="test2">test 2</div>
<div id="test3">test 3</div>
<div id="test4">test 4</div>
<div id="test5">test 5</div>
<div id="test6">test 6</div>
<button data-toggle=".info">Toggle .info</button>
This is some
<span class="info">OLD</span>
<span class="info hidden">NEW!!!</span>
info

Hide Parent Div if nested div is empty

I'm trying to hide the outer div when a nested div is empty (including white-space node). I've found a solution that works if there is NO whitespace:
Hide parent DIV if <li> is Empty
I need it to work when there IS white space present, ie:
<div class="gen-customer">
<div class="wrapper">
<div class="heading">Hidden if working 1</div>
<div class="content">
<div class="product"> </div>
</div>
</div>
</div>
Example fiddle
Working fiddle
You could use the both :empty and :contain() selector :
$("div.product:contains(' '), div.product:empty").closest('div.wrapper').hide();
Hope this helps.
$("div.product:contains(' '), div.product:empty").closest('div.wrapper').hide()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="gen-customer">
<div class="wrapper">
<div class="heading">Hidden if working 1</div>
<div class="content">
<div class="product"> </div>
</div>
</div>
</div>
<div class="gen-customer">
<div class="wrapper">
<div class="heading">Visible if working 2</div>
<div class="content">
<div class="product">text</div>
</div>
</div>
</div>
<div class="gen-customer">
<div class="wrapper">
<div class="heading">Hidden if working 3</div>
<div class="content">
<div class="product"></div>
</div>
</div>
</div>
You can iterate over each div.product and trim the text to remove whitespace. If there's anything left, show it, otherwise, hide its wrapper.
$("div.product").each(function() {
if ($(this).text().trim() == '') {
$(this).closest('div.wrapper').hide()
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="gen-customer">
<div class="wrapper">
<div class="heading">Hidden if working 1</div>
<div class="content">
<div class="product"></div>
</div>
</div>
</div>
<div class="gen-customer">
<div class="wrapper">
<div class="heading">Visible if working 2</div>
<div class="content">
<div class="product">text</div>
</div>
</div>
</div>
<div class="gen-customer">
<div class="wrapper">
<div class="heading">Hidden if working 3</div>
<div class="content">
<div class="product"></div>
</div>
</div>
</div>
//Try this using Jquery
<script>
//A perfect reference in Jquery...
var element=$('#target_child');
if($(element).html()==''){
$(element).parent().hide()
}else{
//do some work
}
</script>

jQuery Rearrange HTML Inside Div

I have HTML like this -
<div class="outerDiv" id="od_1">
<label>Div 1</label>
<div class="innerDiv">Inner Div 1</div>
<div class="innerDiv">Inner Div 2</div>
<div class="innerDiv">Inner Div 3</div>
</div>
<div class="outerDiv" id="od_2">
<label>Div 2</label>
<div class="innerDiv">Inner Div 1</div>
<div class="innerDiv">Inner Div 2</div>
<div class="innerDiv">Inner Div 3</div>
</div>
What I want to achieve through jQuery is this -
<div class="outerDiv" id="od_1">
<label>Div 1</label>
<div class="innerDivBox">
<div class="innerDiv">Inner Div 1</div>
<div class="innerDiv">Inner Div 2</div>
<div class="innerDiv">Inner Div 3</div>
</div>
</div>
<div class="outerDiv" id="od_2">
<label>Div 2</label>
<div class="innerDivBox">
<div class="innerDiv">Inner Div 1</div>
<div class="innerDiv">Inner Div 2</div>
<div class="innerDiv">Inner Div 3</div>
</div>
</div>
I started with looping through div with class "outerDiv" -
$('.outerDiv').each(function(i, obj) {
var divId = this.id;
});
But I am not sure how exactly I should proceed from here. Any help would be appreciated. Thanks.
You could use find() and wrapAll() to group the inner divs with a common parent:
$('.outerDiv').each(function(i, obj) {
$(this).find('.innerDiv').wrapAll('<div class="innerDivBox">');
});
jsFiddle here.
Try this:
$('.outerDiv').each(function() {
$(this).append( $('<div class="innerDivBox"></div>').append( $(this).find('.innerDiv') ) );
});

Categories