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();
}
});
});
Related
I would show and hide a button on edit when hovering a div. Like WordPress.
How to display a div inside a div if hovered? I'm trying to do this but there's something wrong, nothing appears.
<div class="row d-flex align-items-center" id="company">
<div class="col-lg-6 mb-4">
<div class="p-5">
<h1>Company</h1>
<p class="text-justify">Description</p>
Start
</div>
</div>
<div class="col-lg-6 mb-4">
<img src="/images/layer.png" alt="Company" class="img-fluid lazy">
</div>
<div class="p-5">
<div id="menu">
<div class="btn-group">
<button type="button" class="btn btn-warning">وEdit</button>
</div>
</div>
</div>
</div
<script>
$("#company").hover(function(){
$("#menu").css("d-none", "d-block");
});
</script>
wordpress
You don't need Javascript for this. You can do it all in CSS. The first rule below defaults to hiding the menu. The second rule overrides the first rule to show the menu when you hover over the #company div.
#menu {
display: none; /* Default */
}
#company:hover #menu {
display: block;
}
If you want to do it with Javascript/jQuery, you must specify separate actions for the mouseenter and mouseleave events, to ensure the menu shows/hides accordingly:
$("#company").hover(
function() {
$("#menu").css("display", "block");
},
function() {
$("#menu").css("display", "none");
},
);
If you want to use purely Bootstrap classes rather than manipulating the CSS style rules directly:
$("#company").hover(
function() {
$("#menu").addClass('d-block').removeClass('d-none');
},
function() {
$("#menu").addClass('d-none').removeClass('d-block');
},
);
Hi your code is almost correct.In your Jquery part you have to use addClass() instead of css() because you are trying to change the class name of the element.Just try this one.Hope this works
<script>
$("#company").hover(function () {
$("#menu").addClass("d-none");
});
</script>
By default the css can be added to the "menu" id
#menu {
display: none;
}
using js:
$("#company").hover(function () {
$("#menu").css("display", "block");
});
I'm working on a list of items for my website.
I have a list of 5 items with a 'id'. When you click the button, an overlay must be shown with 2 buttons 'change to background to red' and than 'cancel'.
If you click the cancel, the specific 'div class="item"' with the specific id his background must become red.
But, the problem is I don't know how using jquery/javascript to know which button of the div what pressed (button of item id 1 or 2 or 3..)
And also when you click outside the buttons, the overlay must be removed.
Here's the code
$(document).ready(() => {
$('.options-btn').click(function ()
{
var id = $(this).parent().attr('id'); /* find <div class="item"> */
$('body').append('<div class="overlay"></div>');
var append = `
<div class="item-options-active">
<button class="feed-option-btn-number background-btn" tabindex="0">Background set to RED</button>
<button class="feed-option-btn-number cancel-btn" tabindex="0">Cancel</button>
</div>
`;
$(append).appendTo('.overlay');
});
$(document).click(function (e)
{
if (document.getElementsByClassName("overlay").length == 1)
{
if(document.getElementsByClassName("item-options-active").length == 1)
{
// this condition is not working when you click the specific button
if($(".background-btn").data('clicked'))
{
// how to get <div> of the button which was press to change the background??
$('.item').css('background', 'red');
}
}
}
});
})
body {
background: grey;
}
.item {
background: green;
border: 5px solid purple;
margin: 20px;
}
.item button {
margin-left: 10px;
}
.overlay {
position: fixed;
width: 100%;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0,0,0,.85);
z-index: 10000;
}
<body>
<div class="show-items">
<div class="item" id="1">
<button type="button" class="options-btn">check options</button>
</div>
<div class="item" id="2">
<button type="button" class="options-btn">check options</button>
</div>
<div class="item" id="3">
<button type="button" class="options-btn">check options</button>
</div>
<div class="item" id="4">
<button type="button" class="options-btn">check options</button>
</div>
<div class="item" id="5">
<button type="button" class="options-btn">check options</button>
</div>
</div>
</body>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
If you rewrite the .option-btn click event to something like this and delete the document click event it should work:
$('.options-btn').click(function ()
{
var id = $(this).parent().attr('id'); /* find <div class="item"> */
var button = $(this);
$('body').append('<div class="overlay"></div>');
var append = `
<div class="item-options-active">
<button class="feed-option-btn-number background-btn" tabindex="0">Background set to RED</button>
<button class="feed-option-btn-number cancel-btn" tabindex="0">Cancel</button>
</div>
`;
$(append).appendTo('.overlay');
$('.overlay').on('click', function(){
$(this).remove();
}).find('.cancel-btn').on('click', function(){
$(this).closest('.overlay').remove();
});
$('.overlay').find('.background-btn').on('click', function(){
button.closest('.item').css('background', 'red');
$(this).closest('.overlay').remove();
});
});
Working fiddle: https://jsfiddle.net/qntupzj6/
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?
Consider the below code snippet:
<div class="row">
<div class="col-sm-12">
<div class="form-group form-group-default">
<label>OPTIONS</label>
<div class="checkbox">
<input type="checkbox" id="checkbox2">
<label for="checkbox2">Email</label>
</div>
</div>
</div>
</div>
Whenever I hover over the <div class="checkbox">, focus event of <div class="form-group form-group-default"> gets triggered; where as in css code I didnt find any relevant hover code for <div class="checkbox">.
As far as i know all events of DOM will get propagate through it's parents. Unless some event handler stops it.
Look at this example.
<!-- https://jsfiddle.net/z0ncxhfq/ -->
<div class="parent">
<a class="dontpropagate child" href="#one">Link one</a>
</div>
<div id="dont" class="parent">
<a class="child" href="#two">Link two</a>
</div>
<script>
var elements = document.querySelectorAll(".dontpropagate");
for(var i = 0, len = elements.length; i < len; ++i) {
elements[i].addEventListener("click", stopPropagation, false);
}
function stopPropagation(event) {
event.stopPropagation();
}
</script>
If I understood it right, you have to simply remove the form-group styling.
Try:
.form-group:focus,
.form-group:hover,
.form-group:active {
outline: none !important;
}
:)
I started making a website few days ago and I'm stuck at this step:
http://jsfiddle.net/r6uhczks/
CSS:
/* This section to be trigger of dropdown process */
.parent_style { background: -webkit-linear-gradient(left,rgba(0,0,0,0.5),transparent); width: 325px; height: 21px; }
.parent_style:hover { background: -webkit-linear-gradient(left,rgba(242,182,0,0.7),transparent); }
.parent_style .textP {}
.parent_style:hover .textP {color: red;}
/* This section to be shown by clicking on ↑ and hidden by clicking for second time */
.child_style { background: rgba(0, 0, 0, 0.5); width: 325px; height; auto; }
.child_style:hover { background: rgba(242,182,0,0.7);}
.child_style .textC {color: white;}
.child_style:hover .textC {color: black;}
As you can see I created two styles for dropdown menu,
.parent_style
defines trigger of show/hide content defined by
.child_style
Simplier I want to make a class="parent_style" to be show/hide trigger of shown/hidden class="child_style"
The problem is that I don't know how to create this action by click with CSS/HTML only, I know only by hover.
HTML:
<div class="parent_style">
<p class="textP">Something</p>
</div>
<div class="child_style">
<p class="textC">Content</p>
</div>
<div class="child_style">
<p class="textC">Content2</p>
</div>
<div class="child_style">
<p class="textC">Content3</p>
</div>
<div class="parent_style">
<p class="textP">Something else</p>
</div>
<div class="child_style">
<p class="textC">Content</p>
</div>
<div class="child_style">
<p class="textC">Content2</p>
</div>
<div class="child_style">
<p class="textC">Content3</p>
</div>
<div class="parent_style">
<p class="textP">Something else 2</p>
</div>
<div class="child_style">
<p class="textC">Content</p>
</div>
<div class="child_style">
<p class="textC">Content2</p>
</div>
<div class="child_style">
<p class="textC">Content3</p>
</div>
<br>
EDIT: and also I would like to know how to edit spaces between these elements, best to remove them.
You can add state to CSS and HTML by using checkboxes (persistent) or focus (temporary):
http://jsfiddle.net/rudiedirkx/L40zcjfc/
HTML
<button>hold down here</button>
<ul>
<li>Foo</li>
<li>Bar</li>
<li>Baz</li>
</ul>
<br />
<br />
<button><label for="cb1">click here</label></button>
<input id="cb1" type="checkbox" />
<ul>
<li>Foo</li>
<li>Bar</li>
<li>Baz</li>
</ul>
CSS
input,
ul {
display: none;
}
button:active + ul,
input:checked + ul {
display: block;
}
The persistence comes from the checkbox's :checked state. You can toggle the checkbox with a <label> so you don't have to know it's there.
The temporariness comes from the :active state of a button. Maybe you can use a normal link, but that might keep focus and/or active.
If you can use custom html and css, i'm sure you can stick a custom script at the bottom of the page if it doesn't allow you to add to the head (keep in mind this script requires the page to be loaded first).
Because the elements are not paired together in individual divs, the javascript is a bit longer but nothing too complicated. This script I created will scan through all the divs on the page and will allow the onclick function to determine what the next three divs are. This means that the script is custom to your layout of navigation so you'll need to change the javascript with a layout change.
Here is a working jsfiddle: http://jsfiddle.net/sLc4svam/
<script>
var divs = document.getElementsByTagName("div");
var parents = document.getElementsByClassName("parent_style");
for (var i=0; i<parents.length; i++) {
parents[i].onclick = function() { toggleChildren(this); };
}
function toggleChildren(elem) {
for (var i=0; i<divs.length;i++) {
if (divs[i] == elem) {
for (var ii=1; ii<=3; ii++) { // The 3 is how many children it will toggle
if (divs[i+ii].style.display == "none") {
divs[i+ii].style.display = "block";
} else {
divs[i+ii].style.display = "none";
}
}
}
}
}
</script>