1

I am developing an mobile app in which i want to populate ListView once the user fill up the form and click on submit button.

I know how to give List View in jQuery but how to populate items in it dynamically at run time?

3
  • 1
    Checkout handlebarsjs.com, literally doing this exact thing in one of my projects right now. Commented Jan 17, 2014 at 18:03
  • I am new to this so i just went through the api.jquerymobile.com/listview but don't have any idea how to proceed. please do provide some tips Commented Jan 17, 2014 at 18:04
  • I second using handlebarsjs Commented Jan 17, 2014 at 18:04

2 Answers 2

5

Are you looking for something like this?

jsFiddle Demo:

HTML:

<div id="listView"></div>
<input type="button" id="mybutt" value="Submit" />

javascript/jQuery:

$('#mybutt').click(function() {
    var out = '<ul><li>Item One</li><li>Item Two</li><li>Item Three</li></ul>';
    $('#listView').html(out);
});

Responding to your comment: "what i need is on click of button form gets submitted and firstname which user enters on form will get added in list"

First, you need to remain on the page after the form was submitted. To do that, you should add e.preventDefault(); in your submit routine:

$( "#target" ).submit(function( event ) {
    //Manually collect form values and 
    //Use ajax to submit form values here (see notes at bottom)
    event.preventDefault();
});

Next, you want to get the data that was in the desired field, and add that to the <ul>. Therefore, change the above like this:

$( "#target" ).submit(function( event ) {
    var fn = $('#fname').val();
    $('ul').append('<li>' +fn+ '</li>');

    //Manually collect form values and 
    //Use ajax to submit form values here (see notes at bottom)
    event.preventDefault();
});

For the AJAX bit, see this post for tips.

Note that you can use $('#formID').serialize(); to quickly serialize all form data.

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

1 Comment

in above example it is hard coded items but what i need is on click of button form gets submitted and firstname which user enters on form will get added in list.
1

js fiddle:

http://jsfiddle.net/7PzcN/1/

html:

<div id="listView"></div>
<input type="text" name="firstname"/>
<input type="text" name="lastname"/>
<input type="button" id="mybutt" value="Submit" />

jquery:

$('#mybutt').click(function() {
    var out = '<ul>';
    $("input[type=text]").each(function() {
        out += "<li>" + $(this).val() + "</li>";
    });
    out += "</ul>";
    $('#listView').html(out);
});

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.