views:

40

answers:

2

instead of using getParameterByName('Field', PostData) (PostData == $('form').serialize();)

I would like to write PostData.Field, how can i do that with javascript?

A: 

Do you mean something like this?

PostData = {
  field: $('input[name=Field]').val(),
  otherData: 'customdata'
};
Shiki
No i mean obj = MakeIntoFields(PostData);obj.FieldName
acidzombie24
+2  A: 

You could write your own extension to return an object like you want, here's what that looks like:

jQuery.fn.MakeIntoFields = function() {
  var arr = this.serializeArray();
  var props = {};
  $.each(arr, function(i, f) {
    props[f.name] = f.value;
  });
  return props;
};

You'd call it by doing this:

var PostData = $("form").MakeIntoFields();

Then you could access the values with dot notation like you want:

PostData.fieldNameHere
//or...
PostData["fieldNameHere"]

You can see this working against a demo <form> here

Nick Craver
That is *very* cool
acidzombie24