tags:

views:

45

answers:

1
 $("#fieldset").closest("fieldset").find("input, select,textarea").change(function() {
        return ($(this).val());
    }).get().join(',');

I am getting all editable field values on change.. I need to store all editable values in an array to pass to the controller?

how to store the values in an array

thanks

+2  A: 
var array;

$("#fieldset").change(function(event) {
    $(event.target).data('changed',true);
});

$("form").submit(function() {
    array = $(this).find("input, select,textarea").map(function() {
        var $th = $(this);
        if( $th.data('changed') ) return $(this).val();
    }).get();
});

The #fieldset change event will fire when any of its descendant elements are changed.

Then handler finds the input/select/textarea elements, and performs .map() on them returning their value, which creates a jQuery object with the values.

.get() grabs/returns the array from the jQuery object.

Test it here: http://jsfiddle.net/Mrrty/2/ (change the value of one of the elements)


EDIT:

Note that if you don't want to get a new array every time one of the elements changes, you can do the same thing with a different event. Perhaps doing this on .submit() would be more appropriate.

patrick dw
Thanks..patrick..I need to have this condition.. if any changes are made fieldset I need to pass array if not I need to pass null value?how to check the condition?
@rockers - What is triggering this? Is it a `submit()`?
patrick dw
@rockers - You didn't respond to my comment, so I went ahead and updated my answer after seeing your other question.
patrick dw
Sorry pat I was away my desk.. yes your logic working for me.