this is my html code. group.participants is an array.
result +=`<button class="gsb-${group.id}" onclick="demo(${group.participants})">`+"DEMO"+`</button><br/>`;
this is my simple javascript code to display the array from the parameter
function demo(participants){
alert(participants);
}
this shows me the error
Uncaught SyntaxError: Unexpected end of input
may I know what is the problem
DarkBee
14.4k9 gold badges86 silver badges135 bronze badges
1 Answer 1
With Jquery you can use the following to pass data to a selector
$(`.gsb-${group.id}`).data(group.participants);
to recover it you just have to call data() method
$(`.gsb-${group.id}`).data();
Finally like each group have different participants, you will have to append first the group button before add the data to it
result.append(`<button class="gsb-${group.id}" onclick="demo(${group.id})">`+"DEMO"+`</button><br/>`);
$(`.gsb-${group.id}`).data(group.participants);
function demo(groupId) {
var participants = $(`.gsb-${groupId}`).data();
console.log(participants);
}
var result = $('#result');
var group = {
id:1,
participants:[
{name:'test1'},
{name:'test2'}
]
}
result.append(`<button class="gsb-${group.id}" onclick="demo(${group.id})">`+"DEMO"+`</button><br/>`);
$(`.gsb-${group.id}`).data(group.participants);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="result"></div>
answered Mar 24, 2022 at 6:53
jeremy-denis
6,8963 gold badges24 silver badges37 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Venkat Cpr
yeah I'm using jqery and it's not working
DarkBee
Well, you cleary said that
participants is an array, so you will need to expand your array first e.g. data-participants="${group.participants.join(',')}"default
Unexpected end of inputis likely unrelated to the code you've provided, but can be easily tested by removing the php(?) parts, egresult +=`<button>DEMO</button><br/>`;to see if you still get the same error. If you don't then it will be caused by the content (actual values) ofgroup.participants(or, less likely,group.id)