JavaScript Using the same jQuery functions but causing conflicts in HTML

Title - Sorry about the title, it was difficult for me to actually explain this.

So I recently finished working on a dynamic fields system using jQuery. This is all working great however I'm wanting to re-use the html for the system over and over again on the same page, this causes problems.

Problem - When you have duplicates of the form on the same page, and you press 'Add Field' it will run the function and apply the functions to the other classes on the page. (See fiddle for example.)

When you just have one form on the DOM it works fine, but I'm wanting to alter the html slightly so I can use it for different scenarios on a page. I don't want to have separate jQuery files to do this because I don't think it's necessary. I was thinking maybe I could target it's parent containers instead of the class directly? Then I could recycle the same code maybe?

Any suggestions on this guys?

HTML:

<form action="javascript:void(0);" method="POST" autocomplete="off">
    <button class="add">Add Field</button>
    <div class='input_line'>
        <input type="text" name="input_0" placeholder="Input1">
        <input type="button" class="duplicate" value="duplicate">
        <input type="button" class="remove" value="remove">
    </div>
</form>

JQUERY:

$(document).ready(function () {
    'use strict';
    var input = 1,
        blank_line = $('.input_line'),
        removing = false;

    $('.remove').hide();

    $('.add').click(function () {
        var newElement = blank_line.clone(true).hide();
        $('form').append(newElement);
        $(newElement).slideDown();
        $('.remove').show();
    });

    $('form').on('click', '.duplicate', function () {
        $(this).parent().clone().hide().insertAfter($(this).parent().after()).slideDown();
        $('.input_line').last().before($('.add'));
        $('.remove').show();
        input = input + 1;
    });

    $('form').on('click', '.remove', function () {
        if (removing) {
            return;
        } else {
            if ($('.input_line').length <= 2) {
                $('.remove').hide();
            }
            $(this).parent().slideUp(function () {
                $(this).remove();
                removing = false;
            });
            $('.input_line').last().before($('.add'));
            input = input - 1;
        }
        removing = true;
    });
});

Working fiddle - JSFiddle

Problem fiddle - JSFiddle

As you can see in the problem fiddle above, when you duplicate the form it start conflicting. I would like each form to work independently.

Any help would be greatly appreciated!

Answer:1

I was wondering if there was a feature or a way to select on line at a time from jquery/javascript. The issues is that it must be the element inside a <pre></pre> tags. The code look like ...

I was wondering if there was a feature or a way to select on line at a time from jquery/javascript. The issues is that it must be the element inside a <pre></pre> tags. The code look like ...

I Have four grids in Dojo, all of which update with real time data every few seconds. I only display one grid to the user at a time, but in the background all four grids update. This makes the page ...

I Have four grids in Dojo, all of which update with real time data every few seconds. I only display one grid to the user at a time, but in the background all four grids update. This makes the page ...

I am trying to dynamically create a simple select which contains an object's property as the option based on some constraints. Everything works fine when my JSON is a part of the script. FIDDLE The ...

I am trying to dynamically create a simple select which contains an object's property as the option based on some constraints. Everything works fine when my JSON is a part of the script. FIDDLE The ...

I use rails 4. The problem that I have is when I follow the links in my rails app - some js libraries doesnt't work(jquery.inview.js , ckeditor/init). When I refresh page - everything works just fine. ...

I use rails 4. The problem that I have is when I follow the links in my rails app - some js libraries doesnt't work(jquery.inview.js , ckeditor/init). When I refresh page - everything works just fine. ...

  1. rails doesn't scale
  2. rails table doesn't exist
  3. rails c doesn't work
  4. rails server doesn't work
  5. rails console doesn't start
  6. rails object doesn't support #inspect
  7. rails s doesn't work
  8. rails console doesn't work
  9. rails new doesn't work
  10. rails server doesn't start
  11. rails includes doesn't work
  12. rails save doesn't work
  13. rails generate doesn't work
  14. rails destroy doesn't work
  15. rails javascript doesn't work
  16. rails command doesn't work
  17. rails ajax doesnt work
  18. rails permit doesn't work
  19. rails ckeditor doesn't work
  20. rails uniq doesn't work