I try to append a couple of buttons with click event when the component is mounted:
data() {
return {
show_word: true,
game: {
//type: "memory, working memory",
type: 1,
name: "Symbols",
start:false,
total_combo:0,
total_correct:0,
total_incorrect:0,
total_correct_in_a_row:0,
max_correct_in_a_row:0,
correct_percent:0,
avg_time_for_ans:0,
button: '<button @click="checkNumbers">click</button>',
score_plus:0,
score_multi:0,
d_timer:600,
finish:false,
positive: null,
negative: null,
numbers_array:[],
abuse: {
clicks:5,
seconds:1,
max:3,
flag: false
}
},
}
},
methods:{
playGame(){
let tilesize = 50, tilecount = 6;
$( '#numbersContainer' ).empty()
let gRows = Math.floor($("#numbersContainer").innerWidth()/tilesize);
let gCols = Math.floor($('#numbersContainer').innerHeight()/tilesize);
let vals = _.shuffle(_.range(tilecount));
let xpos = _.shuffle(_.range(gRows));
let ypos = _.shuffle(_.range(gCols));
console.log(vals);
console.log(xpos);
console.log(ypos);
let $this = this;
_.each(vals, function(d,i){
var $newdiv = $('<button @click="checkNumbers('+(d+1)+')"/>').addClass("tile btn btn-info");
$this.game.numbers_array.push(d+1)
$newdiv.css({
'position':'absolute',
'left':(xpos[i] * tilesize)+'px',
'top':(ypos[i] * tilesize)+'px'
}).appendTo( '#numbersContainer' ).html(d+1);
});
},
checkNumbers($val){
console.log($val)
},
}
This is the html that i getting:
<div id="numbersContainer" class="col-ms-3">
<button @click="checkNumbers(6)" class="tile btn btn-info" style="position: absolute; left: 250px; top: 250px;">6</button>
<button @click="checkNumbers(5)" class="tile btn btn-info" style="position: absolute; left: 150px; top: 150px;">5</button>
<button @click="checkNumbers(1)" class="tile btn btn-info" style="position: absolute; left: 100px; top: 0px;">1</button>
<button @click="checkNumbers(3)" class="tile btn btn-info" style="position: absolute; left: 50px; top: 100px;">3</button>
<button @click="checkNumbers(2)" class="tile btn btn-info" style="position: absolute; left: 200px; top: 200px;">2</button>
<button @click="checkNumbers(4)" class="tile btn btn-info" style="position: absolute; left: 0px; top: 50px;">4</button>
</div>
The problems is that the "click" event is not working. Seems like the "checkNumbers" function is not register, although it registered in my methods.
What is the right way to append html element with register events?
Thank !
datadeclaration?