Answer the question
In order to leave comments, you need to log in
YII2 Connecting JS with binding to PHP variable?
Kind.
A very dumb question.
There is YII2, There is a Module that displays N buttons with objects on the form. You need to hang the same JS on each button. Differences in buttons only by ID. In JS, the differences are already in the input parameters from objects and the button code as a trigger. The code is the same.
I only managed to do it like this:
--- php ---
while ( $i <= $categoryCount) {
echo '<button id="btn'.$i.'" >';
echo "<script> $('btn".$i."').on('click', function() { console.log( $('btn".$i."') ); })</script>";
}
Answer the question
In order to leave comments, you need to log in
Good evening.
Give the buttons some css class.
In js, refer to this class and don't mix php with html.
<?php
while ( $i <= $categoryCount) {
?>
<button id="btn<?= $i ?>" >
<?php
}
// регистрируете js код
$this->registerJs("
$('button.className').on('click', function(){
console.log($(this).attr('id'))
})
", yii\web\View::POS_END);
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question