Thursday, 14 February 2019

Add array value in hidden field and retrieve array value from hidden value


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            //For Add array
            $("#selcity").on("change", function () {
                var itemval = $("#selcity option:selected").val();
                var itemtext = $("#selcity option:selected").text();
             
                var items = JSON.parse($('#hdfield').val());
                //Avaide duplicate
                var bool = true;
                    for (var i = 0; i < items.length; i++) {
                       if (items[i][1] == itemval) {
                            bool = false;
                        }
                    }

//                $("#selcity").each(function () {
//                    alert($(this).val() +'=='+ itemval+ ($(this).val() == itemval))
//                    if ($(this).val() == itemval) {
//                        bool = true;
//                    }
//                })

                if (bool) {
                    //For Add
                    var items1 = [itemtext, itemval]
                    items.push(items1)
                    console.log(JSON.stringify(items));
                    document.getElementById("Start").innerHTML = JSON.stringify(items);
                    $('#hdfield').val(JSON.stringify(items));
                }
                //For remove
                //                     for (var i = 0; i < items.length; i++) {
                //                         if (items[i][0] == "Mumbai") {
                //                             items.splice(i, 1);
                //                         }
                //                     }

             

            })
        });

        function pushvalue() {
            var items = JSON.parse($('#hdfield').val());
            var items1 = ["Nasik", 40]
            items.push(items1)
            console.log(JSON.stringify(items));
            document.getElementById("Start").innerHTML = JSON.stringify(items);

            $('#hdfield').val(JSON.stringify(items));
        }
        function hdread() {
            var items = value = JSON.parse($('#hdfield').val());
            for (var i = 0; i < items.length; i++) {
                alert("hdread " + items[i][1] + '-' + items[i][0])
            }
            document.getElementById("Start").innerHTML = JSON.stringify(items);
        }
    </script>
</head>
<body>
<span >Start</span>
<div id='Start'></div>
<p>--------------</p>
<span>Then</span>
<div id='Then'></div>
<input type="hidden" id="hdfield" />
<select id="selcity" >
<option value="40" >Akola</option>
<option value="50" >Bhusawal</option>
<option value="60" >Raigad</option>
</select>
</body>
 <script type="text/javascript" language="javascript">
     var items = [
                  ["Mumbai", 10],
                  ["Jalgaon", 20],
                  ["Pune", 30]
                ];

//     for (var i = 0; i < items.length; i++) {
//         if (items[i][1] == "10") {
//             items.splice(i, 1);
//         }
//     }
     console.log(items);

     $('#hdfield').val(JSON.stringify(items));
     console.log(JSON.stringify(items))
     var value = $('#hdfield').val();
     value = JSON.parse(value);
 
    </script>
</html>


No comments:

Post a Comment