How to insert HTML content in the div with jQuery without using " (reverse bar and double quotes)

Asked

Viewed 3,861 times

2

I use a CMS called uCoz and it has a variable <?$RELATED_ENTRIES$(5)?> which displays posts related to the page my visitor reading. I would like to put the related lines between the text of the post, but it does not work by CMS system rule, which does not have the root of the scripts open.

The solution I found was to create a <div class="related"></div> and insert the content that the variable <?$RELATED_ENTRIES$(5)?> generate using jQuery. But in Javascript double quotes have to put \ (reverse bar) before and the generated code does not have this \ (reverse bar)

The code that the variable generates is like this

<ul class="uRelatedEntries">
<li class="uRelatedEntry"><a href="/link#1">Lorem Ipsum</a></li>
<li class="uRelatedEntry"><a href="/link#2">Lorem Ipsum</a></li>
<li class="uRelatedEntry"><a href="/link#3">Lorem Ipsum</a></li>
</ul>

Only HTML is compact in one line.

  • If I understand well, you get the HTML you need only it appears on the same line, without being organized in lists?

  • Can show the code you are currently using?

  • 1

    It’s kind of hard to understand, it would really be nice if you put the code you’re using to generate this and how you wanted it to be.

  • and what’s your problem?

  • As I understand it, the uCoz replaces the <?$RELATED_ENTRIES$(5)?> automatically by the HTML that is in the question. He wants to use this HTML string that uCoz provides using Javascript/jQuery.

  • By mentioning the issue of Escaping (need of reverse bar before quotation marks), reveals that this is the problem: how to do the Escaping of a variable that it does not have access to modify (but only to use in your template)?

  • @J.Bruni, I think that when the user explains better we will all have more certainty. For now it is better not to comment too much, or guess. If the matter is not clarified in 24 hours, we vote to close.

  • @J.Bruni, great. You have already answered. When the question is clarified I vote +1 if you are right. Until then I’m not sure what’s being asked for so I wait before answering.

  • @J.Bruni: we can chat? Who voted negative did not explain, hence my comment trying to explain why it might have been...

  • I had the same interpretation of @J.Bruni, even agreeing that the question of space for doubts, but I think that this battle for perfectionism is a bit frivolous. It is right for a user not to be able to formulate his question so well and an answer to be about understanding, I see no reason not to punctuate it.

Show 5 more comments

1 answer

4


In this case, enclose the variable in single quotes:

$('div.related').html('<?$RELATED_ENTRIES$(5)?>');

I’d just have to put \ (reverse slash) before double quotes if the variable was in double quotes: "<?$RELATED_ENTRIES$(5)?>" (this way it doesn’t work because it would need the reverse bar).

However, even involving the variable with simple quotation marks as suggested, if single quotation marks appear in the contents of the variable, then you would need the reverse bar before them.

In short:

  • If the variable content has double quotes, enclose it with single quotes.
  • If the variable content has single quotes, enclose it with double quotes.
  • If the variable content has both single and double quotes, you will not be able to use the text without changing it (which is not possible by the uCoz system).
  • Thank you. Solved my problem. Thank you for helping me and explain. I’m still starting with web programming and I heartily thank you for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.