-2
var mystring1= "'Create new folder' not working (Registry Fix)";
var mystring2 = ""Create new folder" not working (Registry Fix)";
var str = "<div itemname="+mystring1+" title ="+mystring2+">";

But when i'm inspecting the element after this html is rendered i see

"div itemname "Create new folder" not working (Registry Fix) title "Create new folder" not working (Registry Fix)>

But i want in this format

<div itemname='Create new folder' not working (Registry Fix) title="Create new folder" not working (Registry Fix)>

I want a general escape sequence method to use for both single quotes and double quotes.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Sanjay
  • 21
  • 5
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Escape_notation and https://stackoverflow.com/questions/7753448/how-do-i-escape-quotes-in-html-attribute-values – epascarello Oct 07 '20 at 21:38
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals – Taplar Oct 07 '20 at 21:39
  • You should be getting a syntax error on the `var mystring2` line. – Barmar Oct 07 '20 at 21:40
  • [How do I format my posts using Markdown or HTML?](https://stackoverflow.com/help/formatting) – Andreas Oct 07 '20 at 21:42
  • What you want doesn't seem to be right, either. Everything after `Create new folder` isn't part of the attributes. – Barmar Oct 07 '20 at 21:42
  • 'Create new folder' not working (Registry Fix) This is the entire string i want – Sanjay Oct 07 '20 at 21:43
  • @Sanjay use template literals – Tibebes. M Oct 07 '20 at 21:45
  • @Sanjay Then it should be `itemname="'Create new folder' not working (Registry Fix)"` – Barmar Oct 07 '20 at 21:47
  • And `title='"Create new folder" not working (Registry Fix)'` – Barmar Oct 07 '20 at 21:47
  • The code you posted can't be what you're actually running, since it has a syntax error. And if you fix the syntax error, it won't produce the actual results that you show. – Barmar Oct 07 '20 at 22:17
  • @Tibebes.M That doesn't solve the problem of the quoting in the attributes, just quoting of the string literals. – Barmar Oct 07 '20 at 22:17

2 Answers2

1

The best solution is to not use string operations to create DOM elements, use DOM methods.

var mystring1 = "'Create new folder' not working (Registry Fix)";
var mystring2 = '"Create new folder" not working (Registry Fix)';
var div = document.createElement("div");
div.setAttribute("itemname", mystring1);
div.setAttribute("title", mystring2);
div.innerText = "hover or click on me";
div.addEventListener("click", function() {
  console.log(this.getAttribute("itemname"));
}); document.getElementById("container").appendChild(div);
<div id="container">
</div>
Barmar
  • 741,623
  • 53
  • 500
  • 612
-1

Use escape character \ where you need to escape string literals.

var mystring1 = "'Create new folder' not working (Registry Fix)";

var mystring2 = "\"Create new folder\" not working (Registry Fix)";

var str = "<div itemname = \"" + mystring1 + "\" title = '" + mystring2 + "'>Something</div>";