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 7920115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:05:46+00:00 2026-06-03T16:05:46+00:00

This is my all script, I know this is long, but just one line

  • 0

This is my all script, I know this is long, but just one line is important and I add all it for insurance:

//Add new Addable div
$('.AddNewE').click(function () {

    var RemoveAddableButton = $('<input type="button" class="RemoveE button red" value="remove" />');

    $(RemoveAddableButton).click(function () {
        $(this).closest('.Addable').remove();
    });


    var TargetId = $(this).attr('id');
    TargetId = TargetId.substring(3);
    var Target = $('.Addable#' + TargetId + ':first');
    var Count = $('.Addable#' + TargetId).size();
    var CloneTarget = $(Target).clone();
    CloneTarget.find('input').val('');
    CloneTarget.insertAfter('.Addable#' + TargetId + ':last'); // ***importantOne
    var TargetName = $(Target).find('input').attr('name');


    if (Count == 1) {

        var CloneName = TargetName + '[1]';
        TargetName = TargetName + '[0]';

        $(Target).find('input').attr('name', TargetName);
        $(Target).find('span[class*="field-validation"]').attr('data-valmsg-for', TargetName);
        $(CloneTarget).find('input').attr('name', CloneName);
        $(CloneTarget).append($(RemoveAddableButton));

        if ($(CloneTarget).find('span[class*="field-validation"]').size() > 0) {
            $(CloneTarget).find('span[class*="field-validation"]').remove();
            $(CloneTarget).append(
                        $('<span class="field-validation-valid invalid-side-note" data-valmsg-replace="true" data-valmsg-for="' + CloneName + '"></span>')
            );
        }


    } else {

        var indx = TargetName.length - 3;
        var CloneTargetName = TargetName.substring(0, indx);
        CloneTargetName = CloneTargetName + '[' + Count + ']';
        $(CloneTarget).find('input').attr('name', CloneTargetName);
        $(CloneTarget).append($(RemoveAddableButton));

        if ($(CloneTarget).find('span[class*="field-validation"]').size() > 0) {

            $(CloneTarget).find('span[class*="field-validation"]').remove();
            $(CloneTarget).append(
                        $('<span class="field-validation-valid invalid-side-note" data-valmsg-replace="true" data-valmsg-for="' + CloneTargetName + '"></span>')
            );

        }

    }



    (function ($) {
        $.fn.updateValidation = function () {
            var form = this.closest("form").removeData("validator").removeData("unobtrusiveValidation");
            $.validator.unobtrusive.parse(form);
            return this;
        };
    })(jQuery);

    $(Target).updateValidation();
    $(CloneTarget).updateValidation();



});

If I click the .AddNewE button then a new div added, but as my script I want to add this new div to the end of the divs so I use

CloneTarget.insertAfter('.Addable#' + TargetId + ':last');

but always the new div added as a second div it means always the :first and :last div is same and is first one also I checked by:

$('.Addable#' + TargetId).css('border', '');
    $('.Addable#' + TargetId + ':last').css('border', '3px dotted green');
    $('.Addable#' + TargetId + ':first').css('border', '3px dotted red');

So where is the problem? why the jQuery can’t recognize last div ?

  • 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-06-03T16:05:49+00:00Added an answer on June 3, 2026 at 4:05 pm

    The problem is in the jQuery selector: $(‘.Addable#’ + TargetId + ‘:last’)

    It is not valid HTML when you have multiple elements with the same id (#TargetId). ID is unique and you’re not supposed to have more than 1 element with the same ID.

    The jQuery selector assumes you use valid correct HTML markups, so it doesn’t bother to collect all your elements with that ID. As soon as jQuery found the first element in the DOM with that ID, it stops and appends your new element right after that.

    Try updating your jQuery selectors to simply: $(‘.Addable:first’) and $(‘.Addable:last’) and see if it works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At the beginning this worked fine: $ rake cucumber:all But then $ script/plugin install
I know this is a long shot because it is a large script and
how can I make this jquery script close all previously opened children when entering
We currently have an error catching script for all our PHP sites. This script
This is my JavaScript code: <script> function change(name){ var element = document.all.name.value; } </script>
I have this code in a .html.erb file: <script src=http://connect.facebook.net/en_US/all.js#xfbml=1></script> <script> FB.Event.subscribe('edge.create', function(response) {
I know the title isn't informative at all, but I had no idea how
I know some people may just respond never as long as there's user input.
I know this has been done before, but I am running into an issue
The dillema is like this: If I try to fit all the scripts blocks

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.