0

I have multiple checkboxes in form with values like this

<input type="checkbox" value="100400020719-006" name="selected" class="inforID" id="ABS-02072019" plant="VDNF" flowno="FLW-000001">

When checkbox is checked, and button is pressed, I will get attributes and insert into input with id arr

<input type="hidden" id="arr" name="arr" /> 

$(document).ready(function() {
    $("#btnConf").click(function(){
        var selected = [];
        $.each($("input[name='selected']:checked"), function () {
            selected.push($(this).val(), $(this).attr("id"), $(this).attr("plant"), $(this).attr("flowno"));
            document.getElementById("arr").value = selected;
        });
        console.log(selected);
    });
});

But the array which I get is

<input type="hidden" id="arr" name="arr" value="100400020719-006,ABS-02072019,VDNF,FLW-000001,100400020719-007,ABS-02072019,VDNF,FLW-000001">

How can I get the array like this:

[
    {
        "DocNo":"100400020719-006",
        "NewDocNo":"ABS-02072019",
        "Plant":"VDNF",
        "FlowNow":"FLW-000001"
    },
    {
        "DocNo":"100400020719-007",
        "NewDocNo":"ABS-02072019",
        "Plant":"VDNF",
        "FlowNow":"FLW-000001"
    }
]

Or like this

[
    {
        "100400020719-006",
        "ABS-02072019",
        "VDNF",
        "FLW-000001"
    },
    {
        "100400020719-007",
        "ABS-02072019",
        "VDNF",
        "FLW-000001"
    }
]

Thanks so much

3 Answers 3

1

You can just stringify your data and set it in hidden box. and you can parse and use wherever you want.

$(document).ready(function() {
    $("#btnConf").click(function(){
        var selected = [];
        $.each($("input[name='selected']:checked"), function () {
            selected.push({
              "DocNo": $(this).val(),
              "NewDocNo": $(this).attr("id"),
              "Plant": $(this).attr("plant"),
              "FlowNow": $(this).attr("flowno")
            });
            $("#arr").val(JSON.stringify(selected));
        });
        console.log(JSON.stringify(selected));
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" value="100400020719-006" name="selected" class="inforID" id="ABS-02072019" plant="VDNF" flowno="FLW-000001">

<input type="checkbox" value="100400020719-007" name="selected" class="inforID" id="ABS-02072018" plant="VDND" flowno="FLW-000002">

<input type="hidden" id="arr" name="arr" />

<input type="button" value="get" id="btnConf"/>

Sign up to request clarification or add additional context in comments.

3 Comments

thank you very much, I have tried this solution but not convert array to JSON.
I can't get you. are you saying you don't want to convert it to JSON?
I mean I have tried this solution before but not use JSON.stringify, so I didn't get the array
1

insert selected checkbox attribute values as JS objects into selected array

$(document).ready(function() {
  $("#btnConf").click(function() {
    var selected = [];
    $.each($("input[name='selected']:checked"), function() {
      selected.push({
        "DocNo": $(this).val(),
        "NewDocNo": $(this).attr("id"),
        "Plant": $(this).attr("plant"),
        "FlowNow": $(this).attr("flowno")
      });
      document.getElementById("arr").value = selected;
    });
    console.log(selected);
  });
});

Comments

1

Push your information as an object into selected array. At the end of processing use JSON.stringify to change the object into string and store it in the hidden variable.

$(document).ready(function() {
  var selected = [];
  $("#btnConf").click(function() {
    selected = [];
    $.each($("input[name='selected']:checked"), function() {
      selected.push({
        DocNo: $(this).val(),
        NewDocNo: $(this).attr("id"),
        Plant: $(this).attr("plant"),
        FlowNow: $(this).attr("flowno")
      });
    });
    $("#arr").val(JSON.stringify(selected));
    console.log($("#arr").val());
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" checked="checked" value="100400020719-006" name="selected" class="inforID" id="ABS-02072019" plant="VDNF" flowno="FLW-000001">
<input type="checkbox" value="100400020719-006" name="selected" class="inforID" id="ABS-02072019" plant="VDNF" flowno="FLW-000001">
<button id="btnConf">Configure</button>
<input type="hidden" id="arr" name="arr" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.