jQuery to serialize only elements within a div

JquerySerialization

Jquery Problem Overview


I would like to get the same effect as jQuery.serialize() but I would like to return only the child elments of a given div.

Sample result :

single=Single2&multiple=Multiple&radio=radio1

Jquery Solutions


Solution 1 - Jquery

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)

Solution 2 - Jquery

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.

Solution 3 - Jquery

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();

Solution 4 - Jquery

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;
}

Solution 5 - Jquery

Try also this:

> $('#divId').find('input').serialize()

Solution 6 - Jquery

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>

Solution 7 - Jquery

$('#divId > input, #divId > select, #divId > textarea').serialize();

Solution 8 - Jquery

If those elements have a common class name, one may also use this:

$('#your_div .your_classname').serialize()

This way you can avoid selection of buttons, which will be selected using the jQuery selector :input. Though this can also be avoided by using $('#your_div :input:not(:button)').serialize();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrokeMyLegBikingView Question on Stackoverflow
Solution 1 - JqueryjitterView Answer on Stackoverflow
Solution 2 - JqueryThiagoPXPView Answer on Stackoverflow
Solution 3 - JqueryZakaria AcharkiView Answer on Stackoverflow
Solution 4 - JqueryLukasz FrankowskiView Answer on Stackoverflow
Solution 5 - JqueryAlberto BuschettuView Answer on Stackoverflow
Solution 6 - JqueryBrigantiView Answer on Stackoverflow
Solution 7 - JqueryjefissuView Answer on Stackoverflow
Solution 8 - JqueryYogesh MistryView Answer on Stackoverflow