Search code examples
javascripthtmlescapinginnerhtmlquotes

How can I properly escape a large block of HTML when using onClick and innerHTML?


Here's my HTML:

<div id="#my_link"></div>

<a href="#" onclick="document.getElementById('#my_link').innerHTML = ''"></a>

Here's the code that I want to insert into #my_link:

 <div style='position:fixed;left:0;top:0;z-index:100000;width:100%;height:100%;background-color:rgba(255, 255, 255, 0.8); display:flex;justify-content:center;align-content:vertical;align-items:center'><form action='http://example.com' style='display: block; padding: 50px; background-color: #ffffff; box-shadow: 0px 8px 18px 2px #cccccc;'><p style='font-family: SharpGrotesk, Open Sans, Calibri; font-size: 25px; margin-bottom: 5px;'><b>Sign In</b></p><p style='font-size: 10pt;font-family: Atlas Grotesk Web, Open Sans, Calibri, sans-serif; margin-bottom: 10px;'>It's been a while since you signed in.<br>For security reasons, please re-enter your credentials.</p><input style='display:block; padding: 8px 6px 3px 18px; height: 44px; font-size:14px; width: 360px; margin-bottom: 15px;' placeholder='Email'></input><input type='password' style='display:block; padding: 8px 6px 3px 18px; height: 44px; font-size:14px; width: 360px; margin-bottom: 15px;' placeholder='Password'></input><button type='submit' style='display:block;width: 360px; height: 44px; background: #0061ff; border:none; font-size: 16px; color: #ffffff; font-family: Atlas Grotesk Web, Open Sans, Calibri, sans-serif;'>Sign in</button></form></div>

It has a mixture of single quotes, double quotes, and carats. Whenever I try to put it into innerHTML, I get an Uncaught SyntaxError: Unexpected identifier error. I've tried escaping it a bunch of different ways but I always get that error.

I have to use onClick for this, I can't use a separate script tag.

JSFiddle link: https://jsfiddle.net/zwtr96se/


Solution

  • Replace ' with &quot;

    <div id="#my_link"></div>
    
    <a href="#" onclick="document.getElementById('#my_link').innerHTML = '<div style=&quot;position:fixed;left:0;top:0;z-index:100000;width:100%;height:100%;background-color:rgba(255, 255, 255, 0.8); display:flex;justify-content:center;align-content:vertical;align-items:center&quot;><form action=&quot;http://example.com&quot; style=&quot;display: block; padding: 50px; background-color: #ffffff; box-shadow: 0px 8px 18px 2px #cccccc;&quot;><p style=&quot;font-family: SharpGrotesk, Open Sans, Calibri; font-size: 25px; margin-bottom: 5px;&quot;><b>Sign In</b></p><p style=&quot;font-size: 10pt;font-family: Atlas Grotesk Web, Open Sans, Calibri, sans-serif; margin-bottom: 10px;&quot;>It&quot;s been a while since you signed in.<br>For security reasons, please re-enter your credentials.</p><input style=&quot;display:block; padding: 8px 6px 3px 18px; height: 44px; font-size:14px; width: 360px; margin-bottom: 15px;&quot; placeholder=&quot;Email&quot;></input><input type=&quot;password&quot; style=&quot;display:block; padding: 8px 6px 3px 18px; height: 44px; font-size:14px; width: 360px; margin-bottom: 15px;&quot; placeholder=&quot;Password&quot;></input><button type=&quot;submit&quot; style=&quot;display:block;width: 360px; height: 44px; background: #0061ff; border:none; font-size: 16px; color: #ffffff; font-family: Atlas Grotesk Web, Open Sans, Calibri, sans-serif;&quot;>Sign in</button></form></div>'">Click me</a>
    

    Example:https://jsfiddle.net/uLm6bwe2/