0

I'm trying to get all my form inputs and then create with them an xml string containing there values in order to send them via ajax.

i want this :

<form id="formtest">
 <input type="text" name="test1" id="test1"/>
 <input type="text" name="test2" id="test2"/>
 <input type="text" name="test3" id="test3"/>
 <input type="text" name="test4" id="test4"/>
 <input type="text" name="test5" id="test5"/>           
</form>

to become:

<formtest>
  <test1></test1>
  <test2></test2>
  <test3></test3>
  <test4></test4>
  <test5></test5>
</formtest>

How can this be done?

Thank's In Advance.

2
  • I want to get all inputs and serialize them in xml shape and send them in ajax , with xml string as a parameter. Commented Feb 6, 2011 at 14:30
  • @user590586 Please show us the code that you have tried. Commented Feb 6, 2011 at 14:40

2 Answers 2

2

First, create your XML document within jQuery:

var xml = $('<xmlBody></xmlBody>'); //<xmlBody> can be replaced with whatever tag is appropriate for your uses

Add values like such:

xml.find('xmlBody').first().append('<tag>'+data+'</tag>');

You may need a plugin to serialize the XML. This, perhaps?

This is a very simple example, but hopefully it will get you started. Look around the jQuery docs for more info.

Sign up to request clarification or add additional context in comments.

5 Comments

@CloudedVision: thank's for your reply . but this is not what i ment.. my question is how to create the "data" inside the tags.. i want all the inputs to be shown as xml tags that inside of them their values..
Do you mean you want to serialize the jQuery XML object that you have? I edited my answer, it should cover that now.
@user590586 The answer given by @CloudedVision is perfect to do that. What issue do you see?
@Ritesh: from what i see in the attached link it creates an xml like .html() , but i want only the id of the input and it's value.
@user590586 I think you just need to replace root, tag and value in answer given by @CloudedVision. See the snippet in my answer below.
1
    var xml = $('<formtest></formtest>');

$("form#formtest input").each(function(){
    xml.find('formtest').first().append('<' + $(this).attr('id') + '>' + $(this).val() + '</' +  $(this).attr('id') + '>');
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.