Skip to content

[Cookbook/Form] Fixed jQuery bug #2158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 20, 2013
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions cookbook/form/form_collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ will be show next):
// add the "add a tag" anchor and li to the tags ul
collectionHolder.append($newLinkLi);

// count the current form inputs we have (e.g. 2), use that as the new
// index when inserting a new item (e.g. 2)
collectionHolder.data('index', collectionHolder.find(':input').length);

$addTagLink.on('click', function(e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();
Expand All @@ -391,21 +395,24 @@ one example:

function addTagForm(collectionHolder, $newLinkLi) {
// Get the data-prototype explained earlier
var prototype = collectionHolder.attr('data-prototype');
var prototype = collectionHolder.data('prototype');

// count the current form inputs we have (e.g. 2), use that as the new index (e.g. 2)
var newIndex = collectionHolder.find(':input').length;
// get the new index
var index = collectionHolder.data('index');

// Replace '$$name$$' in the prototype's HTML to
// instead be a number based on how many items we have
var newForm = prototype.replace(/\$\$name\$\$/g, newIndex);
var newForm = prototype.replace(/\$\$name\$\$/g, index);

// increase the index with one for the next item
collectionHolder.attr('index', index + 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be .data, not .attr

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stof good catch!


// Display the form in the page in an li, before the "Add a tag" link li
var $newFormLi = $('<li></li>').append(newForm);
$newLinkLi.before($newFormLi);
}

.. note:
.. note::

It is better to separate your javascript in real JavaScript files than
to write it inside the HTML as is done here.
Expand Down