skip to Main Content

I am trying to declare <div class="div-name">1</div> as a string. I am having difficulty due to the double quotes.

I am tried wrapping the string around single quotes ('<div className="div-name">1</div>'), but instead the result is "<div className="right-align">100</div>"

Is there another solution to this?

3

Answers


  1. Please update with more data

    In any case you can always try opening the browser console and creating a variable and the console log you can see the result

    For example

    const htmlString = '<div class="div-name">1</div>';
    console.log(htmlString);
    

    or

    const htmlString = '<div class="div-name">1</div>';
    console.log(htmlString);
    
    Login or Signup to reply.
  2. <h3 name ="demo" id = "demo"/>
    
    <script>
    var div_name = "ABCD";
    var t = '<div className="' + div_name + '">22221</div>'
    
    document.getElementById("demo").innerHTML = t;
    </script>
    
    Login or Signup to reply.
  3. Template Literals

    Use Template Literals:
    Wrap the string into backticks as

    int htmlContent = `<div class="div-name">1</div>`;
    

    By using template literals you can also use variables in it as follow:

    int age = 20;
    // without template literals
    int str = "My age is " + age + ".";
    // with template literals
    int str2 = `My age is ${age}.`;
    

    Learn More About Template Literals from here : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search