Display all elements that come before and after the current element

For debugging purposes, I need to output the IDs of all previous images in a check. The issue arises when trying to assign classes to all previous elements using the script below, as the images are not being added to the correct classes.

$(document).on('click', 'img', function() {
   var t=$(this);
   t.prevAll().removeClass('t2').addClass('t1');
   t.nextAll().removeClass('t1').addClass('t2');

To see an example, visit this jsFiddle. When clicking on any image, my goal is to assign the previous images to one class and the next image to another class.

Answer №1

One way to achieve this is by utilizing the .map function in jQuery.

Here's an example that demonstrates how to obtain a list of checkbox IDs separated by commas:

var numbers = $(':checkbox').map(function() {
      return this.id;
}).get().join();

alert(numbers);

As a result, you will receive the string, "two,four,six,eight".

Answer №2

The img tags currently do not have any id attributes assigned to them. However, assuming they will have id attributes in the future, you can utilize the map method to retrieve these IDs:

var ids = t.prevAll().removeClass('t2').addClass('t1').map(function(){
      return this.id;
}).get().join();

console.log(ids);

It appears that the images are not being assigned to their proper classes.

If you also want to add a class to the clicked element, you can make use of the addBack method:

t.nextAll().addBack().removeClass('t1').addClass('t2');

http://example.com/jsfiddle

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

How can I use AngularJS to filter ng-repeat values based on the input ng-model value of another field?

Can I Repeat Values Based on Another Input NG-Model Value in AngularJS? There is one input value, let's say Social Psychology, and the corresponding ng-model for this value is kpcategory. I am using ng-repeat="question in questions" to list all ...

Vertically Center Image in a Div

I have been struggling to center an image within a div, but all the usual methods like using margin: auto; do not seem to work. I have tried various popular techniques with no success. <style> /* Add a black background color to the top navigation ...

Using Jquery: Knowing when to utilize "this" and when to use "$(this)"

Similar Question: jQuery $(this) vs this Can you explain the distinction between "this" and "$(this)"? How does one determine when to use each? Relatedly, I am curious about: In relation to 'each', there are optional parameters. What set ...

Generating dynamic input field values using jQuery in a CodeIgniter PHP framework application

I am facing an issue where I am unable to display the value from a dynamically created input field in the page controller. Below is the jQuery code used to append the dynamic input fields: var tableRow='<tr>'; tableRow+=' ...

Mobile devices not showing the Navbar bootstrap menu

My header.php works perfectly on desktop, but the navigation bar is not displaying properly on mobile devices. Here is my code: <!DOCTYPE html> <html> <head> <title>International Pvt. Ltd.</title> <meta name="viewpo ...

Using Rails and Stripe: Sending out a form and effectively managing errors with AJAX

Lately, I've been struggling with integrating Stripe into my platform. One of the major issues I'm facing is setting up a subscription using AJAX, which I haven't quite cracked yet. On top of that, I need to ensure the credit card informatio ...

I updated the color of my a:link using jQuery, but now my a:hover is not functioning as expected

Due to the readability issues of the navigation color against a specific image, I am looking to modify it for a particular page. Below is the HTML code: <div id='nav'> <ul> <li id='navBiog'> ...

Using jQuery's .html() method will remove the selected attribute from the dropdown menu option

When generating HTML dynamically, a select element is included. After choosing an option inside it, everything works fine. For example: <div id="test"> <select> <option value="1">1</option> <option value="2" selec ...

Tips for automatically collapsing the Bootstrap 4 Sidebar on smaller devices

I am currently working on some code and have successfully hidden the sidebar using a collapse button. However, I am looking to make it collapse only for small devices, similar to how the bootstrap navbar functions. <link href="https://stackpath.boots ...

Reduce the dimensions of a CSS attribute automatically

Displayed below is the image with a width of 192 and height of 109: <a href="https://www.blogger.com/u/1/blogger.g?blogID=4402911157743442948#"><img alt="" class="thumbnail" height="109" src="https://2.bp.blogspot.com/-E5ftfbCCgkw/XjnLpO347cI/AAA ...

Expression fragment in Thymeleaf

In splitting my templates into head/main/footer parts using thymeleaf, I have found a method to include stylesheets and javascript on certain pages while excluding them from others. This is achieved through the use of fragment expressions outlined here. M ...

Issue with Angular / SCSS where animation is not being executed on the specified element

I have created a post component with a comment section. However, when I click on the comments, I want them to display smoothly with a nice animation effect. Even though I have added the necessary code for animations, it doesn't seem to work as expecte ...

Struggling to accurately capture the values from checkboxes and dropdown selections to ensure the correct data is displayed. Assistance is needed in this

I am facing challenges in retrieving the accurate data for display from Mat-Select and Mat-Checkbox components. My goal is to capture the selected values from users and perform if-else statements to validate conditions, displaying the correct data view if ...

Issue with grid element not properly scaling using the transform:scale property

I have encountered an issue where a simple grid, with the gaps set to 0, displays a small line when I apply the transform:scale(1.5) css property. I am unsure if this is a bug or if I am doing something incorrectly. Interestingly, the result in Firefox dif ...

Error Encountered in jQuery UI SelectMenu

Struggling to integrate the jQuery UI SelectMenu, I keep encountering the same error in the browser console. Below is the HTML Code: <select name="files" id="files"> <optgroup label="Scripts"> <option value="jquery">jQuery.js</ ...

evolving the design of mui stepper

I am looking to customize the code below for my specific needs I want to change the icon from to this: I am unable to modify the style and also need to add an intermediate step I have included , '.Mui-active': { color: '#1C6FC9', }, ...

Finding the current HTML page URL in Ionic 2: A step-by-step guide

Can the URL of the current HTML page be obtained and then converted to PDF using the cordova-pdf-generator package? ...

Select a specific element to apply a CSS selector for a button

One of the challenges I am facing involves a webpage with multiple submit buttons. My goal is to iterate through each button and click on them one by one. While I am aware that this can be achieved using xpath like this (//button[@class='submit' ...

Identifying a change in the source location of an iframe element

I am working with an iframe object that is currently set to a specific page's URL. Here is an example: <iframe src="http://en.wikipedia.org/wiki/Special:Random"></iframe> My goal is to display an alert whenever the location of the iframe ...

Refresh the data using the Ajax function

I am attempting to implement a delete function using Ajax. function delCatItem(catitem){ var theitem = catitem; $.ajax({ url: "movie/deleteitem/", type: "POST", data: { "movieid" : catitem ...