Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function initializeTable() {
- $('#createLink').on('click', onCreateLinkClicked);
- function onCreateLinkClicked(event) {
- const inputCountry = $('#newCountryText');
- const inputCapital = $('#newCapitalText');
- const country = inputCountry.val();
- const capital = inputCapital.val();
- inputCountry.val('');
- inputCapital.val('');
- const elementCountry = $('<td></td>').text(country);
- const elementCapital = $('<td></td>').text(capital);
- const elementLinks = $('<td></td>');
- $createATag('[Up]', onUpClicked, elementLinks);
- $createATag('[Down]', onDownClicked, elementLinks);
- $createATag('[Delete]', onDeleteClicked, elementLinks);
- const elementRow = $('<tr></tr>')
- .append(elementCountry)
- .append(elementCapital)
- .append(elementLinks);
- $('#countriesTable').append(elementRow);
- $fixLinks();
- }
- function $createATag(text, eventHandler, appendTo) {
- $('<a href="#"></a>')
- .text(text)
- .on('click', eventHandler)
- .appendTo(appendTo);
- }
- function onUpClicked(event) {
- const targetRow = $getParentRow(event.target);
- targetRow.fadeOut(function () {
- targetRow.prev().insertAfter(targetRow);
- $fixLinks();
- targetRow.fadeIn();
- });
- }
- function onDownClicked(event) {
- const targetRow = $getParentRow(event.target);
- targetRow.fadeOut(function () {
- targetRow.next().insertBefore(targetRow);
- $fixLinks();
- targetRow.fadeIn();
- });
- }
- function onDeleteClicked(event) {
- const targetRow = $getParentRow(event.target);
- targetRow.fadeOut(function () {
- targetRow.remove();
- $fixLinks();
- });
- }
- function $getParentRow(element) {
- return $(element)
- .parent()
- .parent();
- }
- function $fixLinks() {
- $('#countriesTable a').css('display', 'inline');
- $('#countriesTable tr:nth-child(3) a:contains("Up")').css('display', 'none');
- $('#countriesTable tr:last-child a:contains("Down")').css('display', 'none');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement