Hide price when product has class - javascript - javascript

I have a category with many products. But only one product has specific class. I need to search this class and hide price only for this product.
This code hide prices of all products:
$(document).ready(function(){
if($('.flag').length > 0)
$('.prices').hide();
});
This is a sample code of website:
<div class="products">
<div class="product">
<div class="flag">
...
</div>
<div class="prices">
...
</div>
<div class="product">
<div class="flag2">
...
</div>
<div class="prices">
...
</div>
<div class="product">
<div class="flag3">
...
</div>
<div class="prices">
...
</div>
<div class="product">
<div class="flag4">
...
</div>
<div class="prices">
...
</div>
</div>
Thanks.

If I'm not mistaken $('.flag .prices').hide() should work without any additional checks. If you would need to do anything else with this container just use
$('.flag').each((_, containerEl) => {
//...your code where containerEl is your container element reference ...
})
But it won't work if you have some kind of lazy loading. It'll be a bit trickier.

I am guessing to a certain extend, but could it be that you want to hide the price for a product that has a flagx class assigned to it? (The following sample uses x=3).
If so, the following might help:
$(document).ready(function(){
$('.flag3').next('div').hide();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="products">
<div class="product">
<div class="flag">
flag
</div>
<div class="prices">
prices for flag
</div>
<div class="product">
<div class="flag2">
flag2 div
</div>
<div class="prices">
prices for flag2
</div>
<div class="product">
<div class="flag3">
flag3 div
</div><div class="prices">
prices for flag3
</div>
<div class="product">
<div class="flag4">
flag4
</div>
<div class="prices">
prices for flag4
</div>
</div>

Related

Reset Image Gallery Count When Loading New Article Content

I have a lots of articles that have 1, 2, 3 or 4 pictures. On mobile I created a carousel. All images all on the same line and I have a count that is 0. And when I press on the right button(for example) that count it will be -100 and all images will have a left: -100 and so on. The problem is that, let's say, I press on the button from one article that count will be -100. but after I go to another article and if I press again the count is not -100 and is -200. How can I reset that count when I change the article. The code is something like:
var c = 0;
$('.plus').on('click', function(){
c += 100
$(this).siblings('.num').text(c)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="article">
<div class="minus">Minu</div>
<div class="plus">Plus
</div><div class="num">0</div>
</div>
<div class="article">
<div class="minus">Minu2</div>
<div class="plus">Plus2
</div><div class="num">0</div>
</div>
<div class="article">
<div class="minus">Minu3</div>
<div class="plus">Plus3
</div><div class="num">0</div>
</div>
<div class="article">
<div class="minus">Minu4</div>
<div class="plus">Plus4
</div><div class="num">0</div>
</div>
<div class="article">
<div class="minus">Minu5</div>
<div class="plus">Plus5
</div><div class="num">0</div>
</div>
Here's what I cooked up for you.
$('.plus').on('click', function(){
c = Number($(this).siblings('.num').text()) + 100;
$(this).siblings('.num').text(c)
});
$('.minus').on('click', function(){
c = Number($(this).siblings('.num').text()) - 100;
$(this).siblings('.num').text(c)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="article">
<div class="minus">Minu</div>
<div class="plus">Plus</div>
<div class="num">0</div>
</div>
<div class="article">
<div class="minus">Minu2</div>
<div class="plus">Plus2</div>
<div class="num">0</div>
</div>
<div class="article">
<div class="minus">Minu3</div>
<div class="plus">Plus3</div>
<div class="num">0</div>
</div>
<div class="article">
<div class="minus">Minu4</div>
<div class="plus">Plus4</div>
<div class="num">0</div>
</div>
<div class="article">
<div class="minus">Minu5</div>
<div class="plus">Plus5</div>
<div class="num">0</div>
</div>
The Plus button will add to the total displayed for the current article and Minus will subtract from it. Every article has it's own c value that can't be changed by a button from a different article.
I hope that's what you are looking for.
Add an on('click', function() {...}) handler to your "change the article" button. If this button is just another article's text, then give them all a common class and a common class onclick handler.
HTML...
<span class="article">Article 1</span>
<span class="article">Article 2</span>
<span class="article">Article 3</span>
jQuery...
$('.article').on('click', function() {
c = 0;
});

jquery on click appear another element in html

for example I have this html code:
<div class="product">
<p>Name1<p>
</div>
<div class="product">
<p>Name2<p>
</div>
<div class="product">
<p>Name3<p>
</div>
<div class="product">
<p>Name4<p>
</div>
<div class="settings">
<p>SETTINGS<p>
</div>
I made settings class to display nothing in css, unless I click on one of 4 p elements in product class. After click the settings class appears at the bottom as it should be.
How should I make that if I click for example Name2 then settings class appears after Name2 and not at the bottom?
Use $(this) to target the element you clicked on, combined with insertAfter() to add the content to this element.
Run the code snippet below and click on any of the elements with the classname product to see how this works.
$(".product").click(function(){
$(".settings").insertAfter($(this));
});
$(".product").click(function(){
$(".settings").insertAfter($(this));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="product">
<p>Name1
<p>
</div>
<div class="product">
<p>Name2
<p>
</div>
<div class="product">
<p>Name3
<p>
</div>
<div class="product">
<p>Name4
<p>
</div>
<div class="settings">
<p>SETTINGS
<p>
</div>
You can use .insertAfter() :
$(function(){
$('.product p').click(function(){
$('.settings').insertAfter($(this).closest('.product '))
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="product">
<p>Name1<p>
</div>
<div class="product">
<p>Name2<p>
</div>
<div class="product">
<p>Name3<p>
</div>
<div class="product">
<p>Name4<p>
</div>
<div class="settings">
<p>SETTINGS<p>
</div>
You can do it like,
$(".product > p").click(function(){
var $parent = $(this).parent(".product");
$parent.siblings(".settings").insertAfter($parent);
});
by using .insertAfter()
DEMO

How to get the id of each first element of a nexted div in JQuery

here is my HTML code
<div id="parent">
<div id="computer">
<div id="items">
<div id="1">hp</div>
<div id="2">compaq</div>
<div id="3">toshiba</div>
</div>
</div>
<div id="laptop">
<div id="1">i5</div>
<div id="2">dual core</div>
<div id="3">i3</div>
</div>
<div id="printer">
<div id="laser">hp laser</div>
</div>
<div id="cpu">
<div id="item">
<div id="1">pintuim</div>
<div id="2">celeron</div>
</div>
</div>
<div id="scanner">
<div id="canon">
<div>canon</div>
</div>
</div>
</div>`
I am trying to get the id of each first element of the parent div
that is to get an
array=[computer, laptop, printer, cpu, scanner]
here is my jquery code but it's taking everything.
var a = $("div.content :nth-child(1)")find("*").toArray();
please I need some help thanks
there is a selector in jQuery which will return the first level of children: DEMO
var a=[];
$("#parent > div").each(function(){ // using > will return the first level of children
a.push($(this).attr('id'));
});
alert(a);
Those are children of the #parent element so you can iterate over that and create an array out of it using .map()
var array = $('#parent').children().map(function() {
return this.id;
}).get();
snippet.log(array)
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="parent">
<div id="computer">
<div id="items">
<div id="1">hp</div>
<div id="2">compaq</div>
<div id="3">toshiba</div>
</div>
</div>
<div id="laptop">
<div id="1">i5</div>
<div id="2">dual core</div>
<div id="3">i3</div>
</div>
<div id="printer">
<div id="laser">hp laser</div>
</div>
<div id="cpu">
<div id="item">
<div id="1">pintuim</div>
<div id="2">celeron</div>
</div>
</div>
<div id="scanner">
<div id="canon">
<div>canon</div>
</div>
</div>
</div>

How to wrap all next elements until?

I have following html:
<div class="menuItem">Domů</div>
<div class="menuItem">O nás</div>
<div class="menuItem">Výzkum a vývoj</div>
<div class="submenuItem"><b>Aplikace aktivního gumového prášku</b>
</div>
<div class="submenuItem"><b>Odprašky</b>
</div>
<div class="submenuItem"><b>Guma</b>
</div>
<div class="submenuItem"><b>Zemědělství</b>
</div>
<div class="submenuItem"><b>Potravinářství</b>
</div>
<div class="menuItem">Projekční činnost</div>
<div class="menuItem">Realizace</div>
<div class="submenuItem"><b>realizace podstránka</b>
</div>
<div class="menuItem">Kontakty</div>
What I am trying to achieve, is wrap each .menuItem and all next .submenuItem with .menuSet. Unhappily, the following js wraps the whole snippet instead of the set defined above.
<div class="menuItem">Domů</div>
<div class="menuItem">O nás</div>
<div class="menuItem">Výzkum a vývoj</div>
<div class="submenuItem"><b>Aplikace aktivního gumového prášku</b>
</div>
<div class="submenuItem"><b>Odprašky</b>
</div>
<div class="submenuItem"><b>Guma</b>
</div>
<div class="submenuItem"><b>Zemědělství</b>
</div>
<div class="submenuItem"><b>Potravinářství</b>
</div>
<div class="menuItem">Projekční činnost</div>
<div class="menuItem">Realizace</div>
<div class="submenuItem"><b>realizace podstránka</b>
</div>
<div class="menuItem">Kontakty</div>
For make my intention more understandable, below I post the desired result:
$(".menuItem").nextUntil(".menuItem").andSelf().wrapAll("<div class='menuSet'></div>");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="menuSet">
<div class="menuItem">Domů</div>
</div>
<div class="menuSet">
<div class="menuItem">O nás</div>
</div>
<div class="menuSet">
<div class="menuItem">Výzkum a vývoj</div>
<div class="submenuItem"><b>Aplikace aktivního gumového prášku</b>
</div>
<div class="submenuItem"><b>Odprašky</b>
</div>
<div class="submenuItem"><b>Guma</b>
</div>
<div class="submenuItem"><b>Zemědělství</b>
</div>
<div class="submenuItem"><b>Potravinářství</b>
</div>
</div>
<div class="menuSet">
<div class="menuItem">Projekční činnost</div>
</div>
<div class="menuSet">
<div class="menuItem">Realizace</div>
<div class="submenuItem"><b>realizace podstránka</b>
</div>
</div>
<div class="menuSet">
<div class="menuItem">Kontakty</div>
</div>
As you are selecting all the menuItem's with class and than filtering from there, wrapAll will wrap your whole collection.
To solve this, one way would to be use $.each() and iterate through the menuItems and than wrap them.
$(".menuItem").each(function (index) {
$(this).nextUntil(".menuItem").andSelf().wrapAll("<div class='menuSet'></div>");
});
JSFiddle Demo.

How to change the div with repect to the menu selection

I have a vertical menu.When I click on the first menu i want to show the corresponding div on right side.Two div is possible through it.But cannot do others.How can I do this?
script is
$(function() {
$(".box").hide();
$("#master").click(function(){
$("#leftpanel").show();
$(".box1").show();
});
$("#country").click(function(){
$(".box").hide();
$(".box1").show();
});
$("#currency").click(function(){
$(".box").hide();
$(".box2").show();
});
$("#city").click(function(){
$(".box").hide();
$(".box3").show();
});
$("#EmissionsCountry").click(function(){
$(".box4").show();
$(".box").hide();
});
$("#EmissionsFuel").click(function(){
$(".box5").show();
$(".box").hide();
});
$("#IfcIndustrySector").click(function(){
$(".box6").show();
$(".box").hide();
});
$("#ReTechnologyType").click(function(){
$(".box7").show();
$(".box").hide();
});
$("#Unit").click(function(){
$(".box8").show();
$(".box").hide();
});
$("#Energy").click(function(){
$(".box9").show();
$(".box").hide();
});
});
Html is
<div id="container">
<div id="leftpanel">
<div id="menu1">
<div>
Country
</div>
<div class="imagediv">
<div class="image"><img src="arrow.gif"/></div>
</div>
</div>
<div id="menu1">
<div>
Currency
</div>
<div class="imagediv">
<div class="image"><img src="arrow.gif"/></div>
</div>
</div>
<div id="menu1">
<div>
City
</div>
<div class="imagediv">
<div class="image"><img src="arrow.gif"/></div>
</div>
</div>
<div id="menu1">
<div>
EmissionsCountry
</div>
<div class="imagediv">
<div class="image"><img src="arrow.gif"/></div>
</div>
</div>
<div id="menu1">
<div>
EmissionsFuel
</div>
<div class="imagediv">
<div class="image"><img src="arrow.gif"/></div>
</div>
</div>
<div id="menu1">
<div>
IfcIndustrySector
</div>
<div class="imagediv">
<div class="image"><img src="arrow.gif"/></div>
</div>
</div>
<div id="menu1">
<div>
ReTechnologyType
</div>
<div class="imagediv">
<div class="image"><img src="arrow.gif"/></div>
</div>
</div>
<div id="menu1">
<div>
Unit
</div>
<div class="imagediv">
<div class="image"><img src="arrow.gif"/></div>
</div>
</div>
<div id="menu1">
<div>
Energy
</div>
<div class="imagediv">
<div class="image"><img src="arrow.gif"/></div>
</div>
</div>
</div>
<div id="rightpanel">
<div class="box1 box" id="country">
country
</div>
<div class="box2 box" id="currency">
currency
</div>
<div class="box3 box" id="city">
city
</div>
<div class="box4 box" id="EmissionsCountry">
EmissionsCountry
</div>
<div class="box5 box" id="EmissionsFuel">
EmissionsFuel
</div>
<div class="box6 box" id="IfcIndustrySector">
IfcIndustrySector
</div>
<div class="box7 box" id="ReTechnologyType">
ReTechnologyType
</div>
<div class="box8 box" id="Unit">
Unit
</div>
<div class="box9 box" id="Energy">
Energy
</div>
</div>
</div>
You can see the demo from FIDDLE
I changed your code a little bit and added data-box so you know which .box you show.
FIDDLE
$(function () {
$(".box").hide();
$('.menu1').click(function () {
var boxid = $(this).data('box');
$('.box').hide();
$('.box' + boxid).show();
});
});
I think this will simplify your script a lot and make it more easier to change.
The actuall problem in you HTML is that you don't have IDs assigned to <a>. You only have IDs for the first 2 menu elements and in your script you use
.show(); // this will show the box you need
.hide(); // but then you hide all the boxes
//(even the one you need because it has the class .box)
//Instead use
.hide(); // hide all the boxes
.show(); // show the one you need
FIDDLE with your code <-- added IDs and corrected script show/hide
If youre going to do it this way(by giving divs multiple class names) you should have your .hide functions ALWAYS first in the javascript above, and your .show ALWAYS second. Whats happening is you're showing all elements with a class name of box4, but then hiding all the elements with a class name of box, which includes box4 and the other later divs, so you show it, but then make it hide RIGHT after.
Hope this helps

Categories

Resources