javascript - Using call on a jquery function for multiple elements -


i have following code is, inserts after div specific id attribute when clicked on button. , increments counter used specify div inserted after.

it works, want know is? how can change code around, can create object properties , create method can used insert after, lastly call specific jquery elements , job gets done.

the reason enquiry want use method on other objects follows same principles has different object properties, can use single method call different objects different properties , make call insert after different divs.

$(document).ready(function() {  counter = 0; $("#addmeasurement").on("click", function() {++counter;     //create variables insert after     var container = '<div id="measurement_container' + counter + '" class="col-12 object">'     var icon = '<span id="measurement_icon' + counter + '" class="flaticon-glass-of-water-with-drop" title="measurement"></span>';     var info_span = '<span id="measurement_info' + counter + '" class="">';     var info_mt = '<input class="totop" type="text" name="measurementtype' + counter + '" id="measurementtype' + counter + '" placeholder="measurement" size="10">';     var info_ma = '<input class="totop" style="margin-left:90px;" type="text" name="measurementabbr' + counter + '" id="measurementabbr' + counter + '" placeholder="abbr" size="3">';     var info_mti = '<input class="totop" style="margin-top:25px;" type="text" name="measurementtitle' + counter + '" id="measurementtitle' + counter + '" placeholder="name" size="10"> ';     var info_md = '<input class="totop"  style="margin-top:25px; margin-left:90px; width:3.5em;" type="number" name="measurementdescimalplaces' + counter + '" id="measurementdescimalplaces' + counter + '" min="0" max="4" title="descimal points" placeholder=".dp">';      $(container + icon + info_span + info_mt + info_ma + info_mti + info_md + '</span>' + '</div>').insertafter("#measurement_container" + (-1 + counter));  }); }); 

i want have (i omitted of properties in object easier readability).

$(document).ready(function(){ var counter = 0;  var measurement_object = { container : '<div id="measurement_container' + counter + '"   class="col-12 object">',     icon: '<span id="measurement_icon' + counter + '" class="flaticon-glass-  of-water-with-drop" title="measurement"></span>' }  function duplicate_object(el){ $(this).insertafter(el); }  $("#addmeasurement").on("click", function(){ duplicate_object.call(duplicate_object,"#measurement_container0"); }); }); 

honestly if you're inserting html js, i'd take step , consider other options. it's code smell means you're approaching in wrong manner.

that being said, there's no need create factory function simple. here's core functionality of function. i'm abbreviating building of element sake of time:

$(document).ready(function() {    var count = 0;        $('#addmeasurement').on('click', function() {      var div = document.createelement('div');            // should class, since you're using more once.      $(div).insertafter('#measurement_container');            count++;    });        $('.other-element').on('click', function() {      console.log(count); // outputs # of elements added    });  });

describe want on page, , can little more.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -