I cannot apply js color set (jscolor.js) in input field

Below is the html code of the code code

   <head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
     <script src="jscolor-2.0.4/jscolor.js"></script>
      <body>

        <script>
      $(document).ready(function() {
      var max_fields      = 10; 
       var wrapper         = $(".input_fields_wrap");
          var add_button      = $(".add_field_button"); 

       var x = 1; 
         $(add_button).click(function(e){ 
         e.preventDefault();
          if(x < max_fields){ 
        x++; 
        $(wrapper).append('<div>Case :<input type="text" name="mytext"> Color Picker :  <input class="jscolor" name="c_picker"><br><a href="#" class="remove_field">Remove</a></div>');
    }
});

$(wrapper).on("click",".remove_field", function(e){ 
    e.preventDefault(); $(this).parent('div').remove(); x--;
})
  });
  </script>


        <form>
    DB-URl  :
    <input type="text" name="firstname"><br/>
      Username:
    <input type="text" name="lastname"><br/>
    Password:
   <input type="password" name="Password"><br>
    Table Name:
    <input type="text" name="t_name"><br>
    Color column:
   <input type="text" name="c_column"><br>
  <div class="input_fields_wrap">
    <button class="add_field_button">Add More Fields</button>
    <div>Case :<input type="text" name="mytext">  Color Picker :  <input  class="jscolor" name="c_picker"><br></div>
      </div>
   <input type="submit" value="Update" id="updating">  
 </form> 

The first js color selection is applied in the input field, but when I add more color selection field, dynamic js color selection is not applied in the next input field. Can someone explain to me why this is happening?

+4
source share
4 answers

Use jscolor()to the bindnewly added item:

    $(document).ready(function () {
                var max_fields = 10;
                var wrapper = $(".input_fields_wrap");
                var add_button = $(".add_field_button");

                var x = 1;
                $(add_button).click(function (e) {
                    e.preventDefault();
                    if (x < max_fields) {
                        x++;
                        $(wrapper).append('<div>Case :<input type="text" name="mytext">Color Picker : <input class="jscolor" name="c_picker"><br><a href="#" class="remove_field">Remove</a></div>');
                        var color = new jscolor($(wrapper).find('input.jscolor:last')[0]);
                        //$(wrapper).find('input.jscolor:last').each(function () {
                            //var color = new jscolor($(this)[0]);

                    //});


                    }
                });

                $(wrapper).on("click", ".remove_field", function (e) {
                    e.preventDefault();
                    $(this).parent('div').remove(); x--;
                });
            });
+2
source

You can initialize jsColor on the added input using:

new jscolor($('.jscolor').last()[0]);

NOTE: You do not have to go through all the inputs.

$(document).ready(function() {
  $('.add_field_button').click(function(e){
    e.preventDefault();

    $('form').append('<div>Case :<input type="text" name="mytext">        Color Picker :  <input class="jscolor" name="c_picker"><br><a href="#" class="remove_field">Remove</a></div>');

    new jscolor($('.jscolor').last()[0]);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jscolor/2.0.4/jscolor.min.js"></script>
<form>
  <div class="input_fields_wrap">
    <button class="add_field_button">Add More Fields</button>
    <br><br>
    <div>Case :<input type="text" name="mytext">  Color Picker :  <input  class="jscolor" name="c_picker"><br></div>
  </div>  
</form>
Run codeHide result
+2
source

, , .

If you insert a new HTML element after loading the document, you will need to assign a unique identifier to each new element and attach it manually using this code:

$("#idOfNewElement").colorpicker();
+1
source

This is solved in this stackoverflow question:
using jscolor.js for dynamic input

The solution was to add:

$(document).ready(function() {
  jscolor.installByClassName("jscolor");
});
0
source

Source: https://habr.com/ru/post/1663505/


All Articles