tirenweb
tirenweb

Reputation: 31709

jQuery: how to serialize data inside of form that are not inputs?

using serialize() I have no problems to serialize the values inserted in the input text fields of a form, but..how to add to that serialized data the content of <span> tags that are also inside of that form?

Upvotes: 0

Views: 2134

Answers (1)

Billy Moon
Billy Moon

Reputation: 58521

I would add the span contents to the form in hidden inputs, and then serialise the form as usual...

for html like this

<form id="myForm"><input type="text" name="myInput" /><span id="mySpan">this should be captured</span></form>

run this before serializing your form

$("#myForm span").each(function(i,v){
    $this = $(this)
    $("#myForm").append(
        $("<input type='hidden' />").attr({
            name:$this.attr('id'),
            value: $this.text()
        })
    )

})

Upvotes: 1

Related Questions