3

I have problem while convert html to javascript variable.

My HTML Code

<div onclick="openfullanswer('2','Discription part','This is the code part');">

I want to create this html code dynamically, but I have the problem in quote(" and ')

I tried like below

for(i=0;i<result.length;i++)
{       
        strVar +="<div onclick='openfullanswer("+result[i].ReplyID+",'"+result[i].Description+"','"+result[i].Code+"');'>Test code</div>";
}

https://jsfiddle.net/8782n60z/1/

Merbin Jo
  • 1,189
  • 1
  • 11
  • 19

6 Answers6

1

Way 1: You should have to change the sequence of single and double quotes and have to escape the single quotes with '\' in on-click function arguments

Please check the below snippet for more understanding.

var result=[];
var obj=new Object();
obj.ReplyID=1;
obj.Description="This is my description";
obj.Code="This is my Code Part";

result.push(obj);



strVar="";
for(i=0;i<result.length;i++)
{       
  strVar +='<div onclick="openfullanswer(\''+result[i].ReplyID+'\',\''+result[i].Description+'\',\''+result[i].Code+'\');">Test code</div>';
}

document.getElementById("test").innerHTML=strVar;


function openfullanswer(replyid,desc,code)
{
  alert("success");

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test">

</div>

Way 2: Don't change the sequence of quotes. Only change the onclick event declaration quotes from signle to double and escape double quotes with '\' and you need not have to change anything.

Please check the below snippet for more understanding.

var result=[];
var obj=new Object();
obj.ReplyID=1;
obj.Description="This is my description";
obj.Code="This is my Code Part";

result.push(obj);


strVar="";
for(i=0;i<result.length;i++)
{       
    strVar +="<div onclick=\"openfullanswer("+result[i].ReplyID+",'"+result[i].Description+"','"+result[i].Code+"');\">Test code</div>";
}

document.getElementById("test").innerHTML=strVar;


function openfullanswer(replyid,desc,code)
{
alert("success");

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test">

</div>
Rahul Patel
  • 5,248
  • 2
  • 14
  • 26
1
strVar +="<div onclick=\"openfullanswer('"+result[i].ReplyID+"','"+result[i].Description+"','"+result[i].Code+"');\">Test code</div>";

try this.

sss
  • 11
  • 2
1

The code you wrote produces the following:

<div onclick='openfullanswer(1,'This is my description','This is my Code Part');'>Test code</div>

The above is not valid since there are single quotes inside the value of onclick.

You will need to escape these characters.

strVar +="<div onclick=\"window.openfullanswer("+result[i].ReplyID+",'"+result[i].Description+"','"+result[i].Code+"');\">Test code</div>";

See here an article in SO explaining this matter in detail.

See a working demo here: https://jsfiddle.net/8782n60z/3/

P.S. I changed a bit the way to declare and call the function because it gave an error.

Community
  • 1
  • 1
Tasos K.
  • 7,979
  • 7
  • 39
  • 63
1

You can use template literal

var result = [];
var obj = new Object();
obj.ReplyID = 1;
obj.Description = "This is my description";
obj.Code = "This is my Code Part";

result.push(obj);

strVar = "";
for (i = 0; i < result.length; i++) {
  strVar += `<div 
               onclick="openfullanswer('${result[i].ReplyID}'
                                       , '${result[i].Description}'
                                       , '${result[i].Code}');">
               Test code
             </div>`;
}

function openfullanswer(replyid, desc, code) {
  alert("success");
  console.log(replyid, desc, code);
}

document.getElementById("test").innerHTML = strVar;
<div id="test">

</div>

jsfiddle https://jsfiddle.net/8782n60z/5/

guest271314
  • 1
  • 15
  • 104
  • 177
0

Instead of building the string you could also use document.createElement to generate your div. And then append it to the parent #test

var result=[];
var obj=new Object();
obj.ReplyID=1;
obj.Description="This is my description";
obj.Code="This is my Code Part";

result.push(obj);

var div;
for(i=0;i<result.length;i++)
{       
  div = document.createElement('div');
  div.innerHTML = 'Test code'
  div.onclick = openfullanswer;
}
document.getElementById("test").appendChild(div);

function openfullanswer(replyid,desc,code)
{
  alert("success");
}
<div id="test">

</div>
Jairo Dev
  • 51
  • 4
  • Thanks for answering, but I can not able to solve my problem by the method because, I am generating not only this div. – Merbin Jo Sep 24 '16 at 09:18
  • You shouldn't have to build complex DOM elements with strings. If that array is going to have several objects and you want a div with a onclick callback for each object... Check this out: https://jsfiddle.net/o9ctpuno ... I did it my way. Hope it helps, and I'd be glad to explain it further if you need. – Jairo Dev Sep 24 '16 at 09:58
0

Since what you need has already been answered above, I'll just add that the way you are trying to access and manipulate DOM element is not safe or recommended way of handling dom elements. Instead of directly writing to DOM, its better to make use of addEventListner() function and bind your element to it.

For instance, instead of writing ,

strVar +="<div onclick='openfullanswer("+result[i].ReplyID+",'"+result[i].Description+"','"+result[i].Code+"');'>Test code</div>";

you should be instead doing

strVar.outerHTML += '<input id="btncall" ...>'

document.getElementById ("btncall").addEventListener ("click", openfullanswer(parameters), false);
Cyclotron3x3
  • 2,188
  • 23
  • 40