Advertisement
deyanivanov966

Js Event Task Scrypt

Jan 6th, 2022
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function () {
  2.     $("#add").click(function () {
  3.         var lastField = $("#buildyourform div:last");
  4.         var intId = (lastField && lastField.length && lastField.data("idx") + 1) || 1;
  5.         var fieldWrapper = $("<div class=\"fieldwrapper\" id=\"field" + intId + "\"/>");
  6.         fieldWrapper.data("idx", intId);
  7.         var findex = "<span class=\"span-number\">" + intId + "</span>";
  8.         var fName = $("<span class=\"span-field\"><label for=\"inputSubject\">Subject</label><input type=\"text\" onkeypress=\"return /[a-z]/i.test(event.key)\" class=\"form-control field-subject\" id=\"inputSubject\"/><span>");
  9.         var removeButton = $("<span class=\"span-btn\"><button id=\"btn-remove\" type=\"button\" class=\"remove btn btn-danger btn-secondary\">x</button><span id=\"hovertext\">remove</span></span>");
  10.         removeButton.click(function () {
  11.             $(this).parent().remove();
  12.         });
  13.         fieldWrapper.append(findex);
  14.         fieldWrapper.append(fName);
  15.         fieldWrapper.append(removeButton);
  16.         $("#buildyourform").append(fieldWrapper);
  17.     });
  18.     $("#preview").click(function () {
  19.         $("#yourform").remove();
  20.         var fieldSet = $("<fieldset id=\"yourform\"><legend><span class=\"table-first-column\">#</span>Subject</legend></fieldset>");
  21.         $("#buildyourform div").each(function () {
  22.             var id = $(this).attr("id").replace("field", "");
  23.             var label = $("<label for=\"" + id + "\"><span class=\"table-first-column\">" + id + "</span>" + $(this).find("input.form-control").first().val() + "</label>");
  24.             var input = $(this).find("input.form-control").first().val();
  25.             if (input.length > 0) {
  26.                 fieldSet.append(label);
  27.             }
  28.         });
  29.         $("body").append(fieldSet);
  30.     });
  31.     $("#empty").click(function () {
  32.         $("#yourform").remove();
  33.         var fieldSet = $("<fieldset id=\"yourform\"><legend><span class=\"table-first-column\">#</span>Subject</legend></fieldset>");
  34.         $("#buildyourform div").each(function () {
  35.             var id = $(this).attr("id").replace("field", "");
  36.             var label = $("<label for=\"" + id + "\"><span class=\"table-first-column\">" + id + "</span>" + $(this).find("input.form-control").first().val() + "</label>");
  37.             var input = $(this).find("input.form-control").first().val();
  38.             if (input.length == 0) {
  39.                 delete id;
  40.             }
  41.         });
  42.     });
  43. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement