Target only the clicked element using jQuery

How can I make it so that when I click on a box-one arrow, only the corresponding box-two slides open and not all of them? I have multiple boxes with the same class and currently clicking on one opens all of them. Can anyone provide a solution?

<div class="box-one">
<a href="#" class="arrow"></a>
</div> 
<div class="box-two">
<p>Some text</p>
</div>

<div class="box-one">
<a href="#" class="arrow"></a>
</div> 
<div class="box-two">
<p>Some text</p>
</div>

.box-two { display: none; }

$('.box-one a.arrow').on('click', function(e) {
    e.preventDefault();
    ('.box-two').slideToggle(300);
});

Answer №1

Utilize closest to locate the specific .box-one containing the clicked arrow, then use next to target only the subsequent .box-two following that .box-one:

$(".box-one a.arrow").on("click", function(e) {
    e.preventDefault();
    $(this).closest(".box-one").next().slideToggle(300);
});

Interactive Example:

$(".box-one a.arrow").on("click", function(e) {
    e.preventDefault();
    $(this).closest(".box-one").next().slideToggle(300);
});
.box-two { display: none; }
<div class="box-one">
<a href="#" class="arrow">arrow</a>
</div> 
<div class="box-two">
<p>Some text</p>
</div>

<div class="box-one">
<a href="#" class="arrow</a>
</div> 
<div class="box-two">
<p>Some text</p>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Discover more: The traversing section of the jQuery API documentation (the entire API documentation can be read in about an hour).

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

Combine the label and input within a single div tag

I'm attempting to enclose both my radio input and its respective label within a single div, but for some reason, it is not containing only the input. Here is my current code: The HTML code is as follows: <div class="payment_processor-section"> ...

Arranging the 1st element of the 1st row to be placed at the end using CSS Flex

One of my challenges involves choosing between two options: https://i.sstatic.net/JpWiGfW2.png In my project, I am utilizing a container with flex-wrap: wrap to showcase items of equal dimensions. However, the first item in this container stands out due t ...

Vanishing Submit Button with CSS

In my input submit button, I have included the following code: <input class="buttons" type="button" onclick="javascript:jQuery(xxxx)" style="font-size: 12px;" value="Invite to Bid"> Additionally, there is a CSS function that adds an elegant "Go" im ...

Is it possible that a link with a negative z-index is unclickable in IE11 but functions properly in Chrome, Firefox, and Edge?

I am currently facing an issue with a menu and div content. The problem is that the link in the menu is not clickable on IE 11. What could be causing this problem? Visit this link for more details #menu { position: absolute; background: red; wid ...

What is the best way to effectively integrate jQuery plugins into Node.JS?

Getting Started with Node.JS I recently ventured into the world of Node.JS, leveraging my existing expertise in JavaScript and jQuery. While I successfully installed jQuery using npm install jquery, incorporating plugins into my code has proven to be a bi ...

Is there a solution available for tidying and organizing a CSS stylesheet efficiently?

In the project I am currently working on, the previous developers took an interesting approach by constructing all the selectors based on individual properties. For instance: .panel .selected a, a.selected-thumb, .date-picker input[type="submit"], .headi ...

Unraveling the mysteries of JQuery AJAX POST and Serialization techniques!

I am struggling to implement an AJAX request using JQuery to validate and submit a form, extract its values, and assign them to variables for further use. Unfortunately, I lack a clear understanding of AJAX functionality as well as how serializing works. ...

Necessary input in Html.TextBoxFor

Is there a way to format the value of a TextBox using the following structure: <td>@Html.TextBoxFor(m =>(m.Code), new { @required = "required"})</td> Initially, this format works as intended. However, when a default value is set for the T ...

Tips for creating text that wraps around an image in an editor

On my webpage, I am using an editor called Cleditor jquery plugin. The default setting allows me to insert images, but the text does not wrap around it. I attempted using vertical-align:top, but it did not resolve the issue: What CSS property should I ap ...

Enhancing Next.js with custom CSS for React-Bootstrap components: A step-by-step guide

After installing the react-bootstrap module using the pm command and importing it into my _app.js file, I encountered an issue when trying to modify the Bootstrap code for my navbar component. The code snippet provided below showcases the navbar component: ...

What steps should be followed in order to write this piece of JavaScript

Upon viewing this post, you will notice an "add comment" link below it. Clicking on this link will add a textarea for users to input their comments. I am curious about how I can implement a similar function? Update: Thank you all for your assistance. I ...

What is the best way to utilize jquery autocomplete for searching values?

I've noticed that the autocomplete feature is working fine, but when I click on one of the suggested values in the drop-down list, it just gets added to the search box. I then have to manually hit enter to perform the actual search. Is there a way to ...

Creating a chic look for your conditional statement: If Else Styling

While it may not be possible to directly style PHP code, you can definitely style the HTML output that PHP generates. I'm curious if there's a way for me to apply styles to the output of an IF ELSE statement. if ($result != false) { print "Y ...

Insert straight lines between elements in an inline list

I am working on building a step progress bar using the HTML code below: .fa-check-circle { color: #5EB4FF; } .fa-circle { color: #CFD7E6; font-size: 14px; } .container { width: 100%; position: absolute; z-index: 1; margin-top: 20px; } . ...

Asynchronous JavaScript function within a loop fails to refresh the document object model (DOM) despite

I have been working on a function that utilizes ajax to retrieve instructions from a backend server while the page is loading. The ajax code I've written retrieves the instructions based on the number provided and displays them using the response.setT ...

Delivering styled coldfusion outcomes utilising css and jquery

After using jquery to retrieve some data, I've noticed that the results are displayed correctly but the CSS is not being applied. Is there a way to style the results with CSS? function doSearch() { //Making an AJAX call to the server $.ajax({ / ...

Issue with absolute positioning in IE6 and IE7 not functioning correctly

I recently created a website for a friend that involves absolute positioning within a relative positioned div. After testing it in various browsers using Browserlabs, I found that it works well except for IE6 & IE7. Despite searching online for solut ...

Place the blockquote in the middle, but maintain the text's alignment to

I recently designed a webpage using Bootstrap and CSS. My main goal is to have my two blockquotes centered on the page while keeping the text left-aligned (which is the default behavior for the selected classes in Bootstrap). To see the full code, take a ...

Fetching content and an image simultaneously via AJAX

My website features a photo gallery that I created using the code below: /*Begin Photo Gallery Code*/ var images = ['g1.jpg', 'g2.jpg', 'g3.jpg', 'g4.jpg']; function loadImage(src) { ...

Problem with sending variable via AJAX

Hey everyone, I'm attempting to send form data along with an extra variable using AJAX. Here's the code snippet: function tempFunction(obj) { var data = $('form').serializeArray(); data.push( { no: $(obj).at ...