1

How can I embed large HTML tags into this scenario?

$(document).ready(function () {
            window.showim = function(src) {
                $("#divcont1").html(src);
            };
        });

Instead of <h1>test</h1> I need to insert large html content

<span class="spcusr" onclick="showim('<h1>test</h1>');">click me</span>

      <div id="divcont1"></div>

My Large HTML Content Sample

<div style="width:976px; height:576px; overflow:hidden; background-repeat:no-repeat; background-image:url(MASTER.jpg)">
<table style="cursor:pointer;">
  <tr height="144">
    <td width="485"  onclick="window.location='#';">&nbsp;</td>
    <td width="485" onclick="window.location='#';">&nbsp;</td>
  </tr>
  <tr height="144">
    <td onclick="window.location='#';">&nbsp;</td>
    <td onclick="window.location='#';">&nbsp;</td>
  </tr>
  <tr height="144">
    <td onclick="window.location='#';">&nbsp;</td>
    <td onclick="window.location='#';">&nbsp;</td>
  </tr>
  <tr height="144">
    <td onclick="window.location='#';">&nbsp;</td>
    <td onclick="window.location='#';">&nbsp;</td>
  </tr>
</table>
</div>

My main problem is at double quotes and quotes. Beacuase onclick="showim('this started with single quotes and no more quotes allowed inside')"; So, what to do in this situation? Thanks

3
  • You can escape the single quotes. Commented Jan 29, 2014 at 13:17
  • Take a look at html string encoding and decoding methods of javascript. Commented Jan 29, 2014 at 13:21
  • 2
    An attribute is not a good place for storing HTML content. That's anti-pattern. You should consider using a templating library. Commented Jan 29, 2014 at 13:22

2 Answers 2

1

You have to Escape Quotes that go deeper than " and '.

This here, may help you: Escape quotes in JavaScript

Sign up to request clarification or add additional context in comments.

Comments

1

Escape the quotes before doing anything in the function.

window.showim = function(src) {
    src = src.replace(/'/g, "\'");
    src = src.replace(/"/g, '\"')
    $("#divcont1").html(src);
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.