Pekka
Pekka

Reputation: 1115

appending object values in jquery

<div class="col-xs-1 table-bordered text-sm-center"> <h6><label class="col-form-label small newvalues deduction">'+$.each( deductionname, function( i, val ) {console.log(val);return val + "<br>"  })+'</label></h6></div>\n\

I have this dynamically added on my page using Jquery the value of deduction is like this

["SSS:1000", "GSIS:1000"]

I want to have the value on top of each other but right now it is not what is happening is i am getting like this

SSS:1000,GSIS:1000

I dont get it because I added <br> on the dynamically adding of the values

I want them to be

SSS:1000,
GSIS:1000

Upvotes: 2

Views: 39

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337713

The issue is because you're appending an each() call. The return value has no bearing on the string you're concatenating. Instead the array itself is coerced to a string, hence you see the plain SSS:1000,GSIS:1000 output.

To fix this you can use join() to combine the values of the string before you concatenate them. Try this:

var deductionname = ["SSS:1000", "GSIS:1000"];
var str = '<div class="col-xs-1 table-bordered text-sm-center"> <h6><label class="col-form-label small newvalues deduction">' + deductionname.join(',<br />') + '</label></h6></div>';

$('div').html(str);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>

Upvotes: 3

Related Questions