Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
103
rated 0 times [  109] [ 6]  / answers: 1 / hits: 25619  / 11 Years ago, fri, january 17, 2014, 12:00:00

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?


More From » jquery

 Answers
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.


[#73097] Thursday, January 16, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dahlias

Total Points: 730
Total Questions: 104
Total Answers: 101

Location: Denmark
Member since Tue, Jul 19, 2022
2 Years ago
;