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

Switching from a click event to a hover event in JavaScript

I've been experimenting with animating a burger bar on hover and came across an example online that I managed to adapt for mouseenter functionality. However, I'm struggling to make it revert back to the burger bar shape once the mouse leaves on m ...

Manipulate HTML elements using JavaScript when a key is being held down

I'm currently developing a simple game using vueJS for the frontend. I have successfully created all the necessary objects and now my goal is to enable their movement when a key is pressed. However, I am facing an issue where the object only moves on ...

Centering Vertical Tabs Using HTML and CSS

I followed a vertical tab tutorial on W3Schools and would like to implement it on my website. However, I am having trouble centering the tabs after reducing their width sizes. I have attempted adding div tags to the code and aligning them to the center wit ...

Guide on incorporating jQuery accordion within a specific div element through ajax loading?

I am facing an issue with my jQuery accordion. It functions properly when viewed on its own, but when I try to load it into a div on my home page using a link and the load method, it loses all functionality and appears as a list with headings. You can see ...

index.html: using jquery for selecting colors

I attempted to integrate a jQuery plugin into my application, but it doesn't seem to be working. In the head section of my code, I have included: <link rel="stylesheet" media="screen" type="text/css" href="./style/colorpicker.css" /> <script ...

Guide to resizing images with next.js

I have an issue with resizing an image within a Bootstrap card using next/image. I am trying to decrease the size of the image to about 60% of its original size and prevent it from filling the entire top portion of the card. I attempted wrapping the imag ...

Can jQuery be used to change the functionality of a submit button, such as toggling between show, hide, and submit options?

I am having trouble toggling a button to either submit a form on click or reveal an input field, depending on whether the user clicks outside the input field to hide/collapse it. The issue arises when the user dismisses the input field and the submit butto ...

What is the best way to retrieve the DOM of a webpage that uses MP4 file extension?

I'm attempting to access the DOM within a page that has an MP4 extension. The page isn't a video or audio file, it's just HTML with an MP4 extension "". I've included the code I'm using to try and access it using Simple HTML DOM. T ...

Show information from table B following the selection made in table A

In my project, I need to retrieve and display data from the "foom" table once an option is selected from the "factory" table. The options in the select dropdown are populated with data from the "factory" table. The layout of the tables is as follows: Ima ...

Icon appearing outside the button instead of inside

Here's a button I'm working with: <button class="glyphicon glyphicon-option-horizontal" (click)="myFuncion()"></button> Recently, I updated my bootstrap library from version 3 to 4.2.1 and now the icon is no longer visible. I' ...

Connecting mysql workbench data using html: A step-by-step guide

I'm currently creating a user interface for a database system using HTML. I'm a bit stuck on how to access and manipulate the database in MySQL Workbench. Does anyone have any suggestions or sample code that could help me connect them? Thanks! ...

Refresh the DIV by populating it with data retrieved from an AJAX call

My inquiry is regarding two divs: one containing an @ajax.beginform and the other being empty but hidden. I am trying to implement a scenario where the form in the first div makes an ajax request, and the action method of the controller checks if the model ...

What steps do I need to take to ensure that this Regex pattern only recognizes percentages?

I am attempting to create a specific scenario where I can restrict my string to three digits, followed by a dot and two optional digits after the dot. For example: 100.00 1 10.56 31.5 I've developed a regex pattern that allows me to filter out any ...

Using Jquery to Deactivate Field upon Meeting Conditions

In my form, there are 15 rows with unique IDs ranging from 1 to 15 (row_id). Each row consists of a select field and multiple text fields. My goal is to disable row_id 5 if the select option in rows 6 or higher is anything other than 0, and to disable row ...

Utilize a JavaScript variable within HTML within the confines of an if statement

On my HTML page, I am dynamically displaying a list of properties and then counting how many are displayed in each div. <script type="text/javascript> var numItems = $('.countdiv').length; </script> The class name for each property ...

I am unable to correctly fetch the data string using Jquery Ajax from the server

I have implemented a jQuery Ajax function to check the availability of a username in real-time from the database. If the username is not available, the response is marked as "Unavailable" and vice versa. While I am successfully receiving this response, I a ...

jquery for quick search

<form method="post" action="search.php"> Commence search: <input id="search" type="text" size="30" > <div id="search_results"></div> <script src="//code.jquery.com/jquery-1.12.0.min.js"></script> <script src="//code. ...

Tips for enhancing your custom jQuery code

Here is a simple jQuery lightbox code that requires a new line of code for each element clicked. I'm looking for a way to bind this lightbox to any class or id with a rel="qpLighbox" attribute and use the href tag to load the necessary file via AJAX. ...

The image zoom function is malfunctioning when trying to adjust the image position

Having some trouble with a code for image zoom in/out. When I adjust the position of the image by applying margin left to either the image or the image container (#view), it affects the zoom functionality - causing the image to move to the left during zoom ...

Enhance the styling of elements generated through JavaScript in VueJs with custom CSS

I need help applying CSS to elements that I dynamically created using JavaScript. buttonClicked(event) { console.log(event); let x = event.clientX - event.target.offsetLeft; let y = event.clientY - event.target.offsetTop; let ripples = document.cre ...