GithubHelp home page GithubHelp logo

sutara79 / jquery.add-input-area Goto Github PK

View Code? Open in Web Editor NEW
49.0 10.0 25.0 909 KB

jQuery plug-in. You can add or delete the list of input area.

Home Page: https://sutara79.github.io/jquery.add-input-area/

JavaScript 49.97% HTML 50.03%
jquery-plugin html-form

jquery.add-input-area's Introduction

jquery.add-input-area

npm version Build Status

jQuery plugin to add or delete form fields.

Sample image

Demo

https://sutara79.github.io/jquery.add-input-area/

Install

Usage

HTML
<ol id="list"><!-- "id" attribute is required -->
  <li class="list_var"><!-- .(id)_var -->
    <input type="text" name="list_0" id="list_0">
    <button class="list_del">Delete</button><!-- .(id)_del -->
  </li>
</ol>
<input type="button" value="Add" class="list_add"><!-- .(id)_add -->

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="jquery.add-input-area.min.js"></script>
JavaScript
$('#list').addInputArea();

Naming convention

  • CSS class for the wrapper of form fields: .(id)_var
  • CSS class for del-button: .(id)_del
  • CSS class for add-button: .(id)_add

You can change these rules above by options. (area_var, btn_add, btn_del)

  • Form field: (a text)_0

If you follow this rule, "name", "id" and "for" attribute are numbered automatically.

(default)
<input type="text" name="foo_0">

(add)
<input type="text" name="foo_1">
<input type="text" name="foo_2">
<input type="text" name="foo_3">

You can use original naming convention by setting custom data attribute, "data-name-format" or "data-id-format".

(default)
<input type="text" data-name-format="foo_%d_bar" name="foo_0_bar">

(add)
<input type="text" data-name-format="foo_%d_bar" name="foo_1_bar">
<input type="text" data-name-format="foo_%d_bar" name="foo_2_bar">
(default)
<input type="text" data-name-format="data[posts][mail][%d]" name="data[posts][mail][0]">

(add)
<input type="text" data-name-format="data[posts][mail][%d]" name="data[posts][mail][1]">
<input type="text" data-name-format="data[posts][mail][%d]" name="data[posts][mail][2]">

Options

Name Type Default Description
area_var string '.(id)_var' CSS class for the wrapper of form fields.
btn_add string '.(id)_add' CSS class for add button.
btn_del string '.(id)_del' CSS class for delete button.
area_del string (btn_del) CSS class for wrapper of delete button.
after_add function null A callback function to be called after adding.
clone_event boolean true If event will be copied or not.
maximum number 0 Max number of wrapper of form fields. "0" means unlimited.
dont_clone string null CSS selector to avoid cloning.
validate function null Validation before adding. Should return boolean. If false, adding stops.

License

MIT

Author

Yuusaku Miyazaki ( [email protected] )

jquery.add-input-area's People

Contributors

bignumorg avatar lukasdrgon avatar sutara79 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jquery.add-input-area's Issues

Prevent alert on pressing enter

First, thank you for sharing this, real time saver:)

Far from jQuery script guru... so please forgive me if missing simple solution. Having problem removing the popup alert that appears when user presses "Enter" while in input area.

Ideally another row of input is added, but simply removing the alert is good enough, If it helps, using based off the "Not to initialize".

Thank you!

Eng comments

Hi!
Can u share version with comments on eng? I just learn JS and JQuery, comments are very useful.

Multiple tag

Hi, this script awesome. How about for using addInputArea() in multiple tag in one page ?

Bootstrap prevents correct behavior if there's a span inside the delete button

If Bootstrap CSS is used, AND a span is used inside the delete button tag (for instance to use an icon on the button), then all delete buttons always remove the last input added. Expected behavior is to remove the input associated with that specific delete button.

Here is a codepen showing with and without spans on the buttons. Remove Bootstrap from the pen to see that both examples work without Bootstrap.

codepen

Not a huge deal as there are plenty of good options to replace icon fonts on buttons, or replace Bootstrap for that matter. I just found it and figured I should make a record in case it helps someone else who is struggling with this.

And also ... thank you for this incredibly handy plugin! :)

Don't clone some elements at startup

I needed a way to preview already uploaded files (with <input type=file>), but obviously omit the preview tags for the new elements created when adding a row of new inputs. In order to do that, I added a dont-clone class to those elements, triggering detach() (see lines 59-60 below). I didn't add configuration settings for that though...

 57   // Save original
 58   this.option.original = $(this.elem).find(this.option.area_var).eq(0).clone(this.option.clone_event);
 59   // Remove dont-clone elements
 60   $(this.option.original).find('.dont-clone').detach();

edit mode

Hello, @sutara79 !

Thx for add-input-area plugin!
What would be the best way to use it in the edit mode ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.