Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi guys,

i want to add data into arrays in jquery on button click

code:
<script type="text/javascript">
        function ADD2Grid() {
            //Getting the source data with ajax GET request
            var tt = $('#ddltitle :selected').text() + "|" +
                $("#<%=txtfn.ClientID %>").val() + "|" +
                $("#<%=txtmn.ClientID %>").val() + "|" +
                $("#<%=txtln.ClientID %>").val();
            //alert(tt);
            var data = [{ "empName": $("#<%=txtfn.ClientID %>").val(),
                "age": $("#<%=txtmn.ClientID %>").val(),
                "department": { "id": "1234", "name": "Sales" },
                "author": $("#<%=txtln.ClientID %>").val()
            }];

            // prepare the data
            var source =
{
    datatype: "json",
    datafields: [
        { name: 'empName' },
        { name: 'age' },
        { name: 'id', map: 'department>id' },
        { name: 'name', map: 'department>name' },
        { name: 'author' }
    ],
    localdata: data
};
            var dataAdapter = new $.jqx.dataAdapter(source,
                {  contentType: 'application/json; charset=utf-8'}
            );
            $("#jqxgrid").jqxGrid({
                source: dataAdapter,
                theme: 'classic',
                columns: [
                    { text: 'empName', dataField: 'empName', width: 50 },
                    { text: 'age', dataField: 'age', width: 250 },
                    { text: 'name', dataField: 'name', width: 250 },
                    { text: 'author', dataField: 'author', width: 250 }
                ]
            });
        };
    </script>



i have tried this code, but its not excuting.
var sp = tt.split("|");
            if (getData == null) {
                getData = "{'empName' :'" + sp[0] + "','age':'" + sp[1] + "','department':'" + "Sales" + "','author':'" + sp[2] + "'},";
            }
            else {
                getData += "{'empName' :'" + sp[0] + "','age':'" + sp[1] + "','department':'" + "Sales" + "','author':'" + sp[2] + "'},";
            }
            
            var fullData = "[" + getData.replace(/'/g, '"') + "]";
            alert(fullData); // Excutes
            var data = JSON.parse(fullData);
            alert(data); // not Excuting


Error
Uncaught SyntaxError: Unexpected token ]

Can anyone please help me.


Thanks
Posted
Updated 2-Jun-15 6:58am
v5
Comments
F-ES Sitecore 2-Jun-15 9:23am    
What have you tried so far? Have you googled how to use click events in jQuery? How to use arrays in javascript? Is there something you are specifically stuck on?
abdul subhan mohammed 2-Jun-15 9:33am    
You can data above in the code, there you see i'm using asp tags.
there in data i want to use serailizeArrays(). in above formate(data).
How to convert above code into serializeArrays.
can u plz help me
F-ES Sitecore 2-Jun-15 9:40am    
If you serialise a webforms form then you'll end up with what is probably undesirable json as the name attributes will be .net specific. What you're doing isn't too far off what is probably the best way of doing it. You could maybe build a json object with the right properties and serialise that rather than the in-line way you are doing it above, but the overall method is going to be largely the same.
abdul subhan mohammed 2-Jun-15 9:49am    
i have updated code, plz check
What is the issue with this code? Here it is taking values from the textboxes and forming an array. So, what is the issue?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900