HTML replaceWith function with PHP

I am trying to execute this code in javascript:

 $('#content').replaceWith("<div>" + <?php echo $tableau_Text?> + "</div>");

But is not working for me. It shows nothing to me. When I change the PHP code for something static like:

 $('#content').replaceWith("<div>Hello World</div>");

it works perfectly with that.

My html #content definition is:--

<div id="content">
    <?php echo $show_menu;?>
</div>

Any idea? Thank you very much!

UPDATE:

The var $tableau_text looks like this:

$tableau_Text = "<ul class='object_list' id='object_list' data-role='listview' data-inset='true' data-theme='c'>$TableauText</ul>";

Where $TableauText are the list items in the same format.

$TableauText = <li class='menuA' id='menuA' data-role='list-divider' data-icon='false'>River Flow</li>
<li class='menuB' id='menuB' data-icon='arrow-r'><a href='#' target='foo'>Rossiniere</a></li>
<li class='menuB' id='menuB' data-icon='arrow-r'><a href='#' target='foo'>Montsalvens</a></li>
<li class='menuA' id='menuA' data-role='list-divider' data-icon='false'>Lake Level</li>
<li class='menuB' id='menuB' data-icon='arrow-r'><a href='#' target='foo'>Rossiniere</a></li>
<li class='menuB' id='menuB' data-icon='arrow-r'><a href='#' target='foo'>Montsalvens</a></li>
<li class='menuB' id='menuB' data-icon='arrow-r'><a href='#' target='foo'>Lessoc</a></li>
<li class='menuB' id='menuB' data-icon='arrow-r'><a href='#' target='foo'>Rossens</a></li>
<li class='menuB' id='menuB' data-icon='arrow-r'><a href='#' target='foo'>Maigrauge</a></li>
<li class='menuB' id='menuB' data-icon='arrow-r'><a href='#' target='foo'>Schiffenen</a></li>
<li class='menuB' id='menuB' data-icon='arrow-r'><a href='#' target='foo'>Maigrauge</a></li>

Solution: I had several list elements with the same id which I didn't realized after I paste it here and that was making my code crazy...

Answers:

Answer

Try this:

$('#content').replaceWith("<div>" + <?php echo json_encode($tableau_Text); ?> + "</div>");

Given:

$TableauText = 'hello world ""';

$tableau_Text = "
        <ul class='object_list' id='object_list' data-role='listview' data-inset='true' data-theme='c'>
        $TableauText
        </ul>";

The browser will see:

$('#content').replaceWith("<div>" + "\n        <ul class='object_list' id='object_list' data-role='listview' data-inset='true' data-theme='c'>\n        hello world \"\"\n        <\/ul>" + "</div>")?

Which renders as

hello world ""
Answer

Don't treat php code as variable

Try this

$('#content').replaceWith("<div><?php echo $tableau_Text?></div>");
Answer

Unless your <?php echo $tableau_Text?> outputs the relevant text surrounded in quotes, you'll end up with invalid string concatenation and therefore a JavaScript syntax error. However, since the PHP is executed before the JavaScript gets to the browser, you don't need to use string concatenation at all.

Answer

One counter question : is your code

$('#content').replaceWith("<div>" + <?php echo $tableau_Text?> + "</div>");

in .js file?

if so this want work. and you shoul do this before including js

var tableau_Text  = ' <?php echo $tableau_Text?>';
<script src = 'any_js.js'></script>

and in .js

$('#content').replaceWith("<div>" + tableau_Text + "</div>");

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.