Changing the arrow in jQuery's slideToggle for different div elements is a breeze

Good day, I'm struggling with getting the arrow switch to work correctly for slideToggle. Even though I have three of them, the arrow changes only for the first one - no matter which div I click on. Any suggestions on what I might be missing? Thank you.

Check out the demo here

//jquery

$(document).ready(function(){
  $('.flip').click(function(){
  $(this).next('.panel').slideToggle('fast');
        var src = $('#arrow').attr('src') == "arrow-down.png" ? "arrow-up.png" : "arrow-down.png";
        $('#arrow').attr('src', src);
  });
});

//html

<div id="container">    
    <div class="flip">One   <img id="arrow" src="arrow-down.png" alt="" /></div>

    <div class="panel"> 
        <p>Foo</p>
    </div>
</div>

<div id="container">    
    <div class="flip">Two   <img id="arrow" src="arrow-down.png" alt="" /></div>

    <div class="panel"> 
        <p>Bar</p>
    </div>
</div>

<div id="container">    
    <div class="flip">Xxx   <img id="arrow" src="arrow-down.png" alt="" /></div>

    <div class="panel"> 
        <p>Yyy</p>
    </div>

Answer №1

You were referencing the overall $('#arrow') instead of the specific one within the relevant .flip. I resolved it like this:

$(this).find('#arrow').attr('src', src);

I made updates to your jsFiddle…

http://jsfiddle.net/8y6xb9n5/1/

…however, your code is still a bit disorganized. It's better not to have multiple instances of id="arrow". Rather, utilize classes. Additionally, using a ternary operation to compare src values may be excessive. Instead, consider using classes (.addClass(), .removeClass()) and background-image.

In essence, <img> is ideal for content (such as images) while CSS background is more suitable for interface elements (like icons).

Answer №2

You currently have 3 images all with the same id arrow, and you are updating the src attribute for each of them. A more efficient way to do this is by using the following code:

var src = $(this).children('#arrow').attr('src') == "http://www.tuneup.cz/fileadmin/images/2014/others/arrow-down.png" ? "http://www.tuneup.cz/fileadmin/images/2014/others/arrow-up.png" : "http://www.tuneup.cz/fileadmin/images/2014/others/arrow-down.png";
$(this).children('#arrow').attr('src', src);

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

Is there a way to implement a scrollbar that only scrolls through one specific column in an HTML table?

I need help adding a scrollbar to a specific column in an HTML table. Take a look at this scenario https://jsfiddle.net/6wpdc4tL/: https://i.stack.imgur.com/svzIg.png This table should have two scrollbars, one for the light blue SCROLL column and another ...

What causes the delay in loading certain CSS and JS files?

My ASP.NET MVC5 application hosted on an IIS Server has a slow-loading login page, especially after clearing the browser history. The Developer Tools Network field indicates a problem with loading page contents such as css and js files. Can you provide gui ...

PHP AJAX Request Failing to Transfer Files

I am having trouble with sending files in a PHP AJAX Request: Here is the form code: <form class="frmContact" action="#" method="post"> <div class="col-md-6"> <input type="hidden" name="emailTo" id= ...

Click on the links to view various captions for a single image, one at a time

My goal is to create an interactive image similar to what can be found at . (Click Play and navigate to page 5 to see the interactive physical exam). While this example seems to use Flash, I am interested in achieving a similar effect using javascript/jQue ...

Error alert: The function is declared but appears as undefined

Below is the javascript function I created: <script type="text/javascript"> function rate_prof(opcode, prof_id) { $.ajax({ alert('Got an error dude'); type: "POST", url: "/caller/", data: { ...

Processing JSON data from an array in PHP

Currently, my code involves utilizing an AJAX request to retrieve data from a database dynamically. The data received is processed by the PHP function json_encode() before being sent back to AJAX. Upon receiving the data through the AJAX request, it is for ...

What is the best way to display multiple files in a vertical stack when choosing a file?

<div class="selected-file-container align-items-center justify-content-between d-none"> <div class="selected-file d-flex align-items-center"> <img id="selectedImage" class="hidden" src="&qu ...

Execute PHP function by clicking on an HTML anchor tag

I'm curious - can a PHP function be triggered simply by clicking a link, or is it better to stick with the form submit method? If linking works, where should the reference point to? Check out this code snippet: <?php session_start(); func ...

Disabling the smooth scrolling feature on tab navigation

I am using smooth scroll JS to navigate from a menu item to an anchor located further down the page. However, I am encountering an issue where my tabs (which utilize #tabname) also trigger the scroll behavior when clicked on. Is there a simple modificati ...

Live search bar feature with jQuery更新

I am currently working on creating a dynamic search bar that updates a list of items from the database based on the input value. Below is the code I have developed for this search bar: $(document).ready(function(){ $('#search').keyup(function ...

Switch from using pure JavaScript to jQuery or back

While I can navigate through jquery, javascript is a bit more challenging for me. I have a simple function that's coded in javascript, but I need to update the selectors. Changing them in jquery wouldn't be a problem for me, but it's tricki ...

What is the reason behind the failure of this ajax request?

I have encountered an issue with my ajax code. It seems to work in one file, but fails to function properly in other files. Below is the code snippet in question: $.ajax({ type: "GET", url: "get_child.php?pc_id="+pc_id.toString(), }).done(fun ...

Dynamically create a button using jQuery and JSON based on specific conditions (either true or false)

Is there a way to generate buttons with specified parameters only if a condition is true? Currently, the buttons are generated without checking conditions. Any help with the correct syntax would be appreciated. Sample JSON code: { "Caption": "Module ...

Dynamic Column Image and Text Display

I am working on a website where I am trying to have an image on the left and a paragraph on the right, but when the screen size is reduced, I need the layout to switch so that the image is at the top and the paragraph is at the bottom. Even though I have ...

Combine the bootstrap button and dropdown menu side by side and adjust the width of the dropdown to be

Snippet: <div class="d-grid gap-2 d-md-block mt-1"> <button class="btn btn-secondary" id="btnSearch"><i class="fa fa-search fa-fw"></i>Search</button> <button class="btn ...

Sending an array with a specific identifier through JQuery ajax

I'm facing an issue where I am sending an array of values via jQuery AJAX to my servlet. However, the servlet is only picking up the first value in the array, even though there are more elements present. $.ajax({ type: "POST", url: "mySer ...

Is it possible to generate an HTML element by utilizing an array of coordinates?

I have a set of 4 x/y coordinates that looks like this: [{x: 10, y: 5}, {x:10, y:15}, {x:20, y:10}, {x:20, y:20}] Is there a way to create an HTML element where each corner matches one of the coordinates in the array? I am aware that this can be done usi ...

Setting maximum and minimum zoom limits for an element ID using JavaScript or jQuery

My application features a DIV element with the unique identifier of mainDiv. The issue I am facing is related to zooming functionality, as it currently lacks any set limits - both for scaling up and scaling down. I have been searching on Google for a sol ...

The functionality of jQuery plugins does not seem to extend to content loaded via Ajax

Hello everyone, I have encountered a frustrating issue. I am currently utilizing bootstrap 2.3 along with some jQuery plugins for form styling and other purposes. However, I've noticed that when I load ajax content, particularly select elements, the ...

jquery logic for iterating through all elements in a select menu encountering issues

In search of a solution to iterate through all options in a dropdown list using code, comparing each to a variable. When a match is found, I aim to set that particular value as the selected item in the dropdown and then exit the loop. Here's what I&ap ...