Jakub Hampl
Jakub Hampl

Reputation: 219

Implementing variable in id

enter image description here

Is there a way to implement variable 'o' instead of '1' in #po_am_1 ? Thank you.

var pondeli_in = [];
var o = 0;
$('input[name="pondeli"]:checked').each(function () {
   pondeli_in.push(($(this).attr('value')) + (' (') + ($('#po_am_1').val()) + ('x)') + ("<br>"));
   o += 1;
});

Upvotes: 0

Views: 54

Answers (4)

Rayees AC
Rayees AC

Reputation: 4659

var pondeli_in = [];
var o = 0;
$('input[name="pondeli"]:checked').each(function () {
   pondeli_in.push(($(this).attr('value')) + (' (') + ($('#po_am_'+o).val()) + ('x)') + ("<br>"));
   o += 1;
});

Upvotes: 0

Ivan Karaman
Ivan Karaman

Reputation: 1234

you can avoid using an extra variable for index

var pondeli_in = [];
$('input[name="pondeli"]:checked').each(function (i, el) {
    pondeli_in.push(($(el).attr('value')) + (' (') + ($('#po_am_' + i).val()) + ('x)') + ("<br>"));
});

Upvotes: 1

Caner Sevince
Caner Sevince

Reputation: 39

you can. with string interpolation.

$(`#po_am_${o}`)

Upvotes: 0

Adam Azad
Adam Azad

Reputation: 11297

ES6 template literals or string concatenation. Pick one.

$(`#po_am_${o}`)

$('#po_am_' + o)

Upvotes: 1

Related Questions