Javascript Replace Method on innerHTML

Posted on 2007-10-10
Last Modified: 2012-08-14
I am trying to replace certain character strings found in the innerHTML and then once changed, write it out to a new HTML page.  
Here is my function which I invoke from a button onClick event:
function printPlanList(id) {
    var disp_setting="toolbar=no,location=no,directories=no,menubar=no,";
    disp_setting+="scrollbars=yes,width=820, height=450, left=100, top=25";
    var content_vlue = new String(eval("document.getElementById('"+id+"').innerHTML"));
    content_vlue.replace(/assignpicn/g, "assignpicp");  // make pics visible
    content_vlue.replace(/ztextview/g, "clickDummy");   // disable visibility function
    content_vlue.replace(/button/g, "hidden");          // make buttons invisible
   docprint.document.write('<html><head><title>Print Plan</title>');
   docprint.document.write('<STYLE type=text/css media=all>');
   var css = "@import url('css/mystyles.css');";
   docprint.document.write("<script language='javascript' src='javascript/zcommon.js' type='text/javaScript'>");
   docprint.document.write("<script language='javascript' src='javascript/zplan.js' type='text/javaScript'>");
   docprint.document.write('</head><body onLoad="self.print()">');

It is working to open the new window and to write the contents, except the REPLACE methods I am using does not change any of the string text I want.  Any ideas on how to perform this function?
Question by:mamuscia
    LVL 23

    Expert Comment

    by:Ashish Patel
    Yes please reload the window document again and it should come.

    Author Comment

    I don't understand your answer.  I am changing the contents of the HTML prior to writing it to the new window so when I do the writes it should be the changed text already.  I'm not sure why the Javascript replace method isn't working on the string contents I put into the content_vlue string from the innerHTML contents.  

    Why does it only work if I Reload the page?
    LVL 23

    Expert Comment

    by:Ashish Patel
    So did you try reloading and did it work after reload?
    LVL 54

    Accepted Solution

    I see one error in what you have ...

        content_vlue = content_vlue.replace(/assignpicn/g, "assignpicp");  // make pics visible
        content_vlue = content_vlue.replace(/ztextview/g, "clickDummy");   // disable visibility function
        content_vlue = content_vlue.replace(/button/g, "hidden");          // make buttons invisible

    Let me know how that change works.


    Author Comment

    I did try ReLoad and the same HTML is present and no changes were made to the HTML string contents.

    Geeze - how dumb.  I did the solution that you outlined and it works like a charm.  I needed to assign the value of content_vlue to the new value after replacing the strings.


    Author Comment

    LVL 54

    Expert Comment

    :D  Your welcome!  It is an easy mistake to make, especially if you usually write the result of the replace() or use other languages.

    I'm glad I could help.  Thanks for the grade, the points and the fun question.


    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now