What is the best way to showcase the current element using jQuery?

I have this example:

link

HTML CODE:

<div class="common-class">
  <div class="trigger">
    <span class="plus">+</span>
    <span class="minus">-</span>
  </div>
  <div class="show"></div>
</div>
<div class="common-class">
  <div class="trigger">
    <span class="plus">+</span>
    <span class="minus">-</span>
  </div>
  <div class="show"></div>
</div>
<div class="common-class">
  <div class="trigger">
    <span class="plus">+</span>
    <span class="minus">-</span>
  </div>
  <div class="show"></div>
</div>

CSS CODE:

.trigger{
  background:red;
  width:100px;
  height:50px;
}
.show{
  width:100px;
  height:100px;
  background:blue;
  display:none;
}
.common-class{
  display:inline-block;
  margin-left:10px;
}
.minus{
  display:none;
  color:white;
}
.plus{
  color:white;
}

JAVASCRIPT CODE:

$( ".plus" ).on( "click", function() {
  $(this).closest('.common-class').find('show').show();
});

My aim is to only display the current element with the class "show".

I've tried something like 'but it shows all elements.

 $(".show").toggle();

How can I solve this issue?

I hope I explained myself clearly... let me know if you need further explanations.

Thank you in advance!

EDIT:

I'm back with a question, I managed to make it work but how can I keep only one open at a time?

new link

JAVASCRIPT CODE:

$( ".plus" ).on( "click", function() {
  $(this).hide();
  $(this).closest('.common-class').find('.show').show();
  $(this).next().show();
});

I want only the current element to stay open, not allow more than one to be open...

EDIT 2:

new link 3

I'm almost done... there's just one problem. There are other minus signs remaining before... only one minus sign should exist when open

JAVASCRIPT CODE:

$( ".plus" ).on( "click", function() {
  $(this).hide();
  $('.show').hide();
  //$('.show').hide();
  $(this).closest('.common-class').find('.show').show();
  $(this).next().show();
});

$( ".minus" ).on( "click", function() {


});

Answer №1

You are currently facing a syntax error related to the selection you are using; it is crucial to locate elements that are relevant to the current element.

One alternative solution is to utilize a class for controlling visibility:

$(".plus, .minus").on("click", function() {
  var $comuna = $(this).closest('.clasa-comuna').toggleClass('open', $(this).hasClass('plus'));
  $('.clasa-comuna.open').not($comuna).removeClass('open');
});
.declansare {
  background: red;
  width: 100px;
  height: 50px;
}
.arata {
  width: 100px;
  height: 100px;
  background: blue;
  display: none;
}
.clasa-comuna {
  display: inline-block;
  margin-left: 10px;
}
.clasa-comuna .minus {
  display: none;
  color: white;
}
.clasa-comuna .plus {
  color: white;
}
.clasa-comuna.open .minus {
  display: inline;
}
.clasa-comuna.open .plus {
  display: none;
}
.clasa-comuna.open .arata {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="clasa-comuna">
  <div class="declansare">
    <span class="plus">+</span>
    <span class="minus">-</span>
  </div>
  <div class="arata"></div>
</div>
<div class="clasa-comuna">
  <div class="declansare">
    <span class="plus">+</span>
    <span class="minus">-</span>
  </div>
  <div class="arata"></div>
</div>
<div class="clasa-comuna">
  <div class="declansare">
    <span class="plus">+</span>
    <span class="minus">-</span>
  </div>
  <div class="arata"></div>
</div>

Answer №2

To ensure only one div is open at a time, you can use the following JQuery code:

$( ".plus" ).on( "click", function() {
  $('.arata').hide();
  $(this).closest('.clasa-comuna').find('.arata').show();
});

This script will hide all other divs before opening the selected one.

Check out this working example on Fiddle

Edit:

I have made an update to my answer based on your specific requirements.

$( ".plus" ).on( "click", function() {

  $('.arata').hide();
  $('.minus').hide();
  $('.plus').show();
  $(this).hide();
  $(this).closest('.clasa-comuna').find('.arata').show();
  $(this).next().show();
});

Here is the updated version on Fiddle

Answer №3

transform your JavaScript CODE in this manner

$( ".plus" ).on( "click", function() { $(this).parent().find('.display').show();

});

Remember to add "." before display if it is a class of div, and '#' if it is an id

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Leveraging the On Keyup Function in Real-Time

How can I modify this code to run after an ajax call using .live method? var element=document.getElementById('txt_url'); element.onkeyup=function(){ var input=element.value; if(input=='') return; if(input.indexOf('h ...

Limiting the use of CSS selectors to a single class without the need to include the class name multiple

I am trying to find a solution to avoid repeating the same class name multiple times in CSS. For instance, if I have: .class1 div { color: red } .class1 h2 { color: blue } .class1 p { color: yellow } Is there a way to consolidate these three rules under ...

Discover the steps to download web page data using Objective-C while ensuring that JavaScript has finished executing

I attempted something similar: NSString *url = @"http://www.example.com"; NSURL *urlRequest = [NSURL URLWithString:url]; NSError *error = nil; NSString *htmlContent = [NSString stringWithContentsOfURL:urlrequest encoding:NSUTF8StringEncoding error:&e ...

Issues with PHP jQuery datatables ajax refresh functionality not working

After searching through numerous Stack Overflow posts regarding this issue, I am still unable to find a solution. Therefore, I am reaching out to you for help. My problem involves making an AJAX call to an external PHP file in order to retrieve the necess ...

Error encountered during the construction of the dojo due to a CSS syntax

I am currently using version 1.9.3 of dojo, and I have encountered an issue with the css file when trying to build dojo using build.bat. The code in my css file is as follows: @import url("TimeDriverCommon.css"); @import url("DialogCommon.css"); @import ...

Accessing and parsing JSON data from directories within directories

My file directory structure is dynamic, with only the name of $(Root Directory) known at runtime. All folders and files are generated dynamically, with all files being in .json format. I am looking to count the number of files and read the content of eac ...

Using Vanilla JavaScript to Disable a Specific Key Combination on a Web Page

On a completely random English-Wikipedia editing page, there exists a way to add content (for example, "test") and save it using the existing key combination of Alt+Shift+S. My goal is to specifically prevent this action without removing the save button b ...

React Native: Struggling with Button Styling

I am relatively new to React Native, although I have experience with React in my professional work. I'm finding that applying styles to components in React Native is a bit different than what I'm used to. Specifically, I am struggling to apply s ...

Roll high with the combinations of Yahtzee

<body> <div class="board" style="border: 2px solid black"> <table border="1" height='200' > <tr> <th colspan="3">Upper Section</th> </tr> <tr> <td>Aces</td> <td>Sum of ...

Safari on iOS 11.4 ignoring the 'touch-action: manipulation' property

I ran into an issue while developing a React web app that I want to work seamlessly across different platforms. My goal was to allow users to interact with a div element by double-clicking on desktop and double-tapping on mobile devices. However, when tes ...

Arranging an accordion-style table using jQuery

Currently, I am in search of an accordion table that has the ability to be both sorted and paged. Despite my efforts, I have not been able to find a suitable sort/page plugin that can provide the desired accordion effect for my table. To achieve the accor ...

Incorporating a background-image to enhance the appearance of checkboxes

I am attempting to set up checkboxes that will display different images when checked and unchecked. Below are the checkboxes I am currently using: <input type="checkbox" name="fordCheckBox" value="Ford"> To assign background images to each checkbox ...

jQuery event.preventDefault not functioning as expected

I am attempting to use jQuery's preventDefault() method, but when I submit the form, it still displays the default behavior and reloads the page. Here is the code from index.html: <body> <script src="/socket.io/socket.io.js"></script& ...

Issue with Child Element Width in CSS3 Not Adhering to Parent Container's Width

I'm working on containing my ul and li elements within a div while respecting the div's width using margin-left: auto. My goal is to prevent the ul/li from extending outside of the div, keeping them neatly inside the container. Based on my resear ...

What steps can I take to decrease the padding of this footer?

Is there a way to reduce the height of the footer so it doesn't dominate the screen on both large and small devices? import { Container, Box, Grid } from "@material-ui/core"; const Footer = (props) => { return ( <footer> ...

Conclusion of Tour when an event is triggered by the parent view - Intro.js

File Manager - Home Normal https://i.stack.imgur.com/7C5lH.png I primarily utilize AJAX callbacks for most of my content. Whenever I click a link button on the page, it triggers a modal pop-up using jQuery events and a specific class. The Tour Guide implem ...

Altering data in a concealed dynamic HTML form

I have a hidden form on my webpage that gets populated dynamically when a button is clicked. I want to be able to change the values within this form dynamically. Initially, the form looks like this: <form id="zakeke-addtocart" method="pos ...

Chrome's Surprising Cross-Domain File URL Problem

Is anyone else experiencing the issue with Chrome throwing a Cross Domain Error when using file URLs? I'm trying to embed an SVG document into an HTML page using the object tag with a relative path in the data attribute. Upon the onload event, I need ...

Complete the form by following a hyperlink located on a different webpage

I recently developed three PHP pages. The first one consists of the following form: <form method="POST" name="go" action="search_form_all.php" > <input name="value" type="text" id="search_form_1" size="65" placeholder="enter name"/> <i ...

The .each method is failing to iterate over the next object

Currently, I have been working with JSON data retrieved from the web. After successfully receiving the data, I proceed to create a JavaScript object to work with it. However, there seems to be an issue with retrieving the values of fname and lname from th ...