Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8958493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:13:32+00:00 2026-06-15T15:13:32+00:00

I am a beginner in jquery so this may be stupid. I want to

  • 0

I am a beginner in jquery so this may be stupid.
I want to take csv data from the user and transform it into json. Found a nice library but don’t quite understand how the submitting is done in jquery.

For instance if I have a view

<script type="text/javascript" src="<?php echo site_url('public/js/jquery.js') ?>" ></script>
<script type="text/javascript" src="<?php echo site_url('javascript/main.js') ?>" ></script>


    <div id="message"><p></p></div>

    <form id="convertForm" name="convertForm" onsubmit="return false; ">
        CSV<br /><textarea id="csv" name="csv" rows="10" cols="60"></textarea><br /><br />

        JSON<br /><textarea id="json" name="json" rows="10" cols="60" readonly="readonly"></textarea><br /><br />


        <input type="button" value="Convert" id="submitButton"/> <input type="reset" />
    </form>

And the main.js file

function parseCSVLine (line)

            {
                line = line.split(',');
                for (var i = 0; i < line.length; i++)
                {
                    var chunk = line[i].replace(/^[\s]*|[\s]*$/g, "");
                    var quote = "";
                    if (chunk.charAt(0) == '"' || chunk.charAt(0) == "'") quote = chunk.charAt(0);
                    if (quote != "" && chunk.charAt(chunk.length - 1) == quote) quote = "";

                    if (quote != "")
                    {
                        var j = i + 1;

                        if (j < line.length) chunk = line[j].replace(/^[\s]*|[\s]*$/g, "");

                        while (j < line.length && chunk.charAt(chunk.length - 1) != quote)
                        {
                            line[i] += ',' + line[j];
                            line.splice(j, 1);
                            chunk = line[j].replace(/[\s]*$/g, "");
                        }

                        if (j < line.length)
                        {
                            line[i] += ',' + line[j];
                            line.splice(j, 1);
                        }
                    }
                }

                for (var i = 0; i < line.length; i++)
                {
                    // remove leading/trailing whitespace
                    line[i] = line[i].replace(/^[\s]*|[\s]*$/g, "");

                    // remove leading/trailing quotes
                    if (line[i].charAt(0) == '"') line[i] = line[i].replace(/^"|"$/g, "");
                    else if (line[i].charAt(0) == "'") line[i] = line[i].replace(/^'|'$/g, "");
                }

                return line;
            }

            function csvToJson ()
            {
                var message = "";
                var error = false;
                var f = document.forms["convertForm"];
                var csvText = f.elements["csv"].value;
                var jsonText = "";

                setMessage(message, error);

                if (csvText == "") { error = true; message = "Enter CSV text below."; }

                if (!error)
                {
                    benchmarkStart = new Date();
                    csvRows = csvText.split(/[\r\n]/g); // split into rows

                    // get rid of empty rows
                    for (var i = 0; i < csvRows.length; i++)
                    {
                        if (csvRows[i].replace(/^[\s]*|[\s]*$/g, '') == "")
                        {
                            csvRows.splice(i, 1);
                            i--;
                        }
                    }

                    if (csvRows.length < 2) { error = true; message = "The CSV text MUST have a header row!"; }
                    else
                    {
                        objArr = [];

                        for (var i = 0; i < csvRows.length; i++)
                        {
                            csvRows[i] = parseCSVLine(csvRows[i]);
                        }

                        benchmarkParseEnd = new Date();

                        for (var i = 1; i < csvRows.length; i++)
                        {
                            if (csvRows[i].length > 0) objArr.push({});

                            for (var j = 0; j < csvRows[i].length; j++)
                            {
                                objArr[i - 1][csvRows[0][j]] = csvRows[i][j];
                            }
                        }

                        benchmarkObjEnd = new Date();

                        jsonText = JSON.stringify(objArr, null, "\t");

                        benchmarkJsonEnd = new Date();

                        f.elements["json"].value = jsonText;

                        benchmarkPopulateEnd = new Date();

                        message = getBenchmarkResults();


                    }
                }

                setMessage(message, error);
            }

$(document).ready(function(){

            $('#submitButton').on('click', function(){

                console.log('i got here');
                csvToJson;


            });



});

This works perfectly but console.log is never shown.

And if I try to do something like return the jsonText from the csvToJson function and then append it to the second textarea, that doesn’t work either.

function csvToJson ()

{

//same code here

return jsonText;

}


$(document).ready(function(){

            $('#submitButton').on('click', function(){

                console.log('i got here');
                jsont = csvToJson();
                                $("#json").val(jsonT);


            });

It’s clearly something I don’t get about submitting. Should I use jquery .submit() function ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:13:34+00:00Added an answer on June 15, 2026 at 3:13 pm

    You are missing brackets when calling csvToJson inside the click handler.

    Change:

    $('#submitButton').on('click', function(){ 
                    csvToJson;
    });
    

    TO

    $('#submitButton').on('click', function(){ 
                    csvToJson();
    });
    

    DEMO: http://jsfiddle.net/MRmJM/1/

    One note: If CSV is separated by semi-colon (not uncommon) , parser code fails to create proper object

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just starting to get into jquery/javascript programming so this may be a beginner
I am a beginner at Jquery. What i want is to display the data
I'm a beginner jquery user so I'm really hoping this will be quite straight
I'm a beginner in JQuery (I know nothing about this framework). I recently found
I'm a beginner in JS and jQuery, and I want to add 2 rows
OK - I'll admit, I'm quite a beginner in this jQuery-department. I've probably made
I am a beginner in jQuery and I want to built a simple jQuery
I am very new to Javascript and Jquery, so my apologies for this beginner's
I'm beginner in jquery.this is my first application. <head> <title>Untitled Document</title> <script type=text/javascript src=../jquery-1.6.2.min.js></script>
I wrote a beginner jQuery plugin which validate a form. this jQuery Plugin show

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.