Reputation: 110
I've got a long HTML created with js and I need to pass an onclick
event to an icon and I want to send a long object as a parameter with other needs like that:
var opt = "<i style='font-size:18px; color:orange; cursor:pointer;' onclick=\"barkod_onizle_dialog.apply('"+vs_id+"',"+stok_ozet+",'"+b.stok_id+"','"+$('[name=tarih]').val()+"','"+secilmis_depo+"','"+b.stok_id+"','"+seri+"','"+urun_stt+"','"+lot+"','"+b.stok_isim+"',this)\" class='fa'></i>";
But on my browser, I get this result :
barkod_onizle_dialog.apply('54',[object Object],'1508','03.10.2018','11','1508','155','2022-10-02','156','ADVIA -DIRUI R1 KABI (75 mL)',this)
And the error is :
Uncaught SyntaxError: Unexpected identifier
I could not solve it with quote approaches.
Thanks for your help.
Upvotes: 1
Views: 69
Reputation: 67525
Your stok_ozet
variable contains an object that why it shows [object Object]
in the concatenated string, so you may need to convert it to a string during the concatenation like :
onclick=\"barkod_onizle_dialog.apply('"+vs_id+"',"+JSON.stringify(stok_ozet)+",'..
I suggest also to attach the click in your JS code if you can :
var opt = "<i style='font-size:18px; color:orange; cursor:pointer;' class='fa barkod_onizle_dialog'></i>";
$('body').on('click', '.barkod_onizle_dialog', function() {
barkod_onizle_dialog.apply(vs_id, stok_ozet, b.stok_id, $('[name=tarih]').val(), secilmis_depo, b.stok_id, seri, urun_stt, lot, b.stok_isim, this);
})
Upvotes: 1
Reputation: 32145
The problem her is that you are trying to concatenate an object
to a string
, so the method toString()
of this object
is called and that's why it is represented as [object object]
.
You need to stringify
this object
before trying to concatenate it, you need to use JSON.stringify()
over your objects before concatenating them to the string
.
Demo:
var obj = {a: 40, b: 60, c:'a string'};
var opt = "<i style='font-size:18px; color:orange; cursor:pointer;' onclick=\"barkod_onizle_dialog.apply('',,'','"+JSON.stringify(obj)+"','','','','','','',this)\" class='fa'></i>";
console.log(opt);
Upvotes: 1