Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

HTML To Javascript function??


I have a php variable that generated by spaw editor.
I have javascript function that save a text into an xml file. (i.e. function  GetContent(text)).
The problem is when I pass the php variable to the getfunction(text) does not work if it contains an HTML tags.(i.e. If does not conatin HTML tags it works correctly).

This is my javascript function:

function GetContent(text) {

element_id = <?=$doc?> * 1000 + 1 ;
title_count = 0;
title_is_first = true ;


          str = new String;
          str = text;
          xml_doc +='<PAR ID="'+element_id+'"><![CDATA[' + str + "]]></PAR>" ;
          element_id ++;
xml_doc += "</DOC>"

     document.edit.xml_doc.value = xml_doc ;

This the for the function:

<input type="button" onClick="GetContent('<? echo $text; ?>')" value="      U p d a t e          "></input>

Thanks for your response
  • 2
  • 2
1 Solution
ZvonkoSystems architectCommented:
Put in fron of your function some global var.
Like this:

var theText = '<? echo $text; ?>';

function GetContent(text) {...

And your button is then like this:
<input type="button" onClick="GetContent(theText)" value="      U p d a t e          ">

ZvonkoSystems architectCommented:
Or if you have several text contents, then put them beside your button.

Like this:
<script>text1='<? echo $text; ?>'</script>
<input type="button" onClick="GetContent(text1)" value="      U p d a t e          ">
<script>text2='<? echo $text2; ?>'</script>
<input type="button" onClick="GetContent(text2)" value="      U p d a t e  Two         ">

$text = str_replace("'", "&#039;", $text;);
$text = str_replace("\"", "&quot;", $text;);
<input type="button" onClick="GetContent('<? echo $text; ?>')" value="      U p d a t e          "></input>
so above example was simple replace on server.

and here client solution:
<span id=cont><? echo $text; ?></span>
<input type="button" onClick="GetContent(document.getElementById('cont').innerHTML)" value="      U p d a t e          "></input>

the problem was not html, but the quotes. i think so :)

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now