Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var newOptionButton = '<button type="button" class="add_option_link">+</button>';
  2. var deleteOptionButton = '<button type="button" class="remove_option_link">x</button>';
  3. var _prototype = '<div class="survey_option"><label>__num__: </label><input type="text" id="survey_options___name___title" name="survey[options][__name__][title]" required="required" maxlength="500" />'+deleteOptionButton+'</div>';
  4.  
  5. $(document).ready(function() {
  6.     $("div#survey_options").data('index', $("div#survey_options").find(':input').length);
  7.     $("div#survey_options").append('<div class="survey_options_list"></div>')
  8.     $("div#survey_options").append(newOptionButton);
  9.  
  10.     $("div#survey_options").attr("data-prototype", null);
  11.  
  12.     $("button.add_option_link").click(function(){
  13.         var prototype = _prototype;
  14.         var index = $("div#survey_options").data('index');
  15.  
  16.         prototype = prototype.replace(/__name__/g, index);
  17.         prototype = prototype.replace(/__num__/g, index+1);
  18.  
  19.         $("div.survey_options_list").append(prototype);
  20.         $("div#survey_options").data('index', index + 1);
  21.         console.log("var index: "+index);
  22.         console.log("data index: "+$("div#survey_options").data('index'));
  23.     });
  24.  
  25.     $(document).on('click','.remove_option_link',function(){
  26.         id = $(this).parent().find("input").attr("name");
  27.         id = id.replace(/^\D+|\D+$/g, "");
  28.  
  29.         $(".survey_option").each(function(){
  30.             temp_id = $(this).find("input").attr("name");
  31.             temp_id = temp_id.replace(/^\D+|\D+$/g, "");
  32.  
  33.             if(temp_id > id) {
  34.                 $(this).find("label").html((temp_id)+": ");
  35.                 $(this).find("input").attr("id", "survey_options_"+(temp_id-1)+"_title");
  36.                 $(this).find("input").attr("name", "survey[options]["+(temp_id-1)+"][title]");
  37.             }
  38.         });
  39.  
  40.         $(this).parent().remove();
  41.  
  42.         console.log($("div#survey_options").data('index'));
  43.  
  44.         $("div#survey_options").data('index', $("div#survey_options").data('index') - 1);
  45.     });
  46. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement