Looking to add a current state for the external links.
Divi Slider control #1
Divi Slider control #2
Divi Slider control #3
Divi Slider control #4
<script>
jQuery(document).ready(function( $ ) {
$("#DTE-slide-1").on("click", function(e) {
e.preventDefault();
$(".et-pb-controllers a:nth-child(1)").trigger("click");
});
$("#DTE-slide-2").on("click", function(e) {
e.preventDefault();
$(".et-pb-controllers a:nth-child(2)").trigger("click");
});
$("#DTE-slide-3").on("click", function(e) {
e.preventDefault();
$(".et-pb-controllers a:nth-child(3)").trigger("click");
});
$("#DTE-slide-4").on("click", function(e) {
e.preventDefault();
$(".et-pb-controllers a:nth-child(4)").trigger("click");
});
});
</script>
Ideally, I would like to add a CSS class to the current link so I can change its color. Assume i can do this within the jquery, but not sure how! Thanks.
Simple update you javaScript with mine. We have to add a active class on the click element and remove from the other.
var selector = '.links a';
$(selector).on('click', function(){
$(selector).removeClass('active');
$(this).addClass('active');
});
.active {
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="links">
Divi Slider control #1
Divi Slider control #2
Divi Slider control #3
Divi Slider control #4
</div>
Use this:
$('a').css({color: '#ffe6e6'});
OR
$('a').addClass('someClass');
.someClass {
color:#ffe6e6
}
I'm assuming .et-pb-controllers a are your external links as the #DTE-slide-X links are internal (as you're triggering them via jQuery). To set a current CSS class to your external links, do this:
<script>
jQuery(document).ready(function( $ ) {
var $allLinks = $('.et-pb-controllers a');
$("#DTE-slide-1").on("click", function(e) {
e.preventDefault();
$allLinks.removeClass('current-link');
$(".et-pb-controllers a:nth-child(1)").addClass('current-link').trigger("click");
});
$("#DTE-slide-2").on("click", function(e) {
e.preventDefault();
$allLinks.removeClass('current-link');
$(".et-pb-controllers a:nth-child(2)").addClass('current-link').trigger("click");
});
$("#DTE-slide-3").on("click", function(e) {
e.preventDefault();
$allLinks.removeClass('current-link');
$(".et-pb-controllers a:nth-child(3)").addClass('current-link').trigger("click");
});
$("#DTE-slide-4").on("click", function(e) {
e.preventDefault();
$allLinks.removeClass('current-link');
$(".et-pb-controllers a:nth-child(4)").addClass('current-link').trigger("click");
});
});
</script>
And your current link CSS is .current-link, which you can style accordingly.
If this solves your question, please accept it as the answer :)
Related
I have made a nav that opens when you click at the .products link and closes if you click anywhere on the screen. But it seems that if you click on the .product link when it is already open, it will not close. How can I do this?
$('#subnav').hide();
$(document).on('click', function(e) {
if ( $(e.target).closest('.products').length ) {
$("#subnav").show();
}else if ( ! $(e.target).closest('#subnav').length ) {
$('#subnav').hide();
}
});
Demo
js
$("#subnav").hide();
$(".products").click(function (e) { //the usual behavior on click on .prducts
$("#subnav").toggle();
e.stopPropagation();
});
$("#subnav").click(function (e) { //ensures the #subnav will not hide on click on it
e.stopPropagation();
});
$(document).click(function () { //ensures the #subnav will hide on click on document
$("#subnav").hide();
});
You need a click event on the document to hide the block and a click event on the button to show the block. And in the event of the button, you need to stop the propagation of the document's event.
Like that :
$("div").hide();
$("button").bind("click",function(e){
e.stopPropagation();
$("div").toggle(200);
});
$(document).bind("click",function(){
$("div").hide(200);
});
Assuming your code looks like that :
<div></div>
<button>open</button>
See example : http://jsfiddle.net/oqgpceod/1/
Aditionnaly you may add this code to prevent the block from hiding when you click on it :
$("div").bind("click",function(e){
e.stopPropagation();
});
Fiddle: http://jsfiddle.net/r2h57jhu/
$(document).ready(function () {
$('#subnav').hide();
$(document).click(function(e) {
$('#subnav:visible').hide();
});
$('.products').click( function (e) {
$('#subnav:not(:visible)').show();
e.stopPropagation();
});
});
I'm trying to add/remove .css('overflow-y','hidden') onclick, Which I did. The problem appears when I try to remove that css, also onclick. But this time, user needs to click on another element.
Idea is to have modal (twitter bootstrap 2.3) window where there is some data and when user click on modal button (triggers) the css applies to html element in order to prevent scrolling of the website. And now when I click anywhere on modal (modal shuts down) but there is still overflow-y styling and because of it I can't scroll my page.
So this is what I've made, but I have been stuck here and don't know where I am making mistake. Could anyone help me with this one, and if is possible give me some advice so I could take care in future.
Thanks!
<script type="text/javascript">
$('#myModal').modal('hide') // initializes and invokes show immediately</p>
$('.note').delay(10000).fadeOut('slow');
$(document).ready(function() {
var $html = $('html');
var $button = $('.container > .btn-primary');
var $modal = $('.modal-backdrop');
$button.on('click', function(e) {
$html.css('overflow-y', 'hidden');
if ($html.attr('style')) {
alert('WORKS!');
}
else {
$modal.onclick( function() {
$html.css('overflow-y','scroll');
});
};
});
});
</script>
Put your css in a class and use jquery's .toggleClass() to show/hide the overflow.
Here's a simplified example: http://jsbin.com/towiyaqa/1/
You can use like this:
$button.on('click', function(e) {
$html.css('overflow-y','hidden' ? 'scroll' : 'hidden');
e.preventDefault();
})
Here is solution for problem:
$(document).ready(function() {
var $html = $('html');
var $button = $('.container > .btn-primary');
var $modal = $('.modal-backdrop');
$button.on('click', function(e) {
$('.note').delay(10000).fadeOut('slow');
$html.css('overflow-y', 'hidden');
if ($html.attr('style')) {
console.log("overflow-y: hidden added");
}
});
$('#myModal').on('hidden.bs.modal', function () {
// do something…
console.log("fires myModal");
$html.css('overflow-y','scroll');
});
});
</script>
I have a <ul> drop down list and all works fine but I'm wondering if it's possible to make the list "retract", so essentially disappear back to it's original state when you click off away from it?
FIDDLE
Here is the code that I acquired to make the drop down work. I'm no good with JS so I don't know what I would need to do or if it's possible to get what I'm after.
$(function(){
$('#select').click(function(){
$('#sel-option').show();
});
$('#sel-option a').click(function(e){
$('#select').text($(this).text());
$('#sel-option').hide();
$(this).addClass('current');
e.preventDefault();
})
})
yes, you just have to handle body click event and prevent select click from bubbling (in other case you will not be able to open it)
$(function(){
$('#select').click(function(event){
$('#sel-option').show();
event.stopPropagation();
});
$('#sel-option a').click(function(e){
$('#select').text($(this).text());
$('#sel-option').hide();
$(this).addClass('current');
e.preventDefault();
})
$('body').on('click', function(){
$('#sel-option').hide();
});
})
UPDATED: and working fiddle: http://jsfiddle.net/Tpf7E/239/
try this
$(function(){
$(document).click(function (e)
{
var container = $(".language");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
$('#sel-option').hide();
show = 0;
}
});
var show=0;
$('#select').click(function(){
if(show==0)
{
$('#sel-option').show();
show = 1;
}
else
{
$('#sel-option').hide();
show = 0;
}
});
$('#sel-option a').click(function(e){
$('#select').text($(this).text());
$('#sel-option').hide();
$(this).addClass('current');
e.preventDefault();
});
});
see FIDDLE
CODE:
<script type="text/javascript">
$(document).ready(function() {
$('.show_or_hide_link_clicker').click(function() {
$(".the_box_to_show").fadeIn(400);
});
});
</script>
When show_or_hide_link_clicker is clicked the_box_to_show is shown. How do I hide it if show_or_hide_link_clicker is clicked again or when the user clicks away?
Update: This is what I am doing now: http://jsfiddle.net/nFbnr/
Question: How can i remove the double click requirement ot show the div?
jQuery Toggle is what you're looking for.
$('.the_box_to_show').toggle();
When clicking anywhere, check if the element was on the propagation path. If not, the user clicked outside of it so you can hide it.
$(document).click(function(e) {
if ($(e.target).closest(".the_box_to_show").size() === 0) {
$(".the_box_to_show").hide();
}
});
http://jsfiddle.net/vdHAu/
$(document).click(function(e) {
if (!$(e.target).is(".the_box_to_show")) {
$(".the_box_to_show").hide();
}
});
$('.show_or_hide_link_clicker').click(function() {
$(this).hide();
$(".the_box_to_show").fadeIn(400);
});
An another way without delegate event to document level:
you have to set attribute tabindex to the box and CSS outline for style
http://jsfiddle.net/GV56b/
$(document).ready(function () {
$('.show_or_hide_link_clicker').click(function () {
$(this).hide();
$(".the_box_to_show").fadeIn(400, function () {
this.focus()
});
});
$(".the_box_to_show").on('blur',function(){
$(this).hide();
$('.show_or_hide_link_clicker').fadeIn(400);
});
});
check this out
$('.show_or_hide_link_clicker').click(function() {
$(this).hide();
$(this).addClass('active);
$(".the_box_to_show").fadeIn(400);
});
$(document).on('click', 'html', function(){
$(".the_box_to_show").fadeOut(400);
});
$(document).on('click', '.active', function(e){
e.stopPropagation();
});
So I have this simple fiddle with 3 lists that expand by setting each <li> a class of active. Is it possible I can make it slide or toggle after I set it's class to active?
Take a look at here: http://jsfiddle.net/QeyPj/
Here you go
http://jsfiddle.net/QeyPj/7/
Code
$('.title').live('click', function(e)
{
e.preventDefault();
if($(this).closest('li').hasClass('active'))
return;
var $this = $(this);
$this.closest('ul').find('li.active')
.find("div.description").slideUp(200, function(){
$this.closest('ul').find('li.active').removeClass('active');
$this.closest('li')
.find("div.description").slideDown(200, function(){
$(this).closest('li').addClass('active');
});
});
});