How to add auto-increment in the DOM

Viewed 69

I have a problem to add auto-increment function in the javascript. For example if Kod Terakhir is 100-1/5, when showing the input field is 100-1/6, that mean every time will detect from the Kod Terakhir number and add 1 behind the number.

Below is my javascript code:

        var id_selected = selectObject.value;
        var cate_id_selected = selectObject.selectedOptions[0].getAttribute("data-cat_id");
        var root_selected = selectObject.selectedOptions[0].getAttribute("data-root");
        var action = 'find_max_code';
        $.ajax({
        url:'?f=kod_perfailan',
        type:'POST',
        data:{id_selected: id_selected,
                    cate_id_selected: cate_id_selected,
                    root_selected: root_selected,
                    action: action
        },
        success:function(data) {
            if(data.trim() !== ""){
                    // document.getElementById("function_code_last").innerHTML = "*Kod Terakhir: " + data;
                    document.getElementById("activity_code_last").innerHTML = "*Kod Terakhir: " + data;
                    document.getElementById("sub_activity_code_last").innerHTML = "*Kod Terakhir: " + data;
                    document.getElementById("transaction_code_last").innerHTML = "*Kod Terakhir: " + data;
                    document.getElementById("activity_code").value = data + 1;
                    document.getElementById("sub_activity_code").value = data + 1;
                    document.getElementById("transaction_code").value =  data + 1;                  
            }else{
                    // document.getElementById("function_code_last").innerHTML = "*Tiada Data Disimpan";
                    document.getElementById("activity_code_last").innerHTML = "*Tiada Data Disimpan";
                    document.getElementById("sub_activity_code_last").innerHTML = "*Tiada Data Disimpan";
                    document.getElementById("transaction_code_last").innerHTML = "*Tiada Data Disimpan";
            }
        }

Below is my output, but it is wrong, because the input field should be 100-1/6, not 100-1/51. I think my code data + 1; here is got problem.

output 1

Actually I want the success output is below the picture:

enter image description here

So that, below is the input field may show the number format (example):

100-1/xxxx
100-1/5/xxxx
100-1/5/10/xxxx
256-1/10/xxxx

That means, xxxx number need to add 1 every time to do the auto-increment. My number is store in the data.

Hope someone can guide me how to solve this problem. Thanks.

1 Answers

You need to tokenize the string, remove and increment the part that you are interested in. It seems that you are simply performing a string concatenation operation.

  function incrementSubActivityCode(sac) {
    var p = sac.lastIndexOf("/") + 1;
    return (p === 0) ? sac :
      sac.substr(0, p) + (parseInt(sac.substr(p)) + 1);
  }
       
  // Example value
  var data = "100-1/5/10/5";

  // Incorrect - performs concatenation
  document.getElementById("sub_activity_code").value = data + 1;

  // Corrected - now performs increment
  document.getElementById("sub_activity_code").value = incrementSubActivityCode(data);
Related