Div display based on radio selection - javascript

Currently I have a piece of code that works fine as long as there are no other divs in the page. If I add other divs, they will close upon any radio selection. I just need a simple modification to the code to open and close without closing all other divs. The working example can be seen here.
http://jsfiddle.net/L5qfn/38/
I added the "wrapper" to the entire contents of the body to demonstrate how everything closes. Take out the wrapper...and things work like I want it to. Any suggestions?

The problem comes from your <div id="wrapper">, and this line :
$('div[class!="formset"]').slideUp("fast"); //Slide Up Effect
I rather suggest to change it to :
$('div .sub-formset').slideUp("fast"); //Slide Up Effect
See the result : http://jsfiddle.net/L5qfn/40/

The problem is caused by the following line:
$('div[class!="formset"]').slideUp("fast");
That basically says "hide any div which does not have the class formset". The wrapper div does not have that class, so it gets hidden. I'm not sure what that line is doing (it appears to do nothing useful in the fiddle at least) so I'd suggest just removing it, unless you can expand upon your problem.
Here's an updated fiddle.
Update based on comments
As #fflorent has mentioned, it looks like you actually wanted to hide .sub-formset, so you probably want to change that selector to:
$(".sub-formset").slideUp("fast");
Note that I've used a class selector (the . character) rather than using an attribute equals selector as you've done in your fiddle.

Related

Link in html doesn't work but is visible i bottom-left corner of browser

I have a problem with a href="... it doesn't work. When i right click on it i can open it in new tab easily but when i left click it doesn't work at all, i think is because of JS script but i have no idea how to change it. It might but that <div id="planes"> getting class="dragged"
Link to: CodePen
And thanks to Jeff Mignone for this script
If you remove this row it will work (line 182):
this.options.element.classList.add("dragged");
Its purpose, as seen at a glance, is to allow you to add a custom style when dragging.
The problem is that for to do so, it adds the class directly to the element that triggered the event. If this element is, for example, the tag 'a' you want, changing the element will interrupt the event handling and the click event will not handled as expected.
A possible solution is to add the class after some time (by using the setTimeout function) or add the class to the parent of the element.
For example, replacing the above line with the following line will solve the problem as well (Don't replace the 10 with 0, it will fail it):
setTimeout(() => this.options.element.classList.add("dragged"), 10);

html append to already appended div does not work with appendTo

i have an appended div, what i want is to append a div with the appended one.
please see the codepen of my code which is well explained with comments.
here is the structure i want:
DIV 1 --> DIV 2(append with div1) --> DIV 3(Append to already appended Div 2)
i tried to use appendTo(); and also $('#clsDashRegion_levelThree').find('#clsDashRegion_siteAlmCriticite').append(infoRegionSiteCriDetails); but it does not work. if you can help me please make it work, thank you !
To be honest the code is quite bad. There are much better ways to accomplish what you want, but nevertheless here is your fiddle slightly modified. Please tell me if the behavior is what you want:
https://codepen.io/anon/pen/OjNXWv
One important thing is that the id in the second div that you want to be clickable does not receive direct events because it was generated dynamically. So you have to use event delegation like so:
$(document).on('click','#clsDashRegion_siteAlmCriticite',function() { ...
After the
$('#clsDashRegion_levelTwo').html(infoRegionCriticite);
});
Shouldn't you then call
levelThreeCriticiteDetailsBloc()
Otherwise when you click '#clsDashRegion_siteAlmCriticite' nothing will happen.

jQuery toggling dynamically generated nested elements

I'm currently designing a webpage presenting a twitter-like user input that generates an <li> (inside a <ul>) element in which are appended one <h6> element (the post's title) and a <p> element underneath (the content).This works, therefore the input and generation of elements is not the problem.
But what I want to do is use jQuery to hide the posts's content, and toggle it when I click on the post's title. The issue is that the event handler seems to work only for every second post. Whenever i post once more, the 1st post on the list can be toggled, the second not, third yes, etc.
From what I've seen in some answers, I've tried the .click() method, the .on() method, I've tried to replace .toggle() with.hide() and .show() under conditionals, then created a class with display:none to toggle on click. This was my last stop, and the result is described in the above paragraph. Here's the event handler:
$('.postinstance').on("click", "h6.postname", function() {
$(this).siblings().toggleClass('postOff');
});
The .siblings() is actually only the post content, but that's the only way I could get near what I wanted. When I replace $(this).siblings() with the actual class of the content element, every post's content toggles when I click on any title.
How can I make each post open individually when I click on it?
Here's a JSFiddle isolating the input & posts part.
I have looked thoroughly in Stack Overflow and other places, even tutorials, to solve this problem but although similar questions were found none of their answers provided a solution.
You should not attach event handlers to dynamically generated elements directly, instead use some common parent element. Here's a piece of your snippet where I changed the selector and everything started working:
$('.postlist').on("click", "h6.postname", function() {
$(this).siblings().toggleClass('postOff');
});
Important note: you must pull this piece of code out from $('.postbtn').click(..) one level up, otherwise for even number of posts toggling will not work!
And move this out of click handler:
$('.postlist').on("click", "h6.postname", function() {
console.log(this);
$(this).siblings().toggleClass('postOff');
});

Using `.toggle()` rather than applying a class to show content

I'm working on a simple website to use at a conference and I'm looking for some help understand the implications of two ways to achieve an effect:
Using .toggle() to show or hide content
This is the method I started with because it is an intuitive action to tap an element to have it's content appear. However, a problem arises when I try to limit one open div at a time.
Summary I'm having trouble limiting the number of opened elements.
Applying an active class with jQuery
Using this method, I can display the hidden content by selecting the child element (see code below), but this stops the user from closing the content by tapping it again. Because I'm expanding divs horizontally, this isn't ideal because of the scroll space that's added.
Summary: How do you close the active div on a second click with this method?
CodePen Demo - Staged site
Relevant Code
This method is using CSS to apply the active class. It works, but like I said above, I'm having a hard time removing the active class from an element tapped again. Use the demo linked above to see how the toggle action works on the page (uncomment lines 8 and 9).
$(".title").click(function() {
//remove active class from other elements
$('.post').removeClass('active');
// Bind to the div
$post = $(this);
// Set active class on .post to control scroll position
$post.parent().toggleClass('active');
// Toggles the hidden .content div
//$post.next().toggle(250);
$('html, body').animate({scrollLeft: $('.active').offset().left},500);
});
The accompanying .active CSS:
.post .content {
display:none;
}
.active {
margin-top:-120px;
}
/* Shows the content div rather than toggling with jQuery */
.active > .content {
display:block;
}
Is there a way I can allow both behaviors (tap to open/close, one open div at a time)? Which method is best suited for that?
You certainly can use toggle() while hiding the other ones. Try something like this:
$(".title").click(function() {
$('.post').not($(this).parent()).hide();
$(this).toggle();
$('html, body').animate({scrollLeft: $(this).parent().offset().left},500);
});
Update: changed .not(this) to .not($(this).parent()) as .title is always child of .post.
Slightly optimised version of #Daniel's solution
$('.title').click(function() {
var clickedPost = $(this).parent('.post')
clickedPost.toggle().siblings('.active').hide();
$('html, body').animate({scrollLeft: clickedPost.offset().left},500);
});
Local var: If you access this, or any other DOM element more than once inside a scope, it's always more efficient to assign it to a local var than wrap it in a JQ object multiple times.
SIblings selector: I don't have a benchmark for this, but running a selector on a subset of the DOM rather than the whole DOM seems intuitively faster. This is more best practice than a large performance hit, but all the little functions add up too.
Chaining JQuery functions: Most JQ functions that act on a JQ element return that element. I can't say that this is more efficient but it's certainly more concise, but this all depends on personal preference.
With very little code you can do this with toggle.
$(".title").click(function() {
$(".post").hide();
$(this).children(".post").toggle();
});
I made it as simple as possible to show the functionality which you could then extend on.
Here is a jsfiddle
EDIT update after comment
I have edited it to now only show 1 at a time and if the 1 currently being shown is clicked it hides it
I also elected to use slideUp() and slideDown() as it seemed to better suit your needs
$(".title").on("click", function(){
if($(this).children(".post").is(":visible")){
$(this).children(".post").slideUp();
}else{
$(".post").not($(this).parent()).slideUp(500);
$(this).children(".post").slideDown(500);
}
});
updated jsfiddle

How do I hide an element with the same markup via jquery?

I have two custom dropdown lists that have the same markup. I need to have only one show at a time. Right now, I'm able to open both at the same time. Both should also close when I click off the list.
The same markup for both lists is required, so I can't use unique ID's or additional classes to make this happen.
Here is a link to my fiddle example: http://jsfiddle.net/dg7Lc/29/
Any help would be greatly appreciated, thanks!
-D
Consider adding a data attribute such as 'active' via jquery when you click on one of them, then hide all those that have that attribute.
$('.custom-select').eq(0).hide() will hide the first one.
Use .show() instead of .hide() to show (obviously) and change the index to (1) to get the second one.
First thought would be if you could wrap a span or div around either or both and use that to get around the "same markup" limitation. Other than that, though, I'd suggest using order in page - use .next() and .prev() to get between them, and something like
$("div.custom-select").get(0)
or
$("div.custom-select").get(1)
to select them from outside.
edit: if you can run them off of something like an onmouseover, onchange, or whatnot, it's even easier - the one that's changing will be passed into the function as the "this" parameter. Just hide both, and show this, or show both and hide this.
edit2: similarly, once you have one of them hidden properly - well, that one will be hidden, and respond to the ":hidden" selector. Use that to distinguish between them (and save the distinction as a jquery variable) before you go showing or hiding anything else
Hide the first:
$('.custom-select').first().hide();
Hide the second:
$('.custom-select').last().hide();
And then put these lines of code where needed.
http://jsfiddle.net/dg7Lc/31/
Basically, closing the others:
$('.custom-select').not(this).find('ul').slideUp('fast');
And for closing when clicking outside the box, I used this piece of code but it's a bit dirty:
$("body").click(function(e) {
var should = true;
for($e = $(e.target); should && $e.length; $e = $e.parent()) {
should = !$e.is(".custom-select");
}
if(should) {
$('.custom-select').find('ul').slideUp('fast');
}
});
You can bind a click to the document, that looks to see if they clicked on the custom-select or the document outside it and hides any open lists as it should:
$(document).click(function(ev){
if(!$(ev.target).is('.custom-select span')){ $('.custom-select').find('ul').slideUp('fast'); }
});
Updated JSFiddle

Categories

Resources