Muhammed Sami
Muhammed Sami

Reputation: 110

Javascript send object as paramater

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'>&#xf06e;</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

Answers (2)

Zakaria Acharki
Zakaria Acharki

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'>&#xf06e;</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

cнŝdk
cнŝdk

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'>&#xf06e;</i>";
console.log(opt);

Upvotes: 1

Related Questions