1

I'm trying to put together a web form to mark an indeterminate number of employees as either present or absent. The page itself contains an arbitrary number of divs of the form:

<div class="employee" empID="9" presence="0">

The divs themselves contain the options, with 'presence' being changed to 1 or 2 using jQuery depending on the option selected.

When the 'submit' button is pressed, I'd like to convert this data into a parsable array of pairs of 'empID' and 'presence'. I've tried doing this with jQuery as follows:

$('.cic-button').click(function(){
        var submitData = {employees:[]};
        $('firedrill-employee').each(function(){
            submitData.push({
                employeeID: $(this).attr('empID'),
                presence: $(this).attr('presence')
            });
        });
    });

However, when this is done, the submitData variable is failing to populate. Any idea why? Am I going about this in the correct manner? Is what I'm trying to do even possible?

Many thanks.

3
  • 5
    I'd recommend not making up random attributes but instead using HTML5's custom data attributes. Ex: <div class="employee" data-empid="9" data-presence="0"> Commented Jan 20, 2015 at 18:44
  • 4
    What is $('firedrill-employee')? There's no such HTML tag. If that's supposed to be a class, you need . at the beginning. Commented Jan 20, 2015 at 18:46
  • 2
    do you mean submitData.employees.push() ? Commented Jan 20, 2015 at 18:49

3 Answers 3

2

You have a few errors. Make the class that you iterate over the collection of "employee" not "firedrill-employee" and don't forget the dot to indicate it's a class. Reference the employees array withing the submitData object. You can't just push an element into an object.

$('.cic-button').click(function () {
    var submitData = {
        employees: []
    };
    $('.employee').each(function () {
        submitData.employees.push({
            employeeID: $(this).data('empID'),
            presence: $(this).data('presence')
        });
    });
    console.log(submitData);
});

Fiddle

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

Comments

0

Js fiddle

$('.cic-button').click(function () {

    var submitData = [];

    $('.employee').each(function () {
        var self = $(this);
        // Create and obj 
        var obj = new Object(); // {};
        obj["employeeID"] = self.attr("empID");
        obj["presence"] = self.attr("presence");

        //push that object into an array
        submitData.push(obj);

        console.log(obj);
        console.log(submitData);
    });
});

Comments

0

You need to specify the employee array as such:

$('.cic-button').click(function(){
    var submitData = {employees:[]}; // employees is an array within submitData...
    $('.firedrill-employee').each(function(){
        submitData.employees.push({ // ...so amend code here to push to the array, not submitData
            employeeID: $(this).attr('empID'),
            presence: $(this).attr('presence')
        });
    });
    console.log(submitData);    
});

See example JSFiddle - http://jsfiddle.net/yng1qb6o/

6 Comments

what does specify the employee array as such mean? Seems like all you did was fix a syntax error ... but didn't say that
It seems to me, that he need to loop $('.employee'), not $('.firedrill-employee') (according to the question and the code he posted)
@charlietfl - The original code attempted to push to an object (submitData), not an array (submitData.employees).
@phillip100 - Original code used a selector for firedrill-employee, I assume the HTML provided in the question was just an example so I kept the selector in his JS.
so instead of expecting people to be code diff tools, a simple explanation would help. Not a big deal but not always easy to spot differences
|

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.