-3

I would like to add a function in an onclick with 3 parameters: int, int, string. The string parameter that contains an HTML and other quotations. Would it be possible?. I saw it here pass string but it is not working on my case.

 <p onclick="myfunction(0,3,'<b">FirstName =
 \"Dondellx\"\n<b>LastName</b> = \"Batacx\"\n<b>path</b> = 
 \"xxxpath\""');dondell
 </p>

This function that contains the String with many \\ characters are actually from a JSON file which I put in a "p" tag in a loop when reading the JSON.

This is what i am doing to get the JSON item and put to the string param;

//This is a for loop
<p onclick="myfunction(0,3,'dataFromJSon[0]')"></p>
<p onclick="myfunction(0,3,'dataFromJSon[1]')"></p>
<p onclick="myfunction(0,3,'dataFromJSon[2]')"></p>

enter image description here

This is the text editor to enter my Text. As you can see, I have quotations in the textArea, so that is why i need to save it with qoutations. :

enter image description here

This is the JSON:

enter image description here

Community
  • 1
  • 1
Dondell Batac
  • 113
  • 1
  • 10
  • Your question is very unclear. The html in your function is very mis-formed as well. I would assume if anything that is your issue, but you need to post more details. A jsfiddle of what you are trying to accomplish and where it is exactly failing would go a long way. – peinearydevelopment Jul 06 '16 at 14:07
  • Hi @peinearydevelopment. Thanks for the comment. I have updated my question. I hope it is more clearer now. Please your welcome to add comment if it is not yet clear. – Dondell Batac Jul 06 '16 at 14:59

2 Answers2

1

This is certainly because there are so many syntax error in you HTML and JavaScript code.

Not correctly escape quote, not escape line break inside a javascript string declaration, HTML tag not properly close, attribute value not correctly defined...

Maybe try this (I have implement a fake myfunction for exemple and replace \n by <br/>, the line break not make sense in HTML).

function myfunction(a,b,str){
  var row = document.createElement('div');
  row.innerHTML = str;
  document.body.appendChild(row);
}
<p onclick="myfunction(0,3,'<b>FirstName = &quot;Dondellx&quot<br/><b>LastName</b> = &quot;Batacx&quot;<br/><b>path</b> = &quot;xxxpath&quot;');">dondell
</p>
Techniv
  • 1,967
  • 15
  • 22
1

Your html is misformed indeed, so I created a simpler demo. Hopefully it'll still be useful as I use an object to pass the html code as well as the other parameters.

var object = {a:0,b:3,c:'<b>FirstName=</b>Dondellx'};
function myfunction(obj){
  document.getElementById("out").innerHTML=obj.c;
}
<p onclick="myfunction(object)">click</p>
<div id="out"></div>
Theodore K.
  • 5,058
  • 4
  • 30
  • 46