jQuerybeast
jQuerybeast

Reputation: 14490

Append() input's val() using each()

So I have an input of where when I submit I'm trying to append its value to a div.

For example:

$('input').submit(function () {
   $('#div').append($('input').val());
   return false;
});​

Now In my actual example, the appended value of the input will be within a layout so I am trying to use a quicker/safer/better(?) method when using each().

According to http://www.learningjquery.com/2009/03/43439-reasons-to-use-append-correctly I am trying to use something like this:

$('input').submit(function () {

   var arr = [];
   var a = '';

    $.each(arr, function(count, item) {
      a += '<div class="a_txt">' + $('input').val() + '</div>';
    });

   $('div').append(a);
   return false;

});​

What I'm I missing or doing wrong?

Basically I want to append the input's value into a layout.

For example:

<div class="a_txt"> ... </div>

What I'm I missing or doing wrong?

Upvotes: 1

Views: 1601

Answers (1)

adeneo
adeneo

Reputation: 318182

Looks like you are trying to do some variation of this:

$('input').submit(function () {
   var a = '';
   $('input').each(function() {
      a += '<div class="a_txt">' + $(this).val() + '</div>';
   });

   $('div').append(a);
   return false;
});​

Upvotes: 2

Related Questions