Reputation: 4496
Hello I have many rows with this structure:
<div class="row">
<div class="input-group col-lg-1" >
<span class="input-group-addon">
<input type="checkbox">
</span>
<span id="value">
@Html.DisplayFor(modelitem => item.DeviceInstanceId)
</span>
</div>
*
*
*
</div>
where @Html.DisplayFor(modelitem => item.DeviceInstanceId)
is a number.
I get the values of spans connected with checked checkboxes. With this code:
$(document).ready(function () {
$("#reservations").click(function () {
var Ids = new Array();
$(".table input:checked").each(function () {
Ids.push(($(this).parentsUntil("row").children("#value").text()));
})
console.log(Ids);
});
});
And the Ids looks like this:
["↵ 38↵ ", "↵ 40↵ ", "↵ 41↵ "]
How I should improve my JS code to get rid of those enters and whitespaces?
Upvotes: 0
Views: 274
Reputation: 85
.children('#value')
will look only one level down
.find('#value')
will look more than one level down.
in your case the span with id "value" is actually two levels down caompared to class "row"
Upvotes: 0
Reputation: 816364
You can either:
Ids.push($.trim($(this).parentsUntil("row").children("#value").text()));
which removes leading and trailing whitespace characters, or
Write your HTML so that it doesn't contain line breaks and spaces:
<span id="value">@Html.DisplayFor(modelitem => item.DeviceInstanceId)</span>
Upvotes: 3