Jquery show hide multiple divs independently

I am new to jquery and need some help with this I have the following simplified html [Code] Is there a way I can simplify my code below? I want to be able to have just one iteration of the code and have it automatically switch out the IDs without have to add a new bit of JavaScript with each new div that is added.

I've got a PHP while loop spitting out an article title and it's content from a database and I have links to show or hide the content of the article under each title. I want to show and hide the content of the article selected as you'd expect. It works well!

jquery show hide multiple divs independently

But my script actually clones elements of the form so a user could be selecting adult or child for a number of tickets - ticket1,ticket2,ticket I am using a class name for the divs but the name is generated by a field have set in my database. I've got an issue with the attached file. If no div was open, then it would simply open the relevant div, however if say div 2 was open and div 3 was pressed, div 2 would toggle closed then div 3 would toggle open.

On the homepage of my site, I would like to have some nav links that toggle between a few divs that are below in the content The divs in the content should display one at a time. For example, if you click nav1, the user would see div1. If nav2 is then clicked, div1 would hide and div2 would display.

Onchange event, getting a value from a select tag and changing the visibility of other elements

I have some code that will do this, but for some reason it is hiding any divs that are within the toggled div. I'm very basic with JS and could use some help here. I have a ul list that I am using for a navigation menu and I would like to be able to click on items in the list to show different div's. I assume this is possible, so could someone please give me an indication of how to do it as I have been trying for a few days now and no joy. Complete JS first timer here, hope you all can understand my difficulties and stupidities without getting too frustrated.

Trying to make a portfolio website as a Christmas gift. Anyway, I'm trying to build a menu that: 1. Allows for more than one object to be listed in each 'header'.

Something similar to this [URl] I've tried to hatch together several online solutions but I really have no idea what I'm doing, as you can see: [Code] How this code can be amended to use multiple drop downs within one form?Sponsored by and others. Copy code. Please tell us why you want to mark the subject as inappropriate. Maximum characters. Report Inappropriate Cancel.

jquery show hide multiple divs independently

From : guest. Content :. Type the characters you see in the picture below. Send Cancel. Subject :. Send Update Cancel. Email ID. Attach files. Desktop Google Docs. Each Attachment size should not exceed 1. Max no of attachments : 3. Loading User Profile Response title. Publish Back to edit Cancel. Sign In. New to this Portal?

jquery show hide multiple divs independently

Click here to Sign up. You can also use the below options to login. Search jQuery Search. Move this topic Cancel. Using jQuery. I am using a class name for the divs but the name is generated by a field i have set in my database. So for example. I hope this makes sense and thanks in advance. Replies 5. If i'm understanding what you're wanting to do, and correct me if I'm wrong, but you want to hide all the divs that match the title of the link you are clicking on, right?

Once you have that javascript in place, you should be able to assign any element with the toggle class, and when it's clicked, any other element with the class matching the clicked element's title should be toggled. Try saying that 5 times fast. Thank you very much you are a true saint and honor i have been trying all day with this lol. As i say i can do toggle or slideup and down and what not on one div but it was just confusing me on doing it to more than 1 and with a title that is returned from my database and inserted in there so its always dynamic as in it will always be different depending on its location.

But thanks again Seriously if you were living near me id buy you a big beer lol. Hey sorry to be a pain dude your code works without fail but i tried putting it in with my other code and it didnt like it here is my code can you have a quick look and see if im doing something wrong thanks in advance. Leave a comment on nexusgx's reply. Sorry for the late late late response. Had some personal problems keeping me offline.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

I have a page that has multiple divs which I would like to open and close independently of each other. But as you can see, while the first one works fine, the subsequent divs do not, and also open and close with the first div's operations. I also want the 'Read More This currently works, but all other solutions to similar questions seem to simply expand beneath the initial Read More Since you are new, I restructured the code to be very basic and explained each section.

As others have stated, it is best practice to use classes for elements that are repeated on a page as id is meant for a single occurrence. This is a longer, drawn out answer to help you understand conceptually what should be happening and how it could work. Once you understand the idea behind the code, you can shorten it. Learn more. Asked 5 years, 3 months ago.

Active 5 years, 3 months ago. Viewed times. Great Thanks in Advance.

Show/Hide Multiple Divs with Jquery

Einulfr Einulfr 3 1 1 bronze badge. Just like what epascarello mentioned, an element id should be unique for the page. Active Oldest Votes. Roamer Roamer This is even better! Does everything I need it to. Excellent, that helps heaps! Thank you so much!By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

I want to make a collapsible DIV like this: If you click on the picture which is in the table and comes with show class the hidden div slides down the one with the content class with it's content and if you click on the hide button or anywhere else on the page, the content div will slide up. I want to use multiple tables and hidden divs on the page with the same classes so i want to make sure to open them independently.

Here is my JSFiddle document, hope that someone can help. Thanks in advance! Learn more. Asked 3 years, 11 months ago. Active 3 years, 11 months ago. Viewed 4k times. Sorry, i changed it!

Active Oldest Votes. Pietro Pietro 8 8 silver badges 17 17 bronze badges. Thank you! It worked! Could you help me with the click-anywhere-to-close problem? It's not working! Pietro Could you help with the last comment? One more thing Pietro: i don't want to close the the content DIV if i click inside just if i click outside of the box. How could i achieve this? Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name.

Email Required, but never shown. The Overflow Blog. Podcast Programming tutorials can be a real drag. Featured on Meta.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up. I know close to nothing about jQuery, but I swear I'm trying to learn.

The thing is, I've got some code that works, but I know it's repetitive and probably not kosher for a real programmer, which is why I've turned to you all.

jQuery: Show a DIV - Hide other DIVs

Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered. Asked 8 years, 5 months ago. Active 3 years, 4 months ago. Viewed 21k times. Connie Connie 31 1 1 gold badge 1 1 silver badge 2 2 bronze badges. Active Oldest Votes.

ANeves ANeves 2, 11 11 silver badges 28 28 bronze badges. And as for social being a list - even when it's not incorporating the li tag to bring about different elements it's still considered a list? I thought it was just a div because it's a bunch of images, and not text Isn't it just more code to add a ul component? I have to have div anyway for the social media buttons to position it correct relative to the nav menu and sub menu and the news ticker.

Plus those images aren't really even a list - just a conglomeration of My thought was that abstractly it is a list of So it could be a ulthen styled to be shown in a line. Not too important, though. I get it. Thanks for the clarification and thanks for all the help!

Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Programming tutorials can be a real drag. Featured on Meta. Community and Moderator guidelines for escalating issues via new response….

Toggle Show/Hide Multiple Elements With Pure JavaScript

Feedback on Q2 Community Roadmap.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. So far, the jquery code I have will show the div clicked, but the hide part wont work.

Learn more. Asked 7 years, 1 month ago. Active 7 years, 1 month ago. Viewed times. BeardFist 7, 3 3 gold badges 30 30 silver badges 38 38 bronze badges. Die 20 Die 20 1 1 gold badge 5 5 silver badges 16 16 bronze badges.

Active Oldest Votes. OJay OJay 4, 2 2 gold badges 19 19 silver badges 36 36 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog.

Podcast Programming tutorials can be a real drag. Featured on Meta. Community and Moderator guidelines for escalating issues via new response…. Feedback on Q2 Community Roadmap. Technical site integration observational experiment live on Stack Overflow.

Dark Mode Beta - help us root out low-contrast and un-converted bits. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled.The page will initially show all divs. The idea then is that there will be a button to reset show all and then separate buttons to show a particular div while hiding the rest. You can make this more generic, and use the obvious convention - the button and the div with the same number in the id are related.

I had this same problem, read this post, but ended building this solution that selects the divs dynamically by fetching the ID from a custom class on the href using JQuery's attr function. If they fall into logical groups, I would probably go with the class approach already listed here. Many people seem to forget that you can actually select several items by id in the same jQuery selectoras well:.

Just a small side-note Just replace it with jQuery and you're good. Any help would be much appreciated. Assign each div a class. Add table row in jQuery How do I check if an element is hidden in jQuery? Creating a div element in jQuery How to check whether a checkbox is checked in jQuery? How can I select an element with multiple classes in jQuery?


comments

Leave a Reply

Your email address will not be published. Required fields are marked *