Create a function called newElement that will simplify the creation of a new HTM
ID: 3725080 • Letter: C
Question
Create a function called newElement that will simplify the creation of a new HTML element. This function should accept four parameters, in the following order: a string that represents the type of element to create, the string containing the id of the parent element the new element should be appended to, a string that contains the id for the new element, and an object that contains any style properties that should be applied to the element. Note that the style object may contain any number of properties, but each one will be a defined As an example, the function call newElement( 'div', 'oldContainer', 'newID', {backgroundColor: 'green', width: '50px'} ) should have the same result as the code below: let x = document.createElement( 'div' ); x.style.backgroundColor = 'green'; x.style.width = '50px'; x.id = 'newID'; document.getElementById( 'oldContainer' ).appendChild( x );
Explanation / Answer
function newElement(tag, parentId, id, style) { var x = document.createElement(tag); x.id = id; x.style = style; document.getElementById(parentId).appendChild(x); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.