Guest User

Untitled

a guest
Sep 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. appending a form element until comma is pressed
  2. $("#options_"+id).keyup(function(){
  3.  
  4. var val_options = $('#options_'+id).val(); //user input is stored in the variable val_options
  5.  
  6. var options = val_options.split(','); //input is exploded with a comma
  7.  
  8. var len = options.length;
  9.  
  10. for(var i =0;i<len;i++)
  11. {
  12.  
  13. var drop_options = "<option id='option_'"+i+">"+options[i]+"</option>";
  14. $("#select_"+id).append(drop_options);
  15. }
  16.  
  17. });
  18.  
  19. $("#options_" + id).keyup(function(e) {
  20.  
  21. var val_options = $(this).val(); // getting input values
  22. options = val_options.split(','); // getting all values as array
  23.  
  24. // checking for comma
  25. if (val_optons.indexOf(',') >= 0 ) {
  26.  
  27. // if comma found then start append
  28. var options = val_options.split(',');
  29. var drop_options = ''; // take an empty string
  30.  
  31. // looping over all values with
  32. // input fields separated by comma
  33.  
  34. for (var i = 0; i < options.length; i++) {
  35.  
  36. // making string of all options
  37. drop_options += "<option id='option_'" + i + ">" + options[i] + "</option>";
  38. }
  39.  
  40. // append all option to select
  41. // here .html() will remove all previous
  42. // options and append newly
  43.  
  44. $("#select_" + id).html(drop_options);
  45.  
  46. }
  47. });
  48.  
  49. $("#options_"+id).keyup(function(event){
  50. if (event.which !== 188) { // 188 is the keycode for ','
  51. return; // Do nothing.
  52. }
  53.  
  54. var $this = $(this),
  55. val_options = $this.val(),
  56. options = val_options.split(','),
  57. $dropdown = $("#select_"+id);
  58.  
  59. $dropdown.empty();
  60.  
  61. for(var i =0;i<options.length-1;i++) {
  62. $dropdown.append(
  63. "<option id='option_'"+i+">"+options[i]+"</option>"
  64. );
  65. }
  66. });
Add Comment
Please, Sign In to add comment