JavaScript how to Creating JSON each input value using jquery jquery create json each

I would like to print some data in JSON format for another apps use. How I should construct the Javascript object to create the JSON format dynamically.

Here is my HTML Code:

<div class="col-sm-6">
    <div class="form-group">
        <input type="text" class="form-control" name="frstname" data-conv="json">
        <input type="text" class="form-control" name="lastname" data-conv="json">
        <select name="gender" class="form-control" data-conv="json">
            <option value="male">Male</option>
            <option value="female">Female</option>
        </select>
        <input type="date" class="form-control" name="dob" data-conv="json">
        <input type="text" class="form-control" name="email" data-conv="json">
    </div>
    <div class="form-group">
        <button type="button" class="btn-action" class="btn btn-default">Create</button>
    </div>
</div>

I have try with following Jquery code:

$(document).ready(function(){
    $('.btn-action').on('click', function(){

        var jsonObj = [];
        $("input[data-conv=json]").each(function() {
            var firstname = $(input[name=frstname]).val();
            var lastname  = $(input[name=lastname]).val();
            var gender    = $(input[name=gender]).val();
            var dob       = $(input[name=dob]).val();
            var email     = $(input[name=email]).val();

            item = {}
            item ['firstname']  = firstname;
            item ['lastname']   = lastname;
            item ['gender']     = gender;
            item ['dob']        = dob;
            item ["email"]      = email;

            jsonObj.push(item);
        });
        var jsonString = JSON.stringify(jsonObj);
        console.log(jsonString);
    });
});

I am getting ERROR:

Uncaught ReferenceError: input is not defined

Any help will be greatly appreciated!

Answer:1

I wanted to get the data-id attribute of the source element when the context menu is activated. How can i access the source element? Here is what Ive done so far: $(".action").contextmenu({ ...

I wanted to get the data-id attribute of the source element when the context menu is activated. How can i access the source element? Here is what Ive done so far: $(".action").contextmenu({ ...

I'm trying to use setState in map function what I want to make when I hit click button that makes states changed I searched "In map function changed setState" in stackoverflow but It was hard to ...

I'm trying to use setState in map function what I want to make when I hit click button that makes states changed I searched "In map function changed setState" in stackoverflow but It was hard to ...

  1. setstate function react
  2. setstate functional component react
  3. setstate function component
  4. usestate functional update
  5. setstate function vs object
  6. setstate function in flutter
  7. setstate function instead of object
  8. setstate function syntax
  9. setstate function parameter
  10. setstate function form
  11. setstate function argument
  12. setstate function in react native

I run an e-commerce website and we have various third-party JavaScript that add click handlers to links and forms and then insert a delay to make sure the tracking goes through. The delay is inserted ...

I run an e-commerce website and we have various third-party JavaScript that add click handlers to links and forms and then insert a delay to make sure the tracking goes through. The delay is inserted ...

I'm trying to write a solution to a problem that requires three possible different return values depending on the result of two values. The possibilities are greater than, less than, or equal to. An ...

I'm trying to write a solution to a problem that requires three possible different return values depending on the result of two values. The possibilities are greater than, less than, or equal to. An ...