Here is the situation. I have a box which is a div which contains another div which is basically a button. When the outer box is clicked it should redirect to a product details page. When the button within the box is clicked it should perform an ajax call which adds that particular item to cart.
Currently, when the outer box is clicked it redirects to product details page which is the desired functionality. However, when the inner button is clicked it adds to cart AND redirects while I do NOT want it to redirect but just add to cart. How do I achieve this desired functionality?
Here is the code for asp.net
#using LaptopMart.Models
#model IEnumerable<LaptopMart.Models.Product>
#{
ViewBag.Title = "User Index";
}
<h2>title</h2>
#foreach (Product product in Model)
{
<div class="inline">
<div class="A" div-product-id ="#product.Id">
<div class="card">
<div class="card-head">
<img src="~/Content/ProductImages/#product.Image" alt="product-image" class="card-product-image">
</div>
<div class="card-body">
<div class="product-details">
<span class="product-title">#product.Name</span>
<span class="product-price">$<b>#product.Price</b></span>
</div>
<div>
<div class="btn-sm btn-primary js-add-to-cart" data-product-id="#product.Id" >Add to Cart</div>
</div>
</div>
</div>
</div>
</div>
}
#section scripts
{
<script>
$(document).ready(function () {
$(".A").on("click", function () {
if ($(this).hasClass("js-add-to-cart")) {
return;
}
else {
window.location.href = "/user/ProductDetails/" + $(this).attr("div-product-id");
}
});
$(".js-add-to-cart").on("click",
function () {
var button = $(this);
$.ajax({
url: "/api/user/AddToCart/" + button.attr("data-product-id"),
method: "GET",
success: function () {
window.setTimeout(function () {
bootbox.hideAll();
}, 1000);
bootbox.alert("This has been added to cart");
}
});
});
});
</script>
}
In the function handling the click event for the inner div, stop the event from propagating to the outer div by calling event.stopPropagation() on the event:
function clickHandler(event) {
event.stopPropagation();
// Do your other stuff like Ajax calls
}
Related
I am trying to hide the div if you click only on the header. But my filter does not seem to work. I get the intended function wherever I click on the div. I want to restrict this to only when you click on the header.
<div class="post" onclick="updatenext()">
<h2>Item3</h2>
</div>
<div class="post" onclick="updatenext()">
<h2>Item4</h2>
</div>
<script>
var index=0;
$(".post").hide();
$(".post").eq(0).show();
// Tried this too: $(".post").filter(":header")....
$(":header.post").on("click",
function () {
index=$(this).index();
//console.log($(this).index());
$(this).hide();
$(".post").eq(index).show();
}
);
</script>
I expect the click to work only when clicking on the header element within each div.
Try using only jQuery for the event listener, like this:
<div class="post">
<h2 onclick="updatenext()">Item3</h2>
</div>
<div class="post">
<h2 onclick="updatenext()">Item4</h2>
</div>
<script>
var index = 0;
$(".post").hide();
$(".post").eq(0).show();
$("h2").on("click", function () {
index = $(this).parent().index();
$(this).parent().hide();
$(".post").eq(index).show();
});
</script>
I'm having trouble adding a link to a button inside a card. the card is wrapped in a div element which when clicked automatically expands and closes.
Is there anyway to remove this functionality just when clicking on the details button? So it acts as a normal link?
Here is the code:
https://codepen.io/candroo/pen/wKEwRL
Card HTML:
<div class="card">
<div class="card__image-holder">
<img
class="card__image"
src="https://source.unsplash.com/300x225/?wave"
alt="wave"
/>
</div>
<div class="card-title">
<a href="#" class="toggle-info btn">
<span class="left"></span>
<span class="right"></span>
</a>
<h2>
Card title
<small>Image from unsplash.com</small>
</h2>
</div>
<div class="card-flap flap1">
<div class="card-description">
This grid is an attempt to make something nice that works on touch
devices. Ignoring hover states when they're not available etc.
</div>
<div class="card-flap flap2">
<div class="card-actions">
Read more
</div>
</div>
</div>
</div>
JS:
$(document).ready(function () {
var zindex = 10;
$("div.card").click(function (e) {
e.preventDefault();
var isShowing = false;
if ($(this).hasClass("show")) {
isShowing = true;
}
if ($("div.cards").hasClass("showing")) {
// a card is already in view
$("div.card.show").removeClass("show");
if (isShowing) {
// this card was showing - reset the grid
$("div.cards").removeClass("showing");
} else {
// this card isn't showing - get in with it
$(this).css({ zIndex: zindex }).addClass("show");
}
zindex++;
} else {
// no cards in view
$("div.cards").addClass("showing");
$(this).css({ zIndex: zindex }).addClass("show");
zindex++;
}
});
});
You can check the target of the event and see if it is an <a> using is()
Something like:
$("div.card").click(function (e) {
// only run when not an `<a>`
if(!$(e.target).is('a')){
e.preventDefault();
//the rest of your code
....
}
});
I don't know Jquery but with javascript you can do this inside your code:
const links = document.querySelectorAll('.btn');
links.forEach(link => link.addEventListener('click', (e)=>e.stopPropagation()))
I'm trying to get an if else statement that will allow me to run a function if the #selectedwhip div has the .active class. If it doesn't, I want it to run another function. I've just used the console to see if it will work but it only returns the else statement in the console.
The scenario is 3 images which act as links. By clicking the links they will add an active class to another div. By being active, an image will be inserted into the div. Clicking an image does add an active class to the div but it doesn't cause the if statement to log to the console.
Is there something that's preventing it from working?
jQuery
$(".carbutton").click(function(){
$("#selectedwhip").addClass("active");
});
$(function() {
if ($("#selectedwhip").hasClass("active")) {
console.log('active');
}
else {
console.log('unactive');
}
});
HTML
<div class="cars">
<h1>Title</h1>
<div class="section group trio-cars">
<div class="col span_1_of_3 carbutton" id="carbutton">
<a id="car1"><img src="img/frontleftbmw.jpg" width="100%"></a>
</div>
<div class="col span_1_of_3 carbutton" id="carbutton">
<a id="car2"><img src="http://placehold.it/350x150" width="100%"></a>
</div>
<div class="col span_1_of_3 carbutton" id="carbutton">
<a id="car3"><img src="http://placehold.it/350x150" width="100%"></a>
</div>
</div>
</div>
<div class="col span_1_of_3">
<div class="stats-image" id="selectedwhip">
</div>
</div>
you have to have a check function such as:
function check() {
if ($("#selectedwhip").hasClass("active")) {
console.log('active');
} else {
console.log('unactive');
}
}
$(function() {
$(".carbutton").click(function() {
$("#selectedwhip").addClass("active");
check();
});
});
Just noticed you have duplicate IDs carbutton and that makes it as a invalid markup creation. Every ID should have to be unique.
you have to be clear what do you want to do and when. because you're just executing a function immediately the page is ready
$(function() {
if ($("#selectedwhip").hasClass("active")) {
console.log('active');
}
else {
console.log('unactive');
}
});
the above code will check if selectedwhip it has active class in the first load of the page.
you have to attach this conditional to an event
I have a button that toggles the playlistHolder div. In addition, when '.albumImage a' is selected, it also unhides the playlist. I am trying to change the text of the button to 'Show Playlist' if the playlist is hidden and 'Hide Playlist' if it is not hidden. I have tried click functions with if(('.playlist').is(:hidden)) but have not had any luck with getting it to work. I am new to Jquery so I understand that my code below can use much improvement, here is my setup now that works when the album image is selected as well as when the toggle button is selected without changing the text:
HTML
<div class="togglePlaylist">
<button id="togglePlaylistBT">Show/Hide Playlist</button>
<!-- POPUP LAUNCHER -->
</div>
<div class="playlistHolder">
<div class="componentPlaylist">
<div class="playlist_inner">
<!-- playlist items are appended here! -->
</div>
</div>
<!-- preloader -->
<div class="preloader"></div>
</div>
<div class="albumWrapper">
<div class="albumCovers">
<div class="albumImage"> <img class="img-responsive" src="media/music/Album_Covers/Swag_Brothers.jpg" alt="thumb" />
<p class="nowPlaying">Now Playing</p>
</div>
</div>
</div>
Javascript/JQuery
$('#togglePlaylistBT, .albumImage a').on('click', function() {
var $this = $('#togglePlaylistBT');
if($('.playlistHolder').is(':visible'))
{
$('.playlistHolder').hide('slow');
$this.text('Hide Playlist');
}
else
{
$('.playlistHolder').show('slow');
$this.text('Show Playlist');
}
});
Try this snippet of code:
$('.togglePlaylist button').on('click', function() {
var $this = $(this);
if($('.playlistHolder').is(':visible'))
{
$('.playlistHolder').hide();
$this.text('Hide Playlist');
}
else
{
$('.playlistHolder').show();
$this.text('Show Playlist');
}
});
Working JSFiddle
When you hide and show the playlist just run document.getElementById("togglePlaylistBT").value="Show Playlist";
For example:
function change()
{
var elem = document.getElementById("playlistButton");
if (elem.value=="Open Playlist") { elem.value = "Close Playlist"; closeBox(); }
else { elem.value = "Open Playlist"; openBox(); }
}
function openBox() {
alert("open");
}
function closeBox() {
alert("closed");
}
<input onclick="change()" type="button" value="Open Playlist" id="playlistButton"></input>
Make sure to change openBox() and closeBox() functions to your box code!
I have a dropdown menu activated on click.
I use toggle to activate it when you click on the .hello_panel
HTML
<div class="container">
<div class="login_panel">
<div class="hello_panel">
<div class="hello_label">Hello </div>
<div class="hello_value">foofoo</div>
</div>
</div>
</div>
jQuery
$('.hello_panel').bind('click', function(){
$('.menu_popup').toggle();
})
if I click it it works fine, it does the show and hide effect when the .hello_panel
is clicked.
what I want is it to be shown if the .hello_panel is clicked and hidden back if when clicking anything else on the page except the .menu_popup
You can hide it whenever you click on the document
JavaScript
$(document).click(function () {
$('.menu_popup:visible').hide();
});
$('.hello_panel').bind('click', function (e) {
$('.menu_popup').toggle();
e.stopPropagation();
});
HTML
<div class="container">
<div class="login_panel">
<div class="hello_panel">
<div class="hello_label">Hello</div>
<div class="hello_value">foofoo</div>
</div>
</div>
</div>
<div style="display:none" class="menu_popup">menu_popup</div>
Demo
http://jsfiddle.net/6bo1rjrt/16/
Another way if you don't want to stopPropagation is passing a call back function that registers a once time click listener to that document to hide the menu
$('.hello_panel').bind('click', function () {
$('.menu_popup').show(function () {
$(document).one('click', function () {
$('.menu_popup:visible').hide();
});
});
});
Demo
http://jsfiddle.net/6bo1rjrt/17/