jquery ui resizable with .each() works only on first child - html

This is the initial html which has 'n' no. of child elements with same class name.
<div class="reading-content">
<div class="c-resourceitem-content">
<div data-id="1" class="resource"></div>
<div class="btn" id="btn"></div>
</div>
<div class="c-resourceitem-content">
<div data-id="2" class="resource"></div>
<div class="btn" id="btn"></div>
</div>
<div class="c-resourceitem-content">
<div data-id="3" class="resource"></div>
<div class="btn" id="btn"></div>
</div></div>`
Javascript: Appending a div as a handler which resizes the element vertically
$('.reading-content').children('.c-resourceitem-content').each(function eachFn() {
$(this).children().wrapAll("<div class='resourceitem-content-wrap'></div>");
var id = $(this).children("resource").attr('id');
ResourceSplitter = $('<label class="resource-splitter ">'+'</label>').attr("id", "id_" + id);
$( ResourceSplitter).appendTo($(this));
$(this).resizable({
handles: { 's' : '.resource-splitter' }
});
});
The final html snippet looks like this by wrapping all child div and appending a handler for resizing as per need .
<div class="reading-content">
<div class="c-resourceitem-content">
<div class="resourceitem-content-wrap">
<div data-id="1" class="resource"></div>
<div class="btn" id="btn"></div>
</div>
<label class="resource-splitter" id="id_1"></label>
</div>
</div>
But the problem is that resizing happens only for the first child element with the class 'c-resourceitem-content' inside the .each() function.
Please do help me out with a solution so that all the child classes are resizable by using the handler appended to each div.
CSS:
.resourceitem-content-wrap{
overflow-y:auto;
overflow-x: hidden;
width:100%;
height:100%;
}
.resource-splitter{
background:#ccc;
height:5px;
border-left:none;
display:block;
flex: 0 0 auto;
cursor: row-resize;
z-index: 80;
}
.reading-content {
height:auto;
margin-top: 15px;
margin-right: 15px;
}

Noticed with the code provided the id applied to the resource-splitter is undefined. Using the index value on the .each function will remove the need for this code:
var id = $(this).children("resource").attr('id');
The index will enumerate over each c-resourceitem-content, I've added an id variable that starts from 1. Like below:
$(".reading-content")
.children(".c-resourceitem-content")
.each(function eachFn(index) {
let id = index + 1;
$(this)
.children()
.wrapAll("<div class='resourceitem-content-wrap'></div>");
ResourceSplitter = $(
'<label class="resource-splitter ">' + "</label>"
).attr("id", "id_" + id);
$(ResourceSplitter).appendTo($(this));
$(this).resizable({
handles: { 's': ".resource-splitter" }
});
});
Are you able to provide the styling applied to the html so I can test it further?

Related

Cloning parent and its first + last child in jQuery

Say I have the following the markup:
$('button').click(function() {
let row = $('div.row');
let clonedRow = row.clone(true);
clonedRow.appendTo('body');
});
.row {
border: 1px solid black;
margin-bottom: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>append</button>
<div class="row">
<div class="first">append me!</div>
<div class="item">DONT append me</div>
<div class="item">DONT append me</div>
<div class="last">append me!</div>
</div>
I can achieve this by using clone() multiple times, but I'm looking for a clean solution that can do it with as little methods/functions as possible. Has to be in jQuery and not vanilla JS.
You can use the remove() method to remove the .item elements from the cloned content:
$('button').click(function() {
let $row = $('div.row:first').clone(true);
$row.find('.item').remove();
$row.appendTo('body');
});
.row {
border: 1px solid black;
margin-bottom: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>append</button>
<div class="row">
<div class="first">append me!</div>
<div class="item">DONT append me</div>
<div class="item">DONT append me</div>
<div class="last">append me!</div>
</div>
First you have to select row, then clone that row & then remove .items elements from that content by remove() method & then append that to the body.
$('button').click(function(e) {
e.preventDefault();
let row = $('div.row:first');
let clonedRow = $(row).clone(true);
$(clonedRow).find("div.item").remove();
$(clonedRow).appendTo('body');
});

How to compare two classes with jquery?

I want to compare two classes if they're equal to display the content. I have three btns with three different classes and three other divs with three similar classes to buttons. I want to check if Over the button is equal to over the div,then i want to show the element inside over the div, and hide the other elements,and when i click on the under the button i want to do the same, I have tried with Jquery to compare to first get the class name from the button and get the class name from the div and compare them to each other and then give the active class to the one that i want to show it but it seems that i have something wrong
var className = $(this).attr('class');
var tabContent = $('.tab-content').hasClass(className);
var classNameBtnsName = $(this).hasClass(className);
$('button').click(function () {
if (tabContent === classNameBtnsName) {
$('.tab-content').addClass("active-content");
} else {
$('.tab-content').removeClass("active-content");
}
})
.active-content h1{
display:block;
}
h1{
display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="btns-container">
<button class="over">Over</button>
<button class="under">Under</button>
<button class="other">Other</button>
</div>
<div class="tab-content over active-content">
<h1 >Show Over elements</h1>
</div>
<div class="tab-content under">
<h1>Show Under elements</h1>
</div>
<div class="tab-content other">
<h1>Show Other Elements</h1>
</div>
I wouldn't use the class of the buttons to specify which content to toggle. It is better to use a data attribute for that to prevent future developments that add more classes to bug your functionality. Then you can use this data attribute for toggling by removing the active class from all and then adding the class to the element belonging to the clicked button.
$('button.togglebutton').on('click', (e) => {
$('.tab-content').removeClass('active-content');
$('.tab-content.' + $(e.currentTarget).data('active-content')).addClass('active-content');
});
.active-content h1 {
display: block;
}
h1 {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="btns-container">
<button data-active-content="over" class="togglebutton">Over</button>
<button data-active-content="under" class="togglebutton">Under</button>
<button data-active-content="other" class="togglebutton">Other</button>
</div>
<div class="tab-content over active-content">
<h1>Show Over elements</h1>
</div>
<div class="tab-content under">
<h1>Show Under elements</h1>
</div>
<div class="tab-content other">
<h1>Show Other Elements</h1>
</div>
Inside the click handler callback function, remove the active-content class from all .tab-content elements first, and then add it to the one that also has the button's class.
$('.btns-container button').on('click', function() {
$('.tab-content').removeClass('active-content');
$('.tab-content.' + this.className).addClass('active-content');
});
.active-content h1{
display:block;
}
h1{
display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="btns-container">
<button class="over">Over</button>
<button class="under">Under</button>
<button class="other">Other</button>
</div>
<div class="tab-content over active-content">
<h1 >Show Over elements</h1>
</div>
<div class="tab-content under">
<h1>Show Under elements</h1>
</div>
<div class="tab-content other">
<h1>Show Other Elements</h1>
</div>
Assuming .tab-content is hidden by default, you can try something like this:
$('button').click(function() {
// Get class from clicked button
var btnClass = $(this).attr('class');
$('.tab-content').each(function() {
// If tab-content has same class as button, show this
if ($(this).hasClass(btnClass)) {
$(this).show();
} else {
$(this).hide();
}
});
});

Open a div by clicking a button, close the div by clicking outside only with CSS

I currently have a button that opens and closes a div. I would like to improve this feature: I want the div to close also when pressing other than on div.
Is it possible to do this in CSS and HTML?
It's for the button 'top-bar-btn settings' :
EDIT
<div class="top-bar-btn" id="top-bar-btn">
<button class="top-bar-btn create"
title="Créer une nouvelle discussion">
<!--(click)="onCreateChatWindow()">-->
<i class="glyphicon glyphicon-edit"></i>
</button>
<button class="top-bar-btn settings"
title="Réglages"
(click)="onSettingsChatWindow()">
<i class="glyphicon glyphicon-cog"></i>
</button>
<div id="outside" (click)="hide()"></div>
<button class="top-bar-btn reduce"
title="Réduire la fenêtre"
(click) = "onReduceChatThreads()">
<i class="glyphicon glyphicon-minus"></i>
</button>
</div>
<div class="settings-notification"
id="settings-notification">
<div class="tr triangle">
<div class="tr inner-triangle">
</div>
</div>
<div class="sound-signal">
Signal sonore :
<input type="radio" name="soundsignal" id="soundsignal1" checked="checked">
<label for="soundsignal1">Oui</label>
<input type="radio" name="soundsignal" id="soundsignal2">
<label for="soundsignal2">Non</label>
</div>
<div class="flash-signal">
Signal visuel :
<input type="radio" name="flashsignal" id="flashsignal1" checked="checked" (click)="flashSignal()">
<label for="flashsignal1">Oui</label>
<input type="radio" name="flashsignal" id="flashsignal2" (click)="notFlashSignal()">
<label for="flashsignal2">Non</label>
</div>
>
#outside{
display:none;
height:100%;
width:100%;
position:fixed;
top:0px;
left:0px;
z-index:1;
}
hide(): void {
document.getElementById('outside').style.display = 'none';
document.getElementById('settings-notification').style.display = 'none';
}
onSettingsChatWindow(): void {
document.getElementById('outside').style.display = 'block';
document.getElementById('chat-threads').classList.toggle('display-settings');
}
I do not understand why it does not work.
This works only once and after the div no longer displays at all. Do you know why ?
You can't show/hide, move... objects using CSS or HTML only. But you can use JavaScript. Make two divs with display:none; one is your div you want to show, and another one is background(width and height 100%, z-index less than div you want to show/hide but more than any other element, and position:fixed;) inside div that represents background add onclick="hide()" and that will call a function hide() whenever you click outside your div. And just add function hide() that will make both div-s disappear.
function myFunction(){
document.getElementById("outside").style.display = "block";
document.getElementById("myDiv").style.display = "block";
}
function hide(){
document.getElementById("outside").style.display = "none";
document.getElementById("myDiv").style.display = "none";
}
#outside{
display:none;
height:100%;
width:100%;
position:fixed;
top:0px;
left:0px;
z-index:1;
}
#myDiv{
background:red;
width:300px;
height:100px;
position:absolute;
top:100px;
left:100px;
display:none;
z-index:2;
}
<button onclick="myFunction()">Show div</button>
<p><span>some text</span></p>
<p><span>some text</span></p>
<p><span>some text</span></p>
<p><span>some text</span></p>
<p><span>some text</span></p>
<div id="outside" onclick="hide()"></div>
<div id="myDiv"></div>
It is not possible using just HTML and CSS, but this JavaScript should do it:
window.onclick = function(event) {
if (!event.target.matches('#settings-notification')) {
document.getElementById('chat-threads').classList.toggle('display-
settings');
}
}
When the user clicks anywhere, the script checks if they have clicked on the div, or on the rest of the page. If it is the rest of the page, then it closes the div.

CSS Opacity When Other Classes Present

I am working with another developer's code and want to know if there is a way to apply a different opacity to "scrollbar" using CSS code when "overlay" is present but not when "overlay-inactive" is present under "table" as shown below. Is this possible?
/*Want scrollbar to have opacity:1 in this table*/
<div class ="table">
<div class = "overlay"></div>
<div class = "scrollbar"></div>
</div>
/*Want scrollbar to have opacity:0 in this table*/
<div class ="table">
<div class = "overlay-inactive"></div>
<div class = "scrollbar"></div>
</div>
/*Class structure is the developer's and cannot be changed to have scrollbar as a subclass of overlay*/
Use the + operator to reference the next element to current element.
.overlay + .scrollbar {
opacity: 1;
}
.overlay-inactive + .scrollbar {
opacity: 0;
}
<div class="table">
<div class="overlay">1 - You can see me</div>
<div class="scrollbar">2 - You can see me</div>
</div>
<div class="table">
<div class="overlay-inactive">1 - You can see me but not below</div>
<div class="scrollbar">2 - Hidden content</div>
</div>
Use the adjacent sibling operator + to determine if the class exists on an adjacent element:
.overlay + .scrollbar {
opacity: 1;
}
.overlay-inactive + .scrollbar {
opacity: 0;
}

html can't click on checkboxes

This is my html:
<div style="width: 45%; float: left; margin-left:5%">
<div class="chartHeaderClass" style="width: 100%;">
<h3>Service Level Per Campaign</h3>
<%-- Start Dropdown Code --%>
<a id="DropdownSeviceLink" href="#">+</a>
<div ID="campaignDiv" runat="server" ><ul>
</ul>
</div>
<script type="text/javascript" src="Scripts/DropdownCheckbox.js"></script>
<%-- End Dropdown Code --%>
</div>
<div id="line-chart" class="chart-holder" style="border:1px solid #D5D5D5; margin-top:2px">
<canvas class="overlay" width="479" height="265"></canvas>
</div>
</div>
<div style="width: 45%; float: right">
<div class="chartHeaderClass" style="width: 100%;">
<h3>Calls Per Campaign</h3>
</div>
<div id="pie-chart" class="chart-holder" style="border:1px solid #D5D5D5; margin-top:2px">
<canvas class="overlay" width="479" height="265"></canvas>
</div>
</div>
notice that it has this div campaignDiv which I fill in c# like this:
if (!IsPostBack) {
List<string> comps = getCompainNames();
string html = "<ul>";
for (int i = 0; i < comps.Count(); i++) {
html = html + CreateLiCheckbox(comps[i]);
}
html = html + "</ul>";
campaignDiv.InnerHtml = html;
}
private string CreateLiCheckbox(string checkBoxText)
{
return string.Format("<li><input type=\"checkbox\">{0}</li>", checkBoxText);
}
This result is this:
I can't click on the checkboxes. In other words, when I click on them nothing happens
I noticed something
I can't select the text inside the red area. It seems that it is not exit because when I tried to select it using the mouse, nothing becomes selected.
could u help please?
css for this red area is
#DropdownSeviceLink {
float:right;
margin-right:10px;
}
a#DropdownServiceLink:visited {
color:inherit;
}
#campaignDiv {
background-color:red;
width:200px;
height:200px;
float:right;
position:relative;
}
Finally the context of my page is
when clicking on that plus sign, I want to show this red area, I can do that on jquery, but I just told you the context maybe that helps
jsfiddle
http://jsfiddle.net/jdhMs/
I had this exact same issue in my application and it turned out that the div I had my checkboxes in was being overlapped by the footer of the modal box. Since the footer of the modal box was white/transparent, this wasn't evident until I added a border around the footer. Only then I was able to see that I could see the checkboxes but couldn't actually select them.
I don't know its work or not but try Z-Index.
Example:
#campaignDiv {
Z-Index:100;
}
li{
Z-Index:101;
}