I'm trying to create a minigame using JavaScript, HTML and CSS. It is structured as a table 4x4. Every row of my table is memorized inside a <section> tag. Each <section> cointains 4 <div>s. I would like to make one random element of the table appear when I hit a button. Also, it would be great to have elements appear in a random order.
Here's part of my code:
<section class="oggettoElements">
<i class="far fa-newspaper oggetto" draggable="true" style="color: #0000FF;" id="11"></i>
<i class="fas fa-wine-glass-alt oggetto" draggable="true" style="color: #008000;" id="21"></i>
<i class="fas fa-wine-bottle oggetto" draggable="true" style="color: #FFD700;" id="31"></i>
<i class="fas fa-apple-alt oggetto" draggable="true" style="color: #8B4513;" id="41"></i>
</section>
<section class="oggettoElements">
<i class="far fa-newspaper oggetto" draggable="true" style="color: #0000FF;" id="12"></i>
<i class="fas fa-wine-glass-alt oggetto" draggable="true" style="color: #008000;" id="22"></i>
<i class="fas fa-wine-bottle oggetto" draggable="true" style="color: #FFD700;" id="32"></i>
<i class="fas fa-apple-alt oggetto" draggable="true" style="color: #8B4513;" id="42"></i>
</section>
<section class="oggettoElements">
<i class="far fa-newspaper oggetto" draggable="true" style="color: #0000FF;" id="13"></i>
<i class="fas fa-wine-glass-alt oggetto" draggable="true" style="color: #008000;" id="23"></i>
<i class="fas fa-wine-bottle oggetto" draggable="true" style="color: #FFD700;" id="33"></i>
<i class="fas fa-apple-alt oggetto" draggable="true" style="color: #8B4513;" id="43"></i>
</section>
<section class="oggettoElements">
<i class="far fa-newspaper oggetto" draggable="true" style="color: #0000FF;" id="14"></i>
<i class="fas fa-wine-glass-alt oggetto" draggable="true" style="color: #008000;" id="24"></i>
<i class="fas fa-wine-bottle oggetto" draggable="true" style="color: #FFD700;" id="34"></i>
<i class="fas fa-apple-alt oggetto" draggable="true" style="color: #8B4513;" id="44"></i>
</section>
So, my goal is: have a function to make one element at a time appear (I'll call it with an onClick attribute in a button). The element will be a random one from the table.
Can anybody help me? Thank you very much
The following inline code snippet implements the random apparition of grid elements and demonstrates some (hopefully) helpful concepts:
Css-based layout using the display property for tabular layout;
Content centering with Css property display: flex;
Programmatic access to DOM element(s) using document.querySelector(...), document.querySelectorAll(...);
A DOM NodeList is not an array. Similar surprises abound, reference docs & specs are your friend. The docs, not YouTube videos ...
Remember that id attribute values must not be numeric. Avoid over-optimizing - the selection of elements not yet on display is extremely primitive, the more elements are visible, the more attempts are needed to find a hidden element. However, in response to interaction with slow humans, efficiency is not an issue ... ;)
let b_allVisible = false
;
function showme ( eve ) {
let e_chosen
, n_c
, n_r
;
b_allVisible = !(
Array
.from(document.querySelectorAll('div[id^="c-"]'))
.some ( (pe_grid) => { return pe_grid.style.visibility !== 'visible'; } )
);
if (b_allVisible) {
document.getElementById('showme').setAttribute('disabled', 'disabled');
} else {
do {
n_c = 1 + Math.floor( 4 * Math.random() );
n_r = 1 + Math.floor( 4 * Math.random() );
e_chosen = document.querySelector(`#c-${n_c}${n_r}`);
// Here (referencing an element by their id), 'document.getElementById(`c-${n_c}${n_r}`)' can also be used.
// Note the absence of the selector marker '#' for ids
} while ( e_chosen.style.visibility === 'visible' );
e_chosen.style.visibility = 'visible';
}
} // showme
body > section {
display: table;
padding: 20px;
}
section.oggettoElements {
display: table-row;
}
section.oggettoElements > div {
display: table-cell;
border: solid 1px black;
height: 30px;
width: 30px;
visibility: hidden;
}
section.oggettoElements > div > div {
display: flex;
height: 30px;
align-items: center;
justify-content: center;
}
div > span {
flex: 1;
text-align: center
}
<section>
<section class="oggettoElements">
<div class="far fa-newspaper oggetto" draggable="true" style="color: #0000FF;" id="c-11"><div><span>01</span></div></div>
<div class="fas fa-wine-glass-alt oggetto" draggable="true" style="color: #008000;" id="c-21"><div><span>02</span></div></div>
<div class="fas fa-wine-bottle oggetto" draggable="true" style="color: #FFD700;" id="c-31"><div><span>03</span></div></div>
<div class="fas fa-apple-alt oggetto" draggable="true" style="color: #8B4513;" id="c-41"><div><span>04</span></div></div>
</section>
<section class="oggettoElements">
<div class="far fa-newspaper oggetto" draggable="true" style="color: #0000FF;" id="c-12"><div><span>05</span></div></div>
<div class="fas fa-wine-glass-alt oggetto" draggable="true" style="color: #008000;" id="c-22"><div><span>06</span></div></div>
<div class="fas fa-wine-bottle oggetto" draggable="true" style="color: #FFD700;" id="c-32"><div><span>07</span></div></div>
<div class="fas fa-apple-alt oggetto" draggable="true" style="color: #8B4513;" id="c-42"><div><span>08</span></div></div>
</section>
<section class="oggettoElements">
<div class="far fa-newspaper oggetto" draggable="true" style="color: #0000FF;" id="c-13"><div><span>09</span></div></div>
<div class="fas fa-wine-glass-alt oggetto" draggable="true" style="color: #008000;" id="c-23"><div><span>10</span></div></div>
<div class="fas fa-wine-bottle oggetto" draggable="true" style="color: #FFD700;" id="c-33"><div><span>11</span></div></div>
<div class="fas fa-apple-alt oggetto" draggable="true" style="color: #8B4513;" id="c-43"><div><span>12</span></div></div>
</section>
<section class="oggettoElements">
<div class="far fa-newspaper oggetto" draggable="true" style="color: #0000FF;" id="c-14"><div><span>13</span></div></div>
<div class="fas fa-wine-glass-alt oggetto" draggable="true" style="color: #008000;" id="c-24"><div><span>14</span></div></div>
<div class="fas fa-wine-bottle oggetto" draggable="true" style="color: #FFD700;" id="c-34"><div><span>15</span></div></div>
<div class="fas fa-apple-alt oggetto" draggable="true" style="color: #8B4513;" id="c-44"><div><span>16</span></div></div>
</section>
</section>
<button id="showme" name="showme" onclick="showme()">Show Me!</button>
Related
I hope your doing great. I want to add a switch that switches between "Monthly" and "Yearly". When Switched to yearly, I want the price shown to change from 9$ to 6$. I basically want a toggle switch to change text.
<!-- Pricing -->
<div class="v-line v-line-left"> <div class="container">
<div class="pricing-items row">
<div class="pricing-col col-xs-12 col-sm-6 col-md-6 col-lg-4">
<div class="pricing-item scrolla-element-anim-1 scroll-animate" data-animate="active">
<div class="lui-subtitle">
<span> Essentials </span>
</div>
<div class="icon"></div>
<div class="price">
<span>9</span><span>$</span>
<em>PM</em>
</div>
<br><br>
<label>
<input type="checkbox" name="switch" value="on" onclick="toggleyearmonth()">
<span class="switch"></span>
</label>
<div class="lui-text">
<div>
<p>Get the essentials you need to succeed with our budget-friendly plan. Includes access to our library of study materials, email support from our team of experts, and summaries.</p>
</div>
</div>
<div class="list">
<div>
<ul>
<li>
<i class="fas fa-check"></i>Summaries
</li>
<li>
<i class="fas fa-check"></i>Chromebook Tips/Tricks
</li>
<li>
<em>H.W Answers & Explanations</em>
</li>
<li>
<em>Weekly Revision Meetings</em>
</li>
</ul>
</div>
</div>
<a href="https://buy.stripe.com/8wMg1bdvwc9Z9Gg003" class="btn btn-solid">
<span>Join Now!</span>
</a>
<div class="bg-img" style="background-image: url(assets/images/pat-2.png);"></div>
</div>
</div>
<div class="pricing-col center col-xs-12 col-sm-6 col-md-6 col-lg-4">
<div class="label">
<span> Popular </span>
</div>
<div class="pricing-item scrolla-element-anim-1 scroll-animate" data-animate="active">
<div class="lui-subtitle">
<span> Power User </span>
</div>
<div class="icon"></div>
<div class="price">
<em><s>$12</s></em> <span> 8 <b>$</b>
</span>
<em>PM</em>
</div>
<div class="lui-text">
<div>
<p>Take your academic success to the next level with our Power User plan. Includes access to all of H.W answers/expinations, and priority access to our team for one-on-one help.</p>
</div>
</div>
<div class="list">
<div>
<ul>
<li>
<i class="fas fa-check"></i>Summaries
</li>
<li>
<i class="fas fa-check"></i>Chromebook Tips/Tricks
</li>
<li>
<i class="fas fa-check"></i>H.W Answers & Explanations
</li>
<li>
<em>Weekly Revision Meetings</em>
</li>
</ul>
</div>
</div>
<a href="https://buy.stripe.com/cN2dT3ezA4Hx7y8bIM" class="btn btn-solid">
<span>Join Now!</span>
</a>
<div class="bg-img" style="background-image: url(assets/images/pat-2.png);"></div>
</div>
</div>
<div class="pricing-col col-xs-12 col-sm-6 col-md-6 col-lg-4">
<div class="pricing-item scrolla-element-anim-1 scroll-animate" data-animate="active">
<div class="lui-subtitle">
<span> Ultimate </span>
</div>
<div class="icon"></div>
<div class="price">
<em><s>$17</s></em><span> 12 <b>$</b>
</span>
<em>PM</em>
</div>
<div class="lui-text">
<div>
<p>Achieve ultimate success with our top-tier plan. Includes access to all of our study materials, priority access to our team for one-on-one help, and exclusive online meetings the day before any test to ensure you're fully prepared.</p>
</div>
</div>
<div class="list">
<div>
<ul>
<li>
<i class="fas fa-check"></i>Summaries
</li>
<li>
<i class="fas fa-check"></i> <span style="font-weight: bold; color: #30a484;">Exclusive</span> Chromebook Tips/Tricks
</li>
<li>
<i class="fas fa-check"></i> <span style="font-weight: bold; color: #30a484;">Preformance Tasks</span> & H.W Answers/Explanations
</li>
<li>
<i class="fas fa-check"></i>Weekly Revision Meetings
</li>
</ul>
</div>
</div>
<a href="https://buy.stripe.com/dR69CN6342zp8Cc8wB" class="btn btn-solid">
<span>Join Now!</span>
</a>
<div class="bg-img" style="background-image: url(assets/images/pat-2.png);"></div>
</div>
</div>
</div>
<div class="lui-bgtitle">
<span> Pricing </span>
</div>
</div>
</div>
<div class="container">
<div class="lui-heading">
<div class="container">
<div class="m-titles align-center">
<h2 class="m-title splitting-text-anim-1 scroll-animate" data-splitting="words" data-animate="active">
<br>
<br>
<br>
<span> What are you waiting for? Join now and guarantee your A+ this year! </span>
</h2>
</div>
</div>
</section>
Thank you.
I have tried searching for a solution for hours but got no luck. I even tried chat gpt...
[UPDATED]
If i understand you right you just want to change the price by flipping a switch.
I created one and a basic JS function for it.
Greetings
[UPDATE]
Changed so it appends on all the prices and automatically divides 20% of the price down. Also changed the default "PM" to either PY or PM.
You can edit the switch I inserted as you want.
The only thing important is the function toggleyearmonth();
function toggleyearmonth() {
document.querySelectorAll(".price span").forEach(pricenum => {
if(pricenum.getAttribute("pm")){
if(pricenum.getAttribute("pm") == pricenum.innerText){
pricenum.innerText = (pricenum.getAttribute("pm") - pricenum.getAttribute("pm")/100*20).toFixed(0);
pricenum.parentNode.childNodes[4].innerText = "PY";
} else {
pricenum.innerText = pricenum.getAttribute("pm");
pricenum.parentNode.childNodes[4].innerText = "PM";
}
}
})
};
input[type="checkbox"] {
display: none;
}
input[type="checkbox"]+.switch {
display: inline-block;
position: absolute;
width: 60px;
height: 34px;
margin: 0;
padding: 0;
background: #ddd;
border-radius: 20px;
position: relative;
vertical-align: middle;
-webkit-transition: background 0.3s ease;
transition: background 0.3s ease;
}
input[type="checkbox"]+.switch:before {
content: "";
width: 50px;
height: 50px;
background: #fff;
border-radius: 20px;
position: absolute;
top: -8px;
left: -5px;
-webkit-transition: all 0.3s ease;
transition: all 0.3s ease;
}
input[type="checkbox"]:checked+.switch {
background: #4caf50;
}
input[type="checkbox"]:checked+.switch:before {
left: calc(100% - 30px);
}
<label>
<input type="checkbox" name="switch" value="on" onclick="toggleyearmonth()">
<span class="switch"></span>
</label>
<br><br>
<!-- Pricing -->
<div class="v-line v-line-left">
<div class="container">
<div class="pricing-items row">
<div class="pricing-col col-xs-12 col-sm-6 col-md-6 col-lg-4">
<div class="pricing-item scrolla-element-anim-1 scroll-animate" data-animate="active">
<div class="lui-subtitle">
<span> Essentials </span>
</div>
<div class="icon"></div>
<div class="price">
<span pm=9>9</span><span>$</span>
<em>PM</em>
</div>
<br><br>
<div class="lui-text">
<div>
<p>Get the essentials you need to succeed with our budget-friendly plan. Includes access to our library of study materials, email support from our team of experts, and summaries.</p>
</div>
</div>
<div class="list">
<div>
<ul>
<li>
<i class="fas fa-check"></i>Summaries
</li>
<li>
<i class="fas fa-check"></i>Chromebook Tips/Tricks
</li>
<li>
<em>H.W Answers & Explanations</em>
</li>
<li>
<em>Weekly Revision Meetings</em>
</li>
</ul>
</div>
</div>
<a href="https://buy.stripe.com/8wMg1bdvwc9Z9Gg003" class="btn btn-solid">
<span>Join Now!</span>
</a>
<div class="bg-img" style="background-image: url(assets/images/pat-2.png);"></div>
</div>
</div>
<div class="pricing-col center col-xs-12 col-sm-6 col-md-6 col-lg-4">
<div class="label">
<span> Popular </span>
</div>
<div class="pricing-item scrolla-element-anim-1 scroll-animate" data-animate="active">
<div class="lui-subtitle">
<span> Power User </span>
</div>
<div class="icon"></div>
<div class="price">
<span pm=12>12</span><span>$</span>
<em>PM</em>
</div>
<div class="lui-text">
<div>
<p>Take your academic success to the next level with our Power User plan. Includes access to all of H.W answers/expinations, and priority access to our team for one-on-one help.</p>
</div>
</div>
<div class="list">
<div>
<ul>
<li>
<i class="fas fa-check"></i>Summaries
</li>
<li>
<i class="fas fa-check"></i>Chromebook Tips/Tricks
</li>
<li>
<i class="fas fa-check"></i>H.W Answers & Explanations
</li>
<li>
<em>Weekly Revision Meetings</em>
</li>
</ul>
</div>
</div>
<a href="https://buy.stripe.com/cN2dT3ezA4Hx7y8bIM" class="btn btn-solid">
<span>Join Now!</span>
</a>
<div class="bg-img" style="background-image: url(assets/images/pat-2.png);"></div>
</div>
</div>
<div class="pricing-col col-xs-12 col-sm-6 col-md-6 col-lg-4">
<div class="pricing-item scrolla-element-anim-1 scroll-animate" data-animate="active">
<div class="lui-subtitle">
<span> Ultimate </span>
</div>
<div class="icon"></div>
<div class="price">
<span pm=17>17</span><span>$</span>
<em>PM</em>
</div>
<div class="lui-text">
<div>
<p>Achieve ultimate success with our top-tier plan. Includes access to all of our study materials, priority access to our team for one-on-one help, and exclusive online meetings the day before any test to ensure you're fully prepared.</p>
</div>
</div>
<div class="list">
<div>
<ul>
<li>
<i class="fas fa-check"></i>Summaries
</li>
<li>
<i class="fas fa-check"></i> <span style="font-weight: bold; color: #30a484;">Exclusive</span> Chromebook Tips/Tricks
</li>
<li>
<i class="fas fa-check"></i> <span style="font-weight: bold; color: #30a484;">Preformance Tasks</span> & H.W Answers/Explanations
</li>
<li>
<i class="fas fa-check"></i>Weekly Revision Meetings
</li>
</ul>
</div>
</div>
<a href="https://buy.stripe.com/dR69CN6342zp8Cc8wB" class="btn btn-solid">
<span>Join Now!</span>
</a>
<div class="bg-img" style="background-image: url(assets/images/pat-2.png);"></div>
</div>
</div>
</div>
<div class="lui-bgtitle">
<span> Pricing </span>
</div>
</div>
</div>
<div class="container">
<div class="lui-heading">
<div class="container">
<div class="m-titles align-center">
<h2 class="m-title splitting-text-anim-1 scroll-animate" data-splitting="words" data-animate="active">
<br>
<br>
<br>
<span> What are you waiting for? Join now and guarantee your A+ this year! </span>
</h2>
</div>
</div>
</section>
I'm with this product loop, each one has a Details box, which appears when you click "More +", but inside the loop it is showing the box of all products when I click on any one, like can i make this unique for each product?
UPDATE!
Box I want to appear:
<div class="keyFeaturesBox">
<div class="keyFeaturesBox-header">
<i id="keyFeaturesClose" class="far fa-times-circle"></i>
</div>
<div class="keyFeaturesBox-container">
<div class="features">
<i class="fas fa-car"></i>
<span>Front Wheel Drive</span>
</div>
<div class="features">
<i class="fas fa-car"></i>
<span>Front Wheel Drive</span>
</div>
<div class="features">
<i class="fas fa-car"></i>
<span>Front Wheel Drive</span>
</div>
</div>
</div>
CSS:
div.keyFeaturesBox, div.viewSavingsBox {
display: none;
position: absolute;
left: 0;
bottom: 50px;
width: 100%;
height: 180px; max-height: 180px;
background-color: #f1f1f1;
box-sizing: border-box;
border: 2px solid rgba(112, 112, 112, .34);
}
Loop:
for ($i = 0; $i < count($vehicles); $i++) {
echo '<div class="keyFeaturesBox">
<div class="keyFeaturesBox-header">
<i id="keyFeaturesClose" class="far fa-times-circle"></i>
</div>
<div class="keyFeaturesBox-container">
<div class="features">
<i class="fas fa-car"></i>
<span>Front Wheel Drive</span>
</div>
<div class="features">
<i class="fas fa-car"></i>
<span>Front Wheel Drive</span>
</div>
<div class="features">
<i class="fas fa-car"></i>
<span>Front Wheel Drive</span>
</div>
</div>
</div>';
}
HTML Code:
<div class="vehicleDetails"><!-- Details start -->
<div class="keyFeatures">
<span id="keyFeatures" class="keyFeatures-span">Key Features +</span>
</div>
<div class="viewSavings">
<span id="viewSavings" class="viewSavings">View Savings +</span>
</div>
</div><!-- Details end -->
jQuery code:
// Features Box Toggle
$(".keyFeatures-span").click(function() {
$(".keyFeatures").toggleClass("selectedFeatures");
$(".keyFeaturesBox").slideToggle("fast");;
});
You can use .closest() to move up the DOM tree and match any selector.
The example below is fully commented.
You didn't provide any HTML for .keyFeaturesBox so that line might need adapting, as a guess you might need to use .closest(".keyFeatures").find(".keyFeaturesBox") to move up the DOM tree to the wrapper class and then search the children for the features box. This is difficult to work out without all the HTML.
Demo
// Event trigger for click of .keyFeatures-span
$(".keyFeatures-span").click(function() {
// Move up DOM tree and apply class selectedFeatures to first match
$(this).closest(".keyFeatures").toggleClass("selectedFeatures");
// You didn't include .keyFeaturesBox in your example HTML so you might need to adapt this
$(this).closest(".keyFeaturesBox").slideToggle("fast");
});
.selectedFeatures {
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="vehicleDetails">
<div class="keyFeatures">
<span id="keyFeatures" class="keyFeatures-span">Key Features +</span>
</div>
<div class="viewSavings">
<span id="viewSavings" class="viewSavings">View Savings +</span>
</div>
</div>
<div class="vehicleDetails">
<div class="keyFeatures">
<span id="keyFeatures" class="keyFeatures-span">Key Features +</span>
</div>
<div class="viewSavings">
<span id="viewSavings" class="viewSavings">View Savings +</span>
</div>
</div>
I have your code running like so:
<!-- Details start -->
<div class="vehicleDetails">
<div class="keyFeatures">
<span id="keyFeatures" class="keyFeatures-span">Key Features +</span>
<div class="keyFeaturesBox">
<p>a feature</p>
<p>a feature</p>
<p>a feature</p>
<p>a feature</p>
<p>a feature</p>
<p>a feature</p>
<p>a feature</p>
</div>
</div>
<div class="viewSavings">
<span id="viewSavings" class="viewSavings">View Savings +</span>
</div>
</div>
<!-- Details end -->
<script>
// Features Box Toggle
$(".keyFeatures-span").click(function() {
let $parentNode = $(this).parent('.keyFeatures');
$parentNode.toggleClass("selectedFeatures");
$parentNode.find(">.keyFeaturesBox").slideToggle("fast");
});
</script>
because of position: absolute; and the same bottom value all divs shown as overlapped and you see the only last div.
if you remove position: absolute; from your style, it will toggle all divs, but it will toggle from up to down, different than yours.
or after toggle, increase bottoms
$(".keyFeatures-span").click(function() {
$(".keyFeatures").toggleClass("selectedFeatures");
$(".keyFeaturesBox").slideToggle("fast");
$(".keyFeaturesBox").each(function(i,e){
$(this).css("bottom" , i * 180); //180 is your box height in css
});
});
div.keyFeaturesBox, div.viewSavingsBox {
display: none;
position: absolute;
left: 0;
bottom: 50px;
width: 100%;
height: 180px; max-height: 180px;
background-color: #f1f1f1;
box-sizing: border-box;
border: 2px solid rgba(112, 112, 112, .34);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="vehicleDetails"><!-- Details start -->
<div class="keyFeatures">
<span id="keyFeatures" class="keyFeatures-span">Key Features +</span>
</div>
<div class="viewSavings">
<span id="viewSavings" class="viewSavings">View Savings +</span>
</div>
</div>
<div class="keyFeaturesBox">
<div class="keyFeaturesBox-header">
<i id="keyFeaturesClose" class="far fa-times-circle"></i>
</div>
<div class="keyFeaturesBox-container">
<div class="features">
<i class="fas fa-car"></i>
<span>Front Wheel Drive1</span>
</div>
<div class="features">
<i class="fas fa-car"></i>
<span>Front Wheel Drive</span>
</div>
<div class="features">
<i class="fas fa-car"></i>
<span>Front Wheel Drive</span>
</div>
</div>
</div>
<div class="keyFeaturesBox">
<div class="keyFeaturesBox-header">
<i id="keyFeaturesClose" class="far fa-times-circle"></i>
</div>
<div class="keyFeaturesBox-container">
<div class="features">
<i class="fas fa-car"></i>
<span>Front Wheel Drive2</span>
</div>
<div class="features">
<i class="fas fa-car"></i>
<span>Front Wheel Drive</span>
</div>
<div class="features">
<i class="fas fa-car"></i>
<span>Front Wheel Drive</span>
</div>
</div>
</div>
<div class="keyFeaturesBox">
<div class="keyFeaturesBox-header">
<i id="keyFeaturesClose" class="far fa-times-circle"></i>
</div>
<div class="keyFeaturesBox-container">
<div class="features">
<i class="fas fa-car"></i>
<span>Front Wheel Drive3</span>
</div>
<div class="features">
<i class="fas fa-car"></i>
<span>Front Wheel Drive</span>
</div>
<div class="features">
<i class="fas fa-car"></i>
<span>Front Wheel Drive</span>
</div>
</div>
</div>
I am working on one of the requirement, where I have to show div based on user input. Below is the code structure.
<input id="location-filter" type="text">
<div class="item">
<div class="item-image">
<a href="">
<img class="img-fluid" src="">
<div class="item-badges"></div>
<div class="item-meta">
<div class="item-price">
<small></small>
</div>
</div>
</a>
</div>
<div class="item-info">
<h3 class="item-title"></h3>
<div class="item-location">
<i class="fa fa-map-marker "></i>
<p>London</p>
</div>
<div class="item-details-i">
<span class="bedrooms" title="Bedrooms">3 <i class="fa fa-bed"></i></span>
<span class="bathrooms" title="Bathrooms">2 <i class="fa fa-bath"></i></span>
<span class="bathrooms" title="Car Space">1 <i class="fa fa-car"></i></span>
</div>
<div class="item-details">
<p>Read More</p>
</div>
</div>
</div>
<div class="item">
<div class="item-image">
<a href="">
<img class="img-fluid" src="">
<div class="item-badges"></div>
<div class="item-meta">
<div class="item-price">
<small></small>
</div>
</div>
</a>
</div>
<div class="item-info">
<h3 class="item-title"></h3>
<div class="item-location">
<i class="fa fa-map-marker "></i>
<p>Canada</p>
</div>
<div class="item-details-i">
<span class="bedrooms" title="Bedrooms">3 <i class="fa fa-bed"></i></span>
<span class="bathrooms" title="Bathrooms">2 <i class="fa fa-bath"></i></span>
<span class="bathrooms" title="Car Space">1 <i class="fa fa-car"></i></span>
</div>
<div class="item-details">
<p>Read More</p>
</div>
</div>
</div>
There are two 'item' divs, the difference is only 'item-location' div, containing p tag of location names. If user enters 'London', I want to show all 'item' divs which contain text 'london' and hide other 'item' divs. I tried the below code, but no luck. Any help would be appreciated. Thank you.
$("#location-filter").keyup(function() {
// Retrieve the input field text and reset the count to zero
var filter = $(this).val(),
count = 0;
// Loop through the div
$('.item > .item-location p').each(function() {
// If the list item does not contain the text phrase fade it out
if ($("this").text().search(new RegExp(filter, "i")) < 0) {
$(this).hide();
// Show the list item if the phrase matches and increase the count by 1
} else {
$(this).show();
count++;
}
});
});
I think the problem is with your CSS selector in the Jquery.
$('.item .item-location p')
should be right. This is because with the arrow, it is looking for an element with "item-location" directly under an element with the class "item" which it is not able to find.
Your code had some mistakes. The > is used to select the direct child. .item has no .item-location element as direct child. That is why, each iterator wasn't running at all. Besides that, the this in the if condition is wrapped inside double quotes, making it a string. And the last mistake was that, in the each loop, this refers to the element being iterated. To hide or show the .item, you've to use closest to reach the ancestor .item
$("#location-filter").keyup(function() {
var filter = $(this).val(),
count = 0;
$('.item .item-location p').each(function() {
if ($(this).text().search(new RegExp(filter, "i")) < 0) {
$(this).closest('.item').hide();
} else {
$(this).closest('.item').show();
count++;
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="location-filter" type="text">
<div class="item">
<div class="item-image">
<a href="">
<img class="img-fluid" src="">
<div class="item-badges"></div>
<div class="item-meta">
<div class="item-price">
<small></small>
</div>
</div>
</a>
</div>
<div class="item-info">
<h3 class="item-title">
</h3>
<div class="item-location">
<i class="fa fa-map-marker "></i>
<p>London</p>
</div>
<div class="item-details-i">
<span class="bedrooms" title="Bedrooms">3 <i class="fa fa-bed"></i></span>
<span class="bathrooms" title="Bathrooms">2 <i class="fa fa-bath"></i></span>
<span class="bathrooms" title="Car Space">1 <i class="fa fa-car"></i></span>
</div>
<div class="item-details">
<p>Read More</p>
</div>
</div>
</div>
<div class="item">
<div class="item-image">
<a href="">
<img class="img-fluid" src="">
<div class="item-badges"></div>
<div class="item-meta">
<div class="item-price">
<small></small>
</div>
</div>
</a>
</div>
<div class="item-info">
<h3 class="item-title">
</h3>
<div class="item-location">
<i class="fa fa-map-marker "></i>
<p>Canada</p>
</div>
<div class="item-details-i">
<span class="bedrooms" title="Bedrooms">3 <i class="fa fa-bed"></i></span>
<span class="bathrooms" title="Bathrooms">2 <i class="fa fa-bath"></i></span>
<span class="bathrooms" title="Car Space">1 <i class="fa fa-car"></i></span>
</div>
<div class="item-details">
<p>Read More</p>
</div>
</div>
</div>
Hello i want to select the first item in a list by default.So when i open the view i want the item Plafond sécurité sociale under Général to be selected by default.
$("#général").val($("#général a:first").selectedIndex());
<div class="col-12">
<div class="container">
<div class="row">
<div class="col-12">
<a class="mdc-list-item" style="width:100%;margin-right:3px;margin-left:3px;">
<div class="col-sm">
<div class="row" data-toggle="collapse" data-target="#général" style="cursor:pointer">
<span class="user" style="font-weight:bold">Général</span>
</div>
</div>
<div class="col-sm">
<div class="col-md-1 chevron-down" data-toggle="collapse" data-target="#général" style="float:right;margin-right:0px">
<i class="général_arrow material-icons float-right material-expand ripple" style="color: #0047FD !important;">
expand_more
</i>
</div>
</div>
</a>
</div>
</div>
</div>
<div id="général" class="collapse">
<div class="container">
<div class="row">
<div class="col-12">
<a class="mdc-list-item" tauxPlafonds="PSS" data-toggle="tooltip" data-placement="top" style="cursor:pointer;">
Plafond sécurité sociale
</a>
<a class="mdc-list-item" tauxPlafonds="SMIC" data-toggle="tooltip" data-placement="top" style="cursor:pointer;">
Smic
</a>
<a class="mdc-list-item" tauxPlafonds="CSG" data-toggle="tooltip" data-placement="top" style="cursor:pointer;">
CSG CRDS
</a>
<a class="mdc-list-item" tauxPlafonds="AGM" data-toggle="tooltip" data-placement="top" style="cursor:pointer">
Abattement gérant majoritaire
</a>
</div>
</div>
</div>
</div>
</div>
How can i add modify my function to have a good result.
Not really sure what you are asking for, but the select the item you want and add styling of your choice.
$('.mdc-list-item')[0]
$('.mdc-list-item:first-child()')[0]
$('.mdc-list-item:first')[0]
Not sure if this is what you are looking for.
$(document).ready(function() {
$("#item1").addClass("selected");
$(".list-item").click(function() {
$(".list-item").removeClass("selected");
$(this).addClass("selected");
});
});
a {
max-width: 100px;
display: block;
margin: 5px;
padding: 5px 10px;
text-align: center;
}
a,
a:hover {
color: inherit;
text-decoration: none;
}
a:hover {
background-color: #eee;
}
a.selected {
background-color: grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="item1" href="#" class="list-item">Item 1</a>
<a id="item2" href="#" class="list-item">Item 2</a>
<a id="item3" href="#" class="list-item">Item 3</a>
Okay I started with Bootstrap today and got some help to start getting one toggle working but I need 4 of them working properly. The problem as you see if you run the code only the first window reveals information, the second thing I need help with is to make so that went it works only one hidden text can be shown at a time, third thing I need help with is for the first hidden text to be visible on page load. * This is not a duplicate post the last solution i got worked fine but wasnt very responsive and it was quite laggy, decided to try Bootstrap and it looks very promising.
#leftpanel h1 {font-size: 18px; font-family: 'Montserrat bold'; color:#b0a887; border-top: 1px solid #b0a887; font-style: normal;}
#leftpanel h2 {font-size: 18px; font-family: 'Montserrat bold'; color:#b0a887; font-style: normal;}
#leftpanel a {margin-left: 230px;}
#demo { color: #333; font-size: 14px;}
.collapse {}
.testver { }
.testver hr {
}
.fa-angle-up {
display: none;
}
.testver i {
}
.testver .fa {
;
}
.arrow[aria-expanded="true"] .fa-angle-up {
display: inline-block;
}
.arrow[aria-expanded="true"] .fa-angle-down {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div id="leftpanel">
<div class="testver">
<h1>First title</h1>
<a href="javascript:void(0);" class="arrow" data-toggle="collapse" data-target="#demo">
<i class="fa fa-angle-down"><img src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-down-01-128.png" width="40" height="40"></i>
<i class="fa fa-angle-up"><img src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-up-01-128.png" width="40" height="40"></i>
</a>
<div id="demo" class="collapse">
First text
</div>
<hr>
</div>
<div class="testver">
<h2>Second title</h2>
<a href="javascript:void(0);" class="arrow" data-toggle="collapse" data-target="#demo">
<i class="fa fa-angle-down"><img src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-down-01-128.png" width="40" height="40"></i>
<i class="fa fa-angle-up"><img src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-up-01-128.png" width="40" height="40"></i>
</a>
<div id="demo" class="collapse">
Second text
</div>
<hr>
</div>
<div class="testver">
<h2>Third title</h2>
<a href="javascript:void(0);" class="arrow" data-toggle="collapse" data-target="#demo">
<i class="fa fa-angle-down"><img src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-down-01-128.png" width="40" height="40"></i>
<i class="fa fa-angle-up"><img src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-up-01-128.png" width="40" height="40"></i>
</a>
<div id="demo" class="collapse">
Third text
</div>
<hr>
</div>
<div class="testver">
<h2>Last title</h2>
<a href="javascript:void(0);" class="arrow" data-toggle="collapse" data-target="#demo">
<i class="fa fa-angle-down"><img src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-down-01-128.png" width="40" height="40"></i>
<i class="fa fa-angle-up"><img src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-up-01-128.png" width="40" height="40"></i>
</a>
<div id="demo" class="collapse">
Last Text
</div>
<hr>
</div>
</div>
The IDs must be unique: so change #demo to #demo1, #demo2, #demo3 and #demo4 just for instance.
After clicking a div it's necessary to hide all the remaining divs:
var currTarget = $(this).closest('a').data('target').substr(1);
$('#leftpanel').find('[id^="demo"]').not(currTarget).collapse('hide');
In order to set open the first div it's necessary to add the collapse in property plus aria-expamded like in:
<div id="demo1" class="collapse in" aria-expanded="true">
The snippet:
$('.testver a i').on('click', function(e) {
var currTarget = $(this).closest('a').data('target').substr(1);
$('#leftpanel').find('[id^="demo"]').not(currTarget).collapse('hide');
});
#leftpanel h1 {font-size: 18px; font-family: 'Montserrat bold'; color:#b0a887; border-top: 1px solid #b0a887; font-style: normal;}
#leftpanel h2 {font-size: 18px; font-family: 'Montserrat bold'; color:#b0a887; font-style: normal;}
#leftpanel a {margin-left: 230px;}
#demo { color: #333; font-size: 14px;}
.collapse {}
.testver { }
.testver hr {
}
.fa-angle-up {
display: none;
}
.testver i {
}
.testver .fa {
;
}
.arrow[aria-expanded="true"] .fa-angle-up {
display: inline-block;
}
.arrow[aria-expanded="true"] .fa-angle-down {
display: none;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div id="leftpanel">
<div class="testver">
<h1>First title</h1>
<a href="javascript:void(0);" class="arrow" data-toggle="collapse" data-target="#demo1" aria-expanded="true">
<i class="fa fa-angle-down"><img src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-down-01-128.png" width="40" height="40"></i>
<i class="fa fa-angle-up"><img src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-up-01-128.png" width="40" height="40"></i>
</a>
<div id="demo1" class="collapse in" aria-expanded="true">
First text
</div>
<hr>
</div>
<div class="testver">
<h2>Second title</h2>
<a href="javascript:void(0);" class="arrow" data-toggle="collapse" data-target="#demo2">
<i class="fa fa-angle-down"><img src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-down-01-128.png" width="40" height="40"></i>
<i class="fa fa-angle-up"><img src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-up-01-128.png" width="40" height="40"></i>
</a>
<div id="demo2" class="collapse">
Second text
</div>
<hr>
</div>
<div class="testver">
<h2>Third title</h2>
<a href="javascript:void(0);" class="arrow" data-toggle="collapse" data-target="#demo3">
<i class="fa fa-angle-down"><img src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-down-01-128.png" width="40" height="40"></i>
<i class="fa fa-angle-up"><img src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-up-01-128.png" width="40" height="40"></i>
</a>
<div id="demo3" class="collapse">
Third text
</div>
<hr>
</div>
<div class="testver">
<h2>Last title</h2>
<a href="javascript:void(0);" class="arrow" data-toggle="collapse" data-target="#demo4">
<i class="fa fa-angle-down"><img src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-down-01-128.png" width="40" height="40"></i>
<i class="fa fa-angle-up"><img src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-up-01-128.png" width="40" height="40"></i>
</a>
<div id="demo4" class="collapse">
Last Text
</div>
<hr>
</div>
</div>