1

I just started learning jQuery and I got stuck on something. I have a form which ask for a name, adress, email and phonenumber. Now I would like to get all this info in just 1 variable when the submitbutton is pressed. Then I would like to show this variable in the next tab.

Can someone help me out?

1
  • 1
    Can you give the HTML for your form and tab? Commented Feb 14, 2011 at 20:20

2 Answers 2

3

Does the format matter? If not, this will get you the serialized form:

var value = JSON.stringify($('form').serializeArray());
alert(value);

Then you can pass around value however you like.

UPDATE

If you want something more user friendly you could try something like this. Since you havent provided your HTML, I'm making up a form.

<input id="fullname" type="text" />
<input id="email" type="text" />
<input id="phone" type="text" />
<input id="submitButton" type="button" />

<div id="results"></div>

And the jQuery would be something like:

$('#submitButton').click(function()
{
    var value = $('#fullname').val() + '<br/>' + $('#email').val() + 
                '<br/>' + $('#phone').val();

    $('#results').text(value);

});

UPDATE 2 If you are having trouble with the event handlers, you might want to try to stop the bubbling of the event. by adding the event parameter to the function and calling event.preventDefault()

$('#submitButton').click(function(event)
{
    //do stuff
    event.preventDefault();
});
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the fast response. Sorry but I dont understand your sollution. I don't wanna be using JSON. jQuery and AJAX should do the job. As far as I know, I'm supposed to start with something like: var info = ('#form')
The code I gave converts the first <form> on your page to the string representation of an equivalent JSON object. It will not be very user friendly if you want to display it to an end user. What exactly are you trying to accomplish on the next tab?
On the next tab the users should be able to check if their info is correct. If not, they can go back one tab to edit it again. Is that possible or does the 'wrong' info stay in that variable?
Thanks, this works. There's only 1 thing. When I press submit, all form fields are cleared and I only see a flash of the results. How can I fix this?
Is your button type="button" or type="submit"? type="submit" will post the form back to the server which will also cause the symptoms you explained. type="button" will not submit the form, but you will still get the button and the click event. If you don't need to send the form to the server at this point, then you should change the type to button.
|
0

HTML

<form>
    <input type="text" id="txtfirstname"/>
    <input type="text" id="txtlastname"/>
    <a href="#" id="submitForm">Submit</a>
</form>

javascript

$(function(){

     var person = new Object();

     $('#submitForm').click(function(event){

         person = new Object();
         person.FirstName = $('#txtfirstname').val();
         person.LastName = $('#txtlastname').val();
         //....

         //hide current tab.

         //initialize next tab with properties of person.

     });
})

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.