Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

serialize without a form?

Tags:

Hello all i'm turning objects "on (adding the class .active) and off" on a html page with html objects, not forms. And on every click i want it to create an array of the items with the class .active but i can't seem to get any results?!

is this in the right direction?

var data = $('li.tagToggle.active').serializeArray();
// li id format is 'id_0001' 
alert(data)
$.post("/scripts/php/process.php",{ 
         'data[]': data,
         funcName : 'tagResults',
         tagResults : '1'
}) 

keeps alerting and empty window but when i use this on a form it grabs all the objects with the class .active

any pointers welcome!

like image 394
v3nt Avatar asked Mar 03 '10 12:03

v3nt


People also ask

What is serialize form?

Definition and Usage. The serialize() method creates a URL encoded text string by serializing form values. You can select one or more form elements (like input and/or text area), or the form element itself. The serialized values can be used in the URL query string when making an AJAX request.

What is the use of serialize ()?

Definition and Usage The serialize() function converts a storable representation of a value. To serialize data means to convert a value to a sequence of bits, so that it can be stored in a file, a memory buffer, or transmitted across a network.

How do you serialize data in JavaScript?

In JavaScript, for example, you can serialize an object to a JSON string by calling the function JSON. stringify() . CSS values are serialized by calling the function CSSStyleDeclaration. getPropertyValue() .


2 Answers

You can serialize all inputs inside an element like this:

var data = $('YourId :input').serialize()
like image 110
dortique Avatar answered Oct 12 '22 23:10

dortique


This is what I'm using

(function($){
$.fn.serializeAny = function() {
    var ret = [];
    $.each( $(this).find(':input'), function() {
        ret.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( $(this).val() ) );
    });

    return ret.join("&").replace(/%20/g, "+");
}
})(jQuery);

to use it in your case $('li.tagToggle.active').serializeAny();

like image 43
fedmich Avatar answered Oct 12 '22 23:10

fedmich