2

I'm having trouble replacing text with a div that includes a checkbox. When I have the following code, the text successfully gets replaced:

<p>Hello</p
<p class="replaced">Hello</p>
<p class="replaced">World</p>

<script>
$( ".replaced" ).replaceWith("<div>REPLACING TEXT.</div>");

</script>

When the replacing text includes a checkbox, however, the script stops working. To be specific, the following does not work:

<p>Hello</p
<p class="replaced">Hello</p>
<p class="replaced">World</p>

<script>
$( ".replaced" ).replaceWith("<div><input type="checkbox" id="fractionsandratioscheck1" class="checkappear"/>REPLACING TEXT.</div>");

</script>

If someone could explain how to fix this, I would appreciate it. Thanks!

J Doe Math
  • 31
  • 5

2 Answers2

0

your problem is that you're using html " inside your javascript "

you must use alternating quotes inside this string

$( ".replaced" ).replaceWith("<div><input type="checkbox" id="fractionsandratioscheck1" class="checkappear"/>REPLACING TEXT.</div>");

it should look something like this

$( ".replaced" ).replaceWith('<div><input type="checkbox" id="fractionsandratioscheck1" class="checkappear"/>REPLACING TEXT.</div>');
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
0

$( ".replaced" ).replaceWith("<div><input type='checkbox' id='fractionsandratioscheck1' class='checkappear'/>REPLACING TEXT.</div>");//escape string properly
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Hello</p
<p class="replaced">Hello</p>
<p class="replaced">World</p>

Only problem with OP is the "" change them to ''

guradio
  • 15,524
  • 4
  • 36
  • 57