I am trying to serialize the current div by using a clickevent. See code for good explanation:
JavaScript
x
7
1
$("#createreview").live('click', function() {
2
alert($(this).parent().serialize());
3
$.post('/admin/home/review/create/', $(this).parent().serialize(), function(data){
4
alert('Review successfully added to database.' +data);
5
});
6
});
7
Alert will show nothing, which means the div is not being serialized. This is the html in question. It’s inside a dialog, which is also the reason why I have to use live()
.
JavaScript
1
6
1
<div id="reviewdiv">
2
<input type="hidden" value="7" name="homeid">
3
<label>Content</label><textarea name="reviewcontent" id="reviews" rows="3" cols="60"></textarea><br>
4
<label>Author</label> <input type="text" name="reviewauthor"><button class="ui-state-default ui-corner-all" id="createreview">Save Review</button><button class="ui-state-default ui-corner-all" id="removereview">Remove Review</button><br>
5
</div>
6
Also this code is working without a problem to remove the current div by using the clickevent and parent selector.
JavaScript
1
4
1
$("#removereview").live('click', function() {
2
$(this).parent().slideUp();
3
});
4
Advertisement
Answer
You can serialize a <div>
.
The serialize uses internally the param
method. So to serialize input elements that do not belong to a form, use the param
method. Example: http://jsfiddle.net/DdVqJ/
But remember, the correct (and semantic) way is to put your input elements inside a form.