-1

Have a code that updates a textarea:

var input_baseprice = $('.active .input_baseprice').html();

$('[name=baseprice]').html(input_baseprice);

However, when there are many .input_baseprice elements, the textarea gets content only from the first one.

How can I automate creation of input_baseprice* to get:

$('[name=baseprice]').html(input_baseprice + '<br>' + input_baseprice2 + '<br>' + input_baseprice3 ...);

?

1
  • So many great variants! Thanks a lot for all. Commented Jan 25, 2013 at 6:38

5 Answers 5

3

That is documented in the api notes http://api.jquery.com/html/

In order for the following 's content to be retrieved

Try this

var html = '';

$('.active .input_baseprice').each( function () {
    html += $( this ).html();
});

$('[name=baseprice]').html( html );

After re-reading your question, you may need to swap our .html() for .val() depending upon the type of element you are dealing with

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

Comments

2

Use .map method, then join them.

$('[name=baseprice]').html($('.active .input_baseprice').map(function() {
  return $(this).html();
}).get().join('<br>'));

Comments

1

use each to loop thrrough the textarea content... .val() should work try this

var str="";

$('.active .input_baseprice').each(function(){
      str += $(this).val();
 });

$('[name=baseprice]').val( str);

OR

in array ..

var temparray= [];

$('.input_baseprice').each(function(){
   temparray.push($(this).html());
}

 $('[name=baseprice]').html(temparray.join('<br>'));

Comments

1

Use the each() function to loop through each element and append the data to an array, which will then be combined into a string:

var data = [];

$('.input_baseprice').each(function(){
    data.push($(this).html());
}

$('[name=baseprice]').html(data.join('<br>'));

Comments

0

you can also create array by jQuery.makeArray()

 var arr = jQuery.makeArray(elements);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.