No problem. Just use the following. This will behave exactly like serializing a form but using a div's content instead.
$('#divId :input').serialize();
Check https://jsbin.com/xabureladi/1 for a demonstration (https://jsbin.com/xabureladi/1/edit for the code)
You can improve the speed of your code if you restrict the items jQuery will look at.
Use the selector :input instead of * to achieve it.
$('#divId :input').serialize()
This will make your code faster because the list of items is shorter.
serialize
all the form-elements within a div
.You could do that by targeting the div #target-div-id
inside your form
using :
$('#target-div-id').find('select, textarea, input').serialize();
The function I use currently:
/**
* Serializes form or any other element with jQuery.serialize
* @param el
*/
serialize: function(el) {
var serialized = $(el).serialize();
if (!serialized) // not a form
serialized = $(el).
find('input[name],select[name],textarea[name]').serialize();
return serialized;
}
Try also this:
$('#divId').find('input').serialize()
What about my solution:
function serializeDiv( $div, serialize_method )
{
// Accepts 'serialize', 'serializeArray'; Implicit 'serialize'
serialize_method = serialize_method || 'serialize';
// Unique selector for wrapper forms
var inner_wrapper_class = 'any_unique_class_for_wrapped_content';
// Wrap content with a form
$div.wrapInner( "<form class='"+inner_wrapper_class+"'></form>" );
// Serialize inputs
var result = $('.'+inner_wrapper_class, $div)[serialize_method]();
// Eliminate newly created form
$('.script_wrap_inner_div_form', $div).contents().unwrap();
// Return result
return result;
}
/* USE: */
// For: $('#div').serialize()
serializeDiv($('#div')); /* or */ serializeDiv($('#div'), 'serialize');
// For: $('#div').serializeArray()
serializeDiv($('#div'), 'serializeArray');
function serializeDiv( $div, serialize_method )
{
// Accepts 'serialize', 'serializeArray'; Implicit 'serialize'
serialize_method = serialize_method || 'serialize';
// Unique selector for wrapper forms
var inner_wrapper_class = 'any_unique_class_for_wrapped_content';
// Wrap content with a form
$div.wrapInner( "<form class='"+inner_wrapper_class+"'></form>" );
// Serialize inputs
var result = $('.'+inner_wrapper_class, $div)[serialize_method]();
// Eliminate newly created form
$('.script_wrap_inner_div_form', $div).contents().unwrap();
// Return result
return result;
}
/* USE: */
var r = serializeDiv($('#div')); /* or serializeDiv($('#div'), 'serialize'); */
console.log("For: $('#div').serialize()");
console.log(r);
var r = serializeDiv($('#div'), 'serializeArray');
console.log("For: $('#div').serializeArray()");
console.log(r);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="div">
<input name="input1" value="input1_value">
<textarea name="textarea1">textarea_value</textarea>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With