-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;
}
1

1 Answer 1

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..

Sign up to request clarification or add additional context in comments.

1 Comment

i hope it helped :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.