Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7512919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:01:03+00:00 2026-05-30T00:01:03+00:00

I have the following jQuery: // get position on new/removed items function get_new_position_for_all_items() {

  • 0

I have the following jQuery:

// get position on new/removed items
function get_new_position_for_all_items() {
    $("li.sortable_shop_item input.sortable_shop_item_position").each(function(index) {
    $(this).val(index + 1);
  });
}

// remove shop_state_fields
function remove_fields(link) {
  $(link).prev("input.destroy_field").val("1");
    $(link).prev(".sortable_shop_item_position").attr('class', 'not_sortable');
  $(link).closest(".fields").hide("slow");
    get_new_position_for_all_items();
}

When I click the remove_fields() link, it will change the value of input.destroy_field which is initially 0. Then it will hide the element with the closest .fields and calls the function get_new_position_for_all_items().

The problem I am having is that I can’t change the class of the nearest .sortable_shop_item_position to something else, so that when get_new_position_for_all_items() this field that has been removed will not be included in the sorting of new position.

I have tried doing something simple such as changing the value of .sortable_shop_item_position, but I just can’t seem to have jQuery to identify and select it.

Note that there are several .sortable_shop_item_position which are wrapped in li.

Please advise. Many thanks.

[UPDATE 1]
Here’s some rough HTML:

<li class="fields sortable_shop_item">
<p style="background-color: yellow; margin: 10px">
<span class="handle">[drag]</span>
<br>
<input id="country_shops_attributes_1_country_date" type="text" value="2012-02-02" size="30" name="country[shops_attributes][1][country_date]">
<label for="country_shops_attributes_1_country_date">country Date</label>
<br>
<input id="country_shops_attributes_1_position" class="sortable_shop_item_position" type="text" value="3" size="30" name="country[shops_attributes][1][position]">
<label for="country_shops_attributes_1_position">Position</label>
<br>
<input id="country_shops_attributes_1__destroy" class="destroy_field" type="hidden" value="false" name="country[shops_attributes][1][_destroy]">
<a onclick="remove_fields(this); return false;" href="#">remove</a>
</p>
<li class="fields sortable_shop_item">
<p style="background-color: yellow; margin: 10px">
<span class="handle">[drag]</span>
<br>
<input id="country_shops_attributes_2_country_date" type="text" value="2012-02-02" size="30" name="country[shops_attributes][2][country_date]">
<label for="country_shops_attributes_2_country_date">country Date</label>
<br>
<input id="country_shops_attributes_2_position" class="sortable_shop_item_position" type="text" value="3" size="30" name="country[shops_attributes][2][position]">
<label for="country_shops_attributes_2_position">Position</label>
<br>
<input id="country_shops_attributes_2__destroy" class="destroy_field" type="hidden" value="false" name="country[shops_attributes][2][_destroy]">
<a onclick="remove_fields(this); return false;" href="#">remove</a>
</p>
<li class="fields sortable_shop_item">
<p style="background-color: yellow; margin: 10px">
<span class="handle">[drag]</span>
<br>
<input id="country_shops_attributes_3_country_date" type="text" value="2012-02-02" size="30" name="country[shops_attributes][3][country_date]">
<label for="country_shops_attributes_3_country_date">country Date</label>
<br>
<input id="country_shops_attributes_3_position" class="sortable_shop_item_position" type="text" value="3" size="30" name="country[shops_attributes][3][position]">
<label for="country_shops_attributes_3_position">Position</label>
<br>
<input id="country_shops_attributes_3__destroy" class="destroy_field" type="hidden" value="false" name="country[shops_attributes][3][_destroy]">
<a onclick="remove_fields(this); return false;" href="#">remove</a>
</p>

You will notice each <li> is almost repeated, but with different in its sub-element. When I remove the <li> with .sortable_shop_item_position valued 2, I want to replace this .sortable_shop_item_position with .not_sortable so that this class will be skipped when re-sorting.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:01:05+00:00Added an answer on May 30, 2026 at 12:01 am

    $.prev gets the immediately preceding sibling of each element http://api.jquery.com/prev/

    Try $.prevAll for the second one, although you may have to use a more specific selector if you have multiple .sortable_shop_item_position elements. http://api.jquery.com/prevAll/

    Alternatively, if your DOM structure is:

    sortable_shop_item_position
    destroy_field
    link
    

    then you can use

    $(link).prev().prev(".sortable_shop_item_position").attr('class', 'not_sortable');
    

    Also, for improved performance, pass link into that function as a jQuery DOM object bind remove_links to the elements and use $(this) i.e. $('a.classname').click(function() { /* contents of remove_links function */ });, rather than just this. Each time you call $(link), jQuery has to find that element in the DOM again. If you call the function using remove_fields($('#linkidselector')), then you just need to use link.prev(). Or just var el = $(link), then use el.prev();

    Further, you could also chain that entire command:

    $(link).prev("input.destroy_field").val("1").prev(".sortable_shop_item_position")
    .attr('class', 'not_sortable').end()
    .closest(".fields").hide("slow");
    

    UPDATE for newly provided HTML structure: Example: http://jsfiddle.net/HSv7m/

    Using prevAll will be fine because the other element you want to modify is a sibling of the link element. The other .sortable_shop_item_position elements are under a different parent LI element, so prevAll won’t access them when it shouldn’t.

    $(link).prev("input.destroy_field").val("1").prevAll(".sortable_shop_item_position")
    .attr('class', 'not_sortable').end()
    .closest(".fields").hide("slow");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have following in jQuery $(#someId).add(#someOtherId).bind(click, function(e) {... I get the click bound
I have the following jQuery: $(document).ready(function () { $(.element).draggable(); $(.element).droppable({ drop: function () {
I have the following jquery code: jQuery(function(){ jQuery(select#rooms).change(function(){ var options = ''; jQuery.getJSON(/admin/selection.php,{id: jQuery(this).val(),
I have the following jquery function for filtering the contents of a listbox on
I have the following JQuery code that processes a GET request upon page load:
I have the following html (i'm also using Jquery and JqueryUI) <div style=display:none; position:fixed;
I'm having problems using ajax with jQuery... I have the following jscript: $(document).ready(function() {
I have the following jQuery: $(this).closest('div.mcoup').find('div.delcoup').slideToggle(400) .siblings().children('div.delcoup').slideUp(400); What I'm trying to do is get
i have the following jquery code. basically i will have several overlapped divs and
I have the following JQuery code that worked perfect in C#/Asp.net 2.0 to call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.