I cannot manage to get the following code to work. I am trying to dynamically generate the following SVG in JS:
<svg xmlns="http://www.w3.org/2000/svg" height="48" width="48"><path d="m12.45 37.65-2.1-2.1L21.9 24 10.35 12.45l2.1-2.1L24 21.9l11.55-11.55 2.1 2.1L26.1 24l11.55 11.55-2.1 2.1L24 26.1Z"/></svg>
I have tried:
let closeButton = document.createElement("svg");
closeButton.setAttribute("height", "48");
closeButton.setAttribute("width", "48");
let closeButtonPath = document.createElementNS('http://www.w3.org/2000/svg',"path");
closeButtonPath.setAttributeNS(null, "d", "m12.45 37.65-2.1-2.1L21.9 24 10.35 12.45l2.1-2.1L24 21.9l11.55-11.55 2.1 2.1L26.1 24l11.55 11.55-2.1 2.1L24 26.1Z");
closeButton.appendChild(closeButtonPath);
this.#form.appendChild(closeButton);
CodePudding user response:
As commented by @enxaneta: your code has errors:
For creating svg
elements you need
createElementNS()
instead of createElement()
Besides, this.#form
is not a valid selector
Rather use something like this:
let form = document.getElementById('form');
form.appendChild(closeButton);
const ns ="http://www.w3.org/2000/svg";
let closeIconSvg = document.createElementNS(ns, "svg");
closeIconSvg.setAttribute("viewBox", "0 0 48 48");
closeIconSvg.classList.add('closeIconSvg');
let closeIconPath = document.createElementNS(ns,"path");
closeIconPath.setAttribute("d", "m12.45 37.65 -2.1-2.1L21.9 24 10.35 12.45l2.1-2.1L24 21.9l11.55-11.55 2.1 2.1L26.1 24l11.55 11.55-2.1 2.1L24 26.1Z");
closeIconSvg.appendChild(closeIconPath);
//let form = document.getElementById('form');
let closeButton = document.getElementById('btnClose');
closeButton.appendChild(closeIconSvg);
*{
box-sizing:border-box
}
.form{
font-size:48px;
display:flex;
align-items:center;
}
input,
.btnClose{
margin:0;
padding:0.15em 0.3em 0.3em 0.3em;
font-size:1em;
border:1px solid #ccc;
background:#fff;
}
.closeIconSvg{
width:auto;
height:1em;
vertical-align: -0.25em;
}
<form id="form" action="">
<input type="text" placeholder="name">
<button id="btnClose" type="button"></button>
</form>
I highly recommend to use more semantic variable names:
Since your icon's parent svg is not the actual button – you better use a self explanatory name like "closeIconSvg".
CodePudding user response:
Why not doing it "the lazy way" in 1 line (except if you have variables for width, height and path)?:)
let svg_str = '<svg xmlns="http://www.w3.org/2000/svg" width="48px" height="48px"><path d="m12.45 37.65-2.1-2.1L21.9 24 10.35 12.45l2.1-2.1L24 21.9l11.55-11.55 2.1 2.1L26.1 24l11.55 11.55-2.1 2.1L24 26.1Z"/></svg>';
document.querySelector('#button1').innerHTML = svg_str;
// if height and widht has to be changed otherwise you put them directly in svg_str
const h = 48 'px';
const w = 48 'px';
// if path is a variable otherwise you put directly in svg_str
const path_d = "m12.45 37.65-2.1-2.1L21.9 24 10.35 12.45l2.1-2.1L24 21.9l11.55-11.55 2.1 2.1L26.1 24l11.55 11.55-2.1 2.1L24 26.1Z";
svg_str = '<svg xmlns="http://www.w3.org/2000/svg" width="' w '" height="' h '"><path d="' path_d '"/></svg>';
document.querySelector('#button2').innerHTML = svg_str;
<div id="button1">
</div>
<div id="button2">
</div>
CodePudding user response:
“Art is in the eye of the beholder, and everyone will have their own interpretation.”
― E.A. Bucchianeri
<style>
svg-icon { background:pink }
</style>
<svg-icon></svg-icon>
<svg-icon width="80"></svg-icon>
<svg-icon width="180"></svg-icon>
<script>
customElements.define("svg-icon", class extends HTMLElement{
connectedCallback(){
this.style.display = "inline-block";
let width = (this.getAttribute("width") || 48) "px";
this.innerHTML =`<svg width="${width}" height="${width}" viewBox="0 0 48 44">`
`<path d="m12.4 37.6-2.1-2.1 11.6-11.5-11.6-11.6 2.1-2.1 11.6 11.6 11.6-11.6 2.1 2.1-11.6 11.6 11.6 11.6-2.1 2.1-11.6-11.6z"/>`
`</svg>`;
}
});
</script>