JavaScript How to add HTML tag into JavaScript? html into javascript variable,insert html into javascript,import html

    
function createCustomElement(anchorText, anchorLink){
  

  var aTag = document.createElement("a");
  aTag.href = anchorLink;
  aTag.innerHTML = anchorText;
  
  return aTag ;
}

var parent = document.getElementById('para');
var customElement = createCustomElement("w3Schools", "www.w3School.com");
parent.appendChild(customElement);
<div id="para">
</div>
Answer:1

We can assign the string template directly by calling innerHTML on a newly created element and returning its childNode. By doing so we can add custom attributes to the new DOM element as well by adding it to the template string.

function strToElem(text, link){
  var temp = '<p> An absolute URL : <a target='_blank' href="'+ link + '">'+text+'</a></p>';
  var a = document.createElement("p");
  a.innerHTML = temp;
  return a.childNodes[0];
}

var parent = document.getElementById('parent');
var elem = strToElem("w3Schools", "www.w3School.com");
parent.appendChild(elem);
<div id="parent">
</div>
Answer:2

You can create html element as objects in JavaScript using document.createElement() method,

To create and store following html string in JavaScript as an object:
<p>An absolute URL: <a target='_blank' href="https://www.w3schools.com">W3Schools</a></p>

You will first need to create parent <p> tag as follows,

var ptag = document.createElement('p');
ptag.innerText = 'An absolute URL:';

Next you will need to create anchor tag object as follows,

var anchorTag = document.createElement('a');
anchroTag.href = 'https://www.w3schools.com';
anchorTag.innerText = 'w3Schools';

Next append anchor tag as child to <p> tag as follows,

ptag.appendChild(anchorTag);

This way now you can refer your object ptag and anchorTag latter.

Answer:3

This is possible and you can store it as String. If you are struggling to make it work because of the quotes confusion, correct would be

var htm = { html : '<p>An absolute URL: <a target='_blank' href="https://www.w3schools.com">W3Schools</a></p>'}

Note the single quotes. Since you already used doubles quotes for href, you string must wrapped with single quotes.

Answer:4

Try using this code. Enclose your html block of code in '' and it should work.

var test = '<p>An absolute URL: <a target='_blank' href="https://www.w3schools.com">W3Schools</a></p>';

Answer:5

I think you have to enclose that html in ' ' (for correct sitaxis) , or, second option is create an Dom object using Javascript.

https://www.w3schools.com/js/js_htmldom_nodes.asp

Then you can edit and modify as a real html not a simple string.

Hope this help you.

Answer:6

I have developed and packaged an offline Cordova-based HTML5 App for Android. It works fine, and some of the resources are accessed locally without an internet connection while other require an ...

I have developed and packaged an offline Cordova-based HTML5 App for Android. It works fine, and some of the resources are accessed locally without an internet connection while other require an ...

I'm trying to prove that a simple function call such as window.alert(); is valid EcmaScript 2016 (7th Edition) grammar. Working backward, with the expectation this is an ExpressionStatement, I see ...

I'm trying to prove that a simple function call such as window.alert(); is valid EcmaScript 2016 (7th Edition) grammar. Working backward, with the expectation this is an ExpressionStatement, I see ...

  1. javascript function call itself
  2. javascript function call another function
  3. javascript function call vs apply
  4. javascript function call in html
  5. javascript function call overhead
  6. javascript function call method
  7. javascript function call not working
  8. javascript function call with parameters
  9. javascript function call performance
  10. javascript function call apply
  11. javascript function call failed null
  12. javascript function call stack
  13. javascript function call on load
  14. javascript function call syntax
  15. javascript function call by reference
  16. javascript function call two parentheses
  17. javascript function call on page load
  18. javascript function call on button click
  19. javascript function call without parentheses
  20. javascript function call inside function

I have a scrollable ul, and several li-items. My goal is to scroll to the li-item with certain id when the user presses a button. I have a code which seems to work, but there are two issues If I ...

I have a scrollable ul, and several li-items. My goal is to scroll to the li-item with certain id when the user presses a button. I have a code which seems to work, but there are two issues If I ...

Background: I'm building a responsive website (html5,css3,js), but noticed some functionality doesn't work when being viewed inside of Facebooks in-app browser. The site works fine in chrome/firefox/...

Background: I'm building a responsive website (html5,css3,js), but noticed some functionality doesn't work when being viewed inside of Facebooks in-app browser. The site works fine in chrome/firefox/...

  1. facebook browser debugging