-1

I want to add svg image via DOM manipulation, instead of adding them in HTML DIV?

I have tried everything by googling but couldnot find any better solution for it. Instead of 'X' i want to insert the svg image (assets/close.svg) inside the span element. which is inside the 'studentDetails' Div. Will be grateful for your help.

HTML:

<div id="studentDetails" class="hidden"></div>

javascript:

var studentDetails = document.getElementById('studentDetails');
var favouriteStudent = 
    contacts[parseInt(e.currentTarget.id)].favourite ? 'favourite' : '';
studentDetails.innerHTML =
    "<span id='close-details'>x</span>" +
    "<span id='favourite' class='" +
    favouriteStudent +
    "'>*</span>" +
    '<div>' +
    contacts[parseInt(e.currentTarget.id)].name +
    '</div>' +
    '<div>' +
    contacts[parseInt(e.currentTarget.id)].email +
    '</div>';
studentDetails.setAttribute('class', 'hidden');

CSS:

#close-details {
    display: block;
    float: right;
    font-size: 20px;
    margin-top: -8px;
    background-image: url('assets/close.svg');
    background-repeat: no-repeat;
    background-position: right;
}
PranshuKhandal
  • 739
  • 7
  • 19
Ayesha
  • 211
  • 1
  • 2
  • 13
  • Have you tried the answers in this question: https://stackoverflow.com/questions/14068031/embedding-external-svg-in-html-for-javascript-manipulation – aptriangle Feb 07 '19 at 10:05

1 Answers1

1

this might help, as you can simply add svg using IMG tag:

var studentDetails = document.getElementById('studentDetails');
var favouriteStudent = 
  contacts[parseInt(e.currentTarget.id)].favourite ? 'favourite' : '';
  studentDetails.innerHTML =
  "<span id='close-details'><img src='assets/close.svg'></span>" +
  "<span id='favourite' class='" +
  favouriteStudent +
  "'>*</span>" +
  '<div>' +
  contacts[parseInt(e.currentTarget.id)].name +
  '</div>' +
  '<div>' +
  contacts[parseInt(e.currentTarget.id)].email +
  '</div>';

i have made an edit in line 5 as you see i replaced X with the image tag..

PranshuKhandal
  • 739
  • 7
  • 19