Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

generate HTML by javascript

// Text nodes can be made using the "write" method
document.write('foo');

// Elements can be made too. First, they have to be created in the DOM
const myElem = document.createElement('span');

// Attributes like classes and the id can be set as well
myElem.classList.add('foo');
myElem.id = 'bar';

// For here, the attribute will look like this: <span data-attr="baz"></span>
myElem.setAttribute('data-atrr', 'baz');

// Finally append it as a child element to the <body> in the HTML
document.body.appendChild(myElem);

// Elements can be imperitavely grabbed with querySelector for one element, or querySelectorAll for multiple elements that can be loopped with forEach
document.querySelector('.class');
document.querySelector('#id');
document.querySelector('[data-other]');
document.querySelectorAll('.multiple');
Source by en.wikipedia.org #
 
PREVIOUS NEXT
Tagged: #generate #HTML #javascript
ADD COMMENT
Topic
Name
2+7 =