I already have added one question related to it. But there is a little change this time.
I have a readonly text input with a default value 1.
<?= $form->field($modelTariffSlabs, "[{$i}]slab_start")->textInput(['readonly'=>true,'value'=>'1','maxlength' => 10,"class"=>"form-control js-slab-start"]) ?>
Also, I have another text input which I am getting from the user
<?= $form->field($modelTariffSlabs, "[{$i}]slab_end")->textInput(['maxlength' => 10]) ?>
What I want to do?
Whenever I press the add button, I want to add +1 to the slab_end text input and then assign it to my next slab_start input text.
What I have tried?
$("#addBtn").click(function(e)
{
$(".dynamicform_wrapper").on("afterInsert", function(e, item) {
$(".dynamicform_wrapper .js-slab-start").each(function(index) {
var end = +$("#mdctariffslabs-0-slab_end").val()+1;
$(this).val(end);
});
});
});
So I added 100 in slab_end input text, and when I press the add button It does add 1 in it but shows me 101 in my next slab_start and current slab_start.
1st Input (Before pressing add button)
After pressing add button
Here as shown in the above image. The start value of 1st level is changed when I press the add button also the start value has changed and the next value of start is 101 but on 3rd level, the start value should be 151 as I have added 150 in my previous end text input.
Update 1
Below is my addBtn jquery code
$("#addBtn").click(function(e)
{
$(".dynamicform_wrapper").on("afterInsert", function(e, item) {
//console.log("after insert");
$(".dynamicform_wrapper .js-slab-name").each(function(index) {
//console.log("set slab-name");
$(this).val("S-" + (index + 1));
});
var len = $(".dynamicform_wrapper .js-slab-end").length-1;
console.log(len);
var slabEndValue = $(".dynamicform_wrapper .js-slab-end:eq("+len+")").val();
slabEndValue = parseInt(slabEndValue);
console.log(slabEndValue);
$(".dynamicform_wrapper .js-slab-start:end").val(slabEndValue);
});
});
});
Update 2
I have updated my code. Removed the addBtn event and simply placed the event afterInsert including the value incrementing logic. But still the result is same
<?PHP
$script = <<< JS
$(".dynamicform_wrapper").on("afterInsert", function(e, item) {
//console.log("after insert");
$(".dynamicform_wrapper .js-slab-name").each(function(index) {
//console.log("set slab-name");
$(this).val("s-" + (index + 1));
});
var len = $(".dynamicform_wrapper .js-slab-end").length-1;
console.log(len);
var slabEndValue = $(".dynamicform_wrapper .js-slab-end:eq("+len+")").val();
console.log(slabEndValue);
slabEndValue = parseInt(slabEndValue);
$(".dynamicform_wrapper .js-slab-start:end").val(slabEndValue);
});
$(".dynamicform_wrapper").on("afterDelete", function(e) {
console.log("Deleted item!");
});
$(".dynamicform_wrapper").on("limitReached", function(e, item) {
alert("Limit reached");
});
JS;
$this->registerJs($script);
?>
Update 3
I have tried to run the jquery in the console window.
In the console, I am getting 100 but while in production I am getting nothing. Secondly, I want to +1 the value entered in end text box
Any help would be highly appreciated.



html, also explain the exact logic what you trying to get.afterInserton every click which is technically wrong , yourafterInsertshould be outside the click bindingHTMLcode of the panel what you render from browser, with the wrapperdivof all your panels.