I am showing a hidden menu when user clicks on an arrow (.userArrow) and I hide it when user clicks outside the container (.menuTrigger)
<div class="box">
<div class="menuTrigger fRight">
<span class="userArrow ">filtrar ▾</span>
<ul class="userOptionsUl dropdownMenu" style="display: none;">
<li><input type="text"></li>
</ul>
</div>
</div>
Javascript
$(function() {
/* el dropdown del dock*/
$('.menuTrigger .userArrow').click(function(e) {
e.preventDefault();
$(this).parent().find('.dropdownMenu').show();
e.stopPropagation();
});
$('body').not('.menuTrigger').click(function() { /* cambiar por la otra clase que tiene*/
$('.dropdownMenu').hide();
console.log('yo lo he escondido');
});
});
http://jsfiddle.net/5VQXg/
My problem here is that when user clicks to show the hidden menu and then clicks to type into the input. The menu is hidden.
The thing that all clicks are being inside .menuTrigger (or childs)
Question is, why $('body').not('.menuTrigger').click() when all clicks are being in childs of this container?
What your code is doing is filtering out any body element with a class of menuTrigger, which doesn't exist.
What you actually need to do is check if the element that was clicked (e.target) is inside of menuTrigger and then don't run the code if it is:
$('body').click(function(e) { /* cambiar por la otra clase que tiene*/
// If the clicked element is not inside menuTrigger div hide the menu
if (!$(e.target).closest('.menuTrigger').length) {
$('.dropdownMenu').hide();
console.log('yo lo he escondido');
}
});
http://jsfiddle.net/5VQXg/3/
Related
I have try to change the pront confirmation of dropzone with a modal confirmation from bootstrap with this code:
Modal:
<div class="modal inmodal fade" id="RemoveFileModal" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content animated bounceInRight">
<div class="modal-header errormdg">
<button type="button" class="close" data-dismiss="modal"><i class="fas fa-window-close text-white"></i><span class="sr-only">Close</span></button>
<h4 class="modal-title">Cuidado: Estas Intentando Remover un Archivo.</h4>
</div>
<div class="modal-body">
<p>
Por algún motivo el sistema detecto que intentaste Remover un Archivo, Ten en cuenta que el archivo removido no podrás subirlo al sistema.<br> Este aviso es para que tengas en cuenta que si aceptas el archivo que remueves no será guardado en la Aplicación.<br><br>
Presiona Aceptar para Remover el archivo y Salir de la pantalla.<br>
Presiona Cancelar para Regresar.<br>
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-cls-error" id="outAceptar" data-dismiss="modal">Aceptar</button>
<button type="button" class="btn btn-white" data-dismiss="modal">Cancelar</button>
</div>
</div>
</div>
</div>
and use this Script for dropzone to catch the remove file event and display the modal:
$('div.dropzone').each(function() {
$(this).dropzone({
autoProcessQueue: true,
url: "/",
maxFilesize: window.SIS.uploadFiles.size,
maxFiles: window.SIS.uploadFiles.max,
acceptedFiles: 'application/pdf, image/jpeg, image/jpg, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
dictDefaultMessage: "<strong>Coloque los archivos Aqui. </strong></br> (Peso Maximo 2Mb, Se Aceptan los Siguientes Formatos: jpg, pdf, xls, xlsx.)",
addRemoveLinks: true,
dictCancelUpload: 'Cancelar Subida',
dictRemoveFile: 'Quitar Archivo',
clickable: true,
init: function() {
this.on("removedfile", function(data) {
var IsRemove = false;
$('#RemoveFileModal').modal('toggle').on('click', '#outAceptar', function(e) {
IsRemove = true;
});
return IsRemove;
});
},
});
});
the Main problem is that toogle modal not stop the remove file event to catch if user click "aceptar" or "cancelar". spanish option of accept and cancel.
the script show the modal but continue remove file in background.
Update
i have try with this but not work:
removedfile: function(file) {
$('#RemoveFileModal').modal('toggle').on('click', '#outAceptar', function(e) {
//myDropzone.removeFile(file);
return true;
});
$('#RemoveFileModal').modal('toggle').on('click', '#outCancelar', function(e) {
return false;
});
},
Using the removed file event is a good way:
// Called whenever a file is removed.
removedfile: function removedfile(file) {
$('#yourModalId').modal({
backdrop: 'static',
keyboard: false
})
$('#yourModalDeleteButton').click({file: file, self: this}, removeFile);
function removeFile(event)
{
$('#yourModalId').modal('hide');
// user confirmed , remove your file
}
},
As u can see I am passing the file object and this to my function because you will need atleast file object to remove the file. (Maybe not this it depends on how you handle file removal).
can anyone make these functions simple?
i have a ul:
<ul class="phone-type">
<li class="office" id="1"></li>
<li class="mobile" id="2"></li>
<li class="fax" id="3"></li>
</ul>
and the JS :
var o = 0;var m = 0;var f = 0;
$('ul.phone-type li.office').click(function () {
o++;
$('.phones').append('<input class="form-control phone_type" placeholder="'+ $(this).text()+'-'+o+'" name="phone['+$(this).attr('class')+'-'+o+']" type="text" ><br>');
});
$('ul.phone-type li.mobile').click(function () {
m++;
$('.phones').append('<input class="form-control phone_type" placeholder="'+ $(this).text()+'-'+m+'" name="phone['+$(this).attr('class')+'-'+m+']" type="text" ><br>');
});
$('ul.phone-type li.fax').click(function () {
f++;
$('.phones').append('<input class="form-control phone_type" placeholder="'+ $(this).text()+'-'+f+'" name="phone['+$(this).attr('class')+'-'+f+']" type="text" ><br>');
});
i have to reset it for every li..
is there any way that i can make it simple!!!!
tnx
This method will allow for an indefinite number of clickable list elements. Just ensure that you provide the 'data-type' attribute in any elements that are included.
When storing data in an html element, it is usually best to use the 'data' attribute. Using classes only works when there is one class.
HTML
<ul class="phone-type">
<li data-type="office" id="1">office</li>
<li data-type="mobile" id="2">mobile</li>
<li data-type="fax" id="3">fax</li>
</ul>
<div class="phones"></div>
JS
// This object will contain how many clicks each element has
var typeClicks = {};
$('ul.phone-type li').click(function() {
var li = $(this),
type = li.data('type'),
text = li.text(),
clicks;
// check if typeClicks contains any click data for this element
if (typeClicks.hasOwnProperty(type)) {
// if it does, increases tracked clicks by one
typeClicks[type]++;
clicks = typeClicks[type];
} else {
// if not, this will create an entry for this element
clicks = typeClicks[type] = 1;
}
// if not, this will create an entry for this element
$('.phones').append('<input class="form-control phone_type" placeholder="'+text+'-'+clicks+'" name="phone['+type+'-'+clicks+']" type="text" ><br>');
});
I am using Twitter Bootstraps "dropdown menu" on WordPress for some widget I created and it works fine. But I want to change the icon to "minus" when it drops the content and when another "plus"-icon is clicked the "minus" should close. At the moment it will only toggle the current "plus".
<div class="dropdown toggle-details">
<img src="">
<h3>title</h3>
<h4><subtitle</h4>
<a class="dropdown-toggle my-btn" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"><i class="fa fa-plus-circle"></i></a>
<ul class="dropdown-menu" >
<li> <h6>item 1</h6></li>
<li><h6>item 2</h6></li>
</ul>
</div>
my script is
jQuery('a').click(function() {
jQuery(this).find('i').toggleClass('fa-minus-circle');
jQuery(this).find('fa-minus-circle').toggleClass('fa-plus-circle')});
You're missing a dot in your jQuery, so right now jQuery is looking for an html element with tag name fa-minus-circle within the "a" element. And obviously not finding it.
jQuery(this).find('.fa-minus-circle').toggleClass('fa-plus-circle')...
actually that probably won't fix it either, because after that statement you'll end up with both classes on the i element. I guess you could work around that with css, but cleaner would be to have the "i" element default to a + icon, and then toggle a more semantic class name like "open".
So css:
i { /* show plus icon */ }
i.open { /* show minus icon */ }
And jQuery:
jQuery("a").on("click", function() {
jQuery(this).find("i").toggleClass("open");
});
Heh - now that I just typed everything out I see what you were doing with that second statement. So yeah, you just need a dot so jquery looks for the classname not the element.
I am trying to develop custom modal plugin for durandal 2.1 to have my own logic and abstract it from the rest of my app here is what I have so far but something does not work and modal gets inserted in DOM twice
define(['jquery', 'knockout', 'plugins/dialog'], function ($, ko, dialog) {
var modal = {
install: function (config) {
dialog.addContext("Modal", {
addHost: function (theDialog) {
var body = $("body");
$('<div id="Dialog" class="AlignC"><div class="ModalHost"></div></div>').appendTo(body);
theDialog.host = $('#Dialog').get(0);
},
removeHost: function (theDialog) {
alert("demoving host");
$("#Dialog").remove();
},
compositionComplete: function (child, parent, context) {
var theDialog = dialog.getDialog(context.model);
}
});
}
};
return modal;
});
and here is how i call it from my viewmodel
dialog.show(this, null, 'Modal');
can anyone tell me what is wrang with this code why my model ELEMENT is inserted twice and ovelay each other. how can i fix that.
second element does not have content inside.
by the way here is view I am trying to show inside modal
<span class="Loader"></span>
<div class="Modal">
<h2 class="Caps">SomeName</h2>
<div class="Row">
<input type="text" />
</div>
<div class="Desc">
description
<br />
XXY YYX XXY
</div>
<div class="Buttons">
<span class="Green">Check</span>
<span>Add</span>
</div>
</div>
Ok I managed to fix this behavior. the problem with click binding was firing twice and this was the problem associated with inserting tow HTML elements in DOM after fixing click handler, everything works just fine.
I have some mobile navigation that uses a scrollTo function, that once clicked, makes my toggle function only work on the second click. It may have to do the the binding of the scrollTo click, but I can't figure out how to fix it.
HTML:
<div class="nav-bar">
<ul class="nav">
<li>Section 1</li>
<li>Section 2</li>
<li>Section 3</li>
</ul>
<a class="nav-toggle">Menu</a>
</div>
<div class="section-wrap">
<div id="section1" class="section">Section 1</div>
<div id="section2" class="section">Section 2</div>
<div id="section3" class="section">Section 3</div>
</div>
JS:
$('ul.nav a').bind('click',function(event){
$('ul.nav a').removeClass('on');
$(this).addClass('on');
var $anchor = $(this);
$('html, body').stop().animate({
scrollTop: $($anchor.attr('href')).offset().top - 30
}, 200, function() {
$('.nav-bar').removeClass('open');
});
event.preventDefault();
});
$('a.nav-toggle').toggle(function() {
$('.nav-bar').addClass('open');
}, function () {
$('.nav-bar').removeClass('open');
});
See this for a working example of the issue: http://jsfiddle.net/MhSKC/9/
At the end of your animate, you remove the open class. Then on the next click of the menu bar, the toggle function tries to remove the open class again, because that's the one of the two toggle functions that needs to execute next. Here's a slightly reworked solution:
http://jsfiddle.net/P4mtC/
$('a.nav-toggle').click(function() {
$('.nav-bar').toggleClass('open');
});
instead of using
$('a.nav-toggle').toggle(function() {
$('.nav-bar').addClass('open');
}, function () {
$('.nav-bar').removeClass('open');
});
use
$('a.nav-toggle').click(function() {
$('.nav-bar').toggleClass('open');
});