HTML To Javascript function??

Posted on 2004-04-06
Last Modified: 2013-12-16

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
Question by:ALNMOO
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 63

Expert Comment

ID: 10769998
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          ">

LVL 63

Expert Comment

ID: 10770017
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         ">

LVL 25

Accepted Solution

devic earned 500 total points
ID: 10770325
$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>
LVL 25

Expert Comment

ID: 10770349
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 free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article provides a case study on how our local youth baseball league deployed a new website, including the platform selection, implementation and benefits to the league.
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
The purpose of this video is to demonstrate how to automatically show related posts at the bottom of a blog post in WordPress. This will be demonstrated using a Windows 8 PC. Plugin “Yet Another Related Posts Plugin” will be used. Go to your…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question