0

i know this question already ask before and i tried to read that question like this question, but i still cannot resolve my problem.

i tried to add \ before " on every onclick but still not resolve my problem. and i also tried to change ' to " and " to '.

this is my code.

    function openchatwindow(kodeid,name,xxid){
        console.log("aaa");
        var chaton = $('#chaton'+kodeid).val();
        var isihtml = '<div class="conversation" id="conversation'+kodeid+'" style="display:none;height:350px"><div id="appenand"> <div class="conversation__header"><span onclick="minimazee('+kodeid+')">'+name+'</span><span class="close-msg"> <span onclick="minimazee('+kodeid+')" id="maxmin'+kodeid+'">&#x1f5d5;</span>   <span onclick="closee('+kodeid+')">&times;</span></span></div>';
        console.log(isihtml);
        isihtml = isihtml+'<ul class="conversation__wrap" id="logchat'+kodeid+'" style="overflow-y: scroll;height: 200px" >';
        isihtml = isihtml+'</ul></div><input type="text" id="isipesan'+kodeid+'" onclick="kirimpesanku('+kodeid+')" name="isipesan" style="resize: none;" class="input" placeholder="Tekan Enter untuk mengirim pesan.."/> ';
        console.log(isihtml);
        console.log("chaton = "+chaton);
        $('#tampungchat').append(isihtml);
    }

i also try to read all my code from console.log

and the log print out like this

<div id="tampungchat">
<p id="demo"></p>
<input type="hidden" name="jumlahchatbox" value="2" id="jumlahchatbox">
<input type="hidden" name="jumlahchatboxyangopen" value="2" id="jumlahchatboxyangopen">

<div class="conversation" 
id="conversation657C9312461DE5E68876FD96BBBA8280" 
style="right: 150px; height: 350px;">

    <div id="appenand"> 
        <div class="conversation__header">
            <span onclick="minimazee(657C9312461DE5E68876FD96BBBA8280)">asdf</span>
            <span class="close-msg"> 
                <span onclick="minimazee(657C9312461DE5E68876FD96BBBA8280)" 
                id="maxmin657C9312461DE5E68876FD96BBBA8280">
                
                </span>   
                <span onclick="closee(657C9312461DE5E68876FD96BBBA8280)">×</span>
            </span>
        </div>
        <ul class="conversation__wrap" 
        id="logchat657C9312461DE5E68876FD96BBBA8280" style="overflow-y: scroll;height: 200px">
            <li class="conversation__msg cf">
                <span>adsf</span>
            </li>
        </ul>
    </div>
    <input type="text" id="isipesan657C9312461DE5E68876FD96BBBA8280" onclick="kirimpesanku(657C9312461DE5E68876FD96BBBA8280)" 
    name="isipesan" style="resize: none;" class="input" placeholder="Tekan Enter untuk mengirim pesan.."> 
</div>
<div class="conversation" id="conversation4" style="right: 460px; height: 350px;">
    <div id="appenand"> 
        <div class="conversation__header">
            <span onclick="minimazee(4)">adsf</span>
            <span class="close-msg"> 
                <span onclick="minimazee(4)" id="maxmin4"></span>   
                <span onclick="closee(4)">×</span>
            </span>
        </div>
        <ul class="conversation__wrap" id="logchat4" style="overflow-y: scroll;height: 200px">
        <li class="conversation__msg cf">
            <span class="rightt">asdf</span></li>
        </ul>
    </div>
    <input type="text" id="isipesan4" onclick="kirimpesanku(4)" name="isipesan" style="resize: none;" class="input" 
    placeholder="Tekan Enter untuk mengirim pesan.."> 
</div>

and i already check this code and i don't get any invalid code or wrong code.

but why every i click on this input text

<input type="text" id="isipesan657C9312461DE5E68876FD96BBBA8280" onclick="kirimpesanku(657C9312461DE5E68876FD96BBBA8280)" name="isipesan" style="resize: none;" class="input" placeholder="Tekan Enter untuk mengirim pesan..">

i always get this error....in my browser Uncaught SyntaxError: missing ) after argument list

maybe someonve can help me, i just wonder why i can get this error.

Community
  • 1
  • 1
  • Look at the line it indicates in the error. Also, you didn't include the `kirimpesanku()` function definition, so there is no way for anyone to help you. – forgivenson Mar 21 '17 at 17:48
  • @forgivenson i get the error at line 1, `index:1` –  Mar 21 '17 at 17:52

2 Answers2

0

onclick="kirimpesanku(657C9312461DE5E68876FD96BBBA8280)" is invalid. It should be onclick="kirimpesanku(\"657C9312461DE5E68876FD96BBBA8280\")".

To produce that string in JavaScript you can do:

var html = 'onclick="kirimpesanku(\\"'+kodeid+'\\")"';

It looks a little strange because you need to escape the escape character but should produce the correct result.

If you want to avoid having to count characters consider doing something like this:

var html = 'onclick="kirimpesanku(' + JSON.stringify(JSON.stringify(kodeid)) + ')"';

Alternating between ' and " can make things easier but sometimes it's nice to always use ":

var html = "onclick=\"kirimpesanku(" + JSON.stringify(JSON.stringify(kodeid)) + ")\"";
Halcyon
  • 57,230
  • 10
  • 89
  • 128
  • i get this error `Uncaught SyntaxError: Unexpected token %` after change to ` onclick="kirimpesanku('+escape(JSON.stringify(kodeid))+')"` –  Mar 21 '17 at 17:57
  • i don't have any `%`. i tried `onclick="kirimpesanku(\\"'+kodeid+'\\")"` and i get `Uncaught SyntaxError: Invalid or unexpected token` –  Mar 21 '17 at 17:59
  • Ah, my bad. Use `JSON.stringify()` again. I thought `escape()` would escape the `"` (wrong language) but that's not what it does. – Halcyon Mar 21 '17 at 17:59
  • ` onclick="kirimpesanku('+JSON.stringify(kodeid)+')"` i am getting `Uncaught SyntaxError: Unexpected token }` –  Mar 21 '17 at 18:01
  • You need to stringify _twice_. Once to get the literal and once to get the escaped form. – Halcyon Mar 21 '17 at 18:02
0

try to embrace the whole id with a quote:

var isihtml = '<div class="conversation" id="conversation'+kodeid+'" style="display:none;height:350px"><div id="appenand"> <div class="conversation__header"> <span onclick="minimazee(';

var isihtml += "'" + kodeid + "'";

var isihtml += ')">'+name+'</span><span class="close-msg"> <span onclick="minimazee('+kodeid+')" id="maxmin'+kodeid+'">&#x1f5d5;</span>   <span onclick="closee('+kodeid+')">&times;</span></span></div>';
Felipe Mariano
  • 564
  • 5
  • 8