Skip to content

Commit fecf841

Browse files
committed
minor #9892 Use <button> instead of <a> for JavaScript interactive buttons (Calinou)
This PR was merged into the 2.8 branch. Discussion ---------- Use <button> instead of <a> for JavaScript interactive buttons This removes the need to call `preventDefault()` to disable the default action of `<a>` elements, since `<button type="button">` has no default action whatsoever. See also [this Stack Overflow question](https://stackoverflow.com/questions/469059/button-vs-input-type-button-which-to-use) about the `<button>` HTML tag. Commits ------- 87db1b6 Use <button> instead of <a> for JavaScript interactive buttons
2 parents 0dbb158 + 87db1b6 commit fecf841

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

form/form_collections.rst

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,8 @@ will be show next):
353353
var $collectionHolder;
354354
355355
// setup an "add a tag" link
356-
var $addTagLink = $('<a href="#" class="add_tag_link">Add a tag</a>');
357-
var $newLinkLi = $('<li></li>').append($addTagLink);
356+
var $addTagButton = $('<button type="button" class="add_tag_link">Add a tag</a>');
357+
var $newLinkLi = $('<li></li>').append($addTagButton);
358358
359359
jQuery(document).ready(function() {
360360
// Get the ul that holds the collection of tags
@@ -367,10 +367,7 @@ will be show next):
367367
// index when inserting a new item (e.g. 2)
368368
$collectionHolder.data('index', $collectionHolder.find(':input').length);
369369
370-
$addTagLink.on('click', function(e) {
371-
// prevent the link from creating a "#" on the URL
372-
e.preventDefault();
373-
370+
$addTagButton.on('click', function(e) {
374371
// add a new tag form (see next code block)
375372
addTagForm($collectionHolder, $newLinkLi);
376373
});
@@ -619,7 +616,7 @@ Template Modifications
619616

620617
The ``allow_delete`` option means that if an item of a collection
621618
isn't sent on submission, the related data is removed from the collection
622-
on the server. In order for this to work in an HTML form, you must remove
619+
on the server. In order for this to work in an HTML form, you must remove
623620
the DOM element for the collection item to be removed, before submitting
624621
the form.
625622

@@ -651,13 +648,10 @@ The ``addTagFormDeleteLink()`` function will look something like this:
651648
.. code-block:: javascript
652649
653650
function addTagFormDeleteLink($tagFormLi) {
654-
var $removeFormA = $('<a href="#">delete this tag</a>');
655-
$tagFormLi.append($removeFormA);
656-
657-
$removeFormA.on('click', function(e) {
658-
// prevent the link from creating a "#" on the URL
659-
e.preventDefault();
651+
var $removeFormButton = $('<button type="button">Delete this tag</a>');
652+
$tagFormLi.append($removeFormButton);
660653
654+
$removeFormButton.on('click', function(e) {
661655
// remove the li for the tag form
662656
$tagFormLi.remove();
663657
});

reference/forms/types/collection.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ you need is this JavaScript code:
168168
// add-collection-widget.js
169169
jQuery(document).ready(function () {
170170
jQuery('.add-another-collection-widget').click(function (e) {
171-
e.preventDefault();
172171
var list = jQuery(jQuery(this).attr('data-list'));
173172
// Try to find the counter of the list
174173
var counter = list.data('widget-counter') | list.children().length;
@@ -211,7 +210,7 @@ And update the template as follows:
211210
{% endfor %}
212211
</ul>
213212
214-
<a href="#"
213+
<button type="button"
215214
class="add-another-collection-widget"
216215
data-list="#email-fields-list">Add another email</a>
217216

0 commit comments

Comments
 (0)