add HTML element inside js variable

321
May 18, 2018, at 02:40 AM
var $document = $(document);
var selector = '[data-rangeslider]';
var $element = $(selector);
// For ie8 support
var textContent = ('textContent' in document) ? 'textContent' : 'innerText';
// Example functionality to demonstrate a value feedback
function valueOutput(element) {
    var value = element.value;
    var output = element.parentNode.getElementsByTagName('output')[0] || element.parentNode.parentNode.getElementsByTagName('output')[0];
    output[textContent] = value + 'mm';
}
$document.on('input', 'input[type="range"], ' + selector, function(e) {
    valueOutput(e.target);
});

in the line output[textContent] = value + 'mm'; I need the output as value + '<span class="classname">mm</span>' I tried most of the things and did a lot of research but no luck so far.

This might be something very simple but I am too new to JavaScript so couldn't figure it out. Please help.

Answer 1

You should change this line:

output[textContent] = value + 'mm';

to:

output.innerHTML = value + '<span>mm</span>';

Also, you could remove the IE8 fallback, as it will not be necessary. All browsers have support for innerHTML.

In your code you are assigning the node text and not the node HTML as you should.

You can read more about the difference between innerText and innerHTML here.

Answer 2

Did you try those solution?

output.innerHTML = `${value} <span class="my-class">mm</span>`;
Answer 3
var textContent = ('textContent' in document) ? 'textContent' : 'innerText';

textContent and innerText are both functions for adding text to an element you want to use innerHTML to add HTML.

Like this:

ouput.innerHTML = ...;
Rent Charter Buses Company
READ ALSO
how to iterate in array and returnn new array having sum of items?

how to iterate in array and returnn new array having sum of items?

I have one question in my exam I want to know how I will implement this in JavaScript

132
Hard code to stop the wheel on exact color

Hard code to stop the wheel on exact color

How I can stop a wheel rotating On exact sector like hard codeJust found a wheel on codepend and can't figure out how to stop it example: make spin and it stops on the red sector after make new spin it stops on a blue sector

177
Having certain behavior occur within the ranges between array values?

Having certain behavior occur within the ranges between array values?

I have an array of values, specifically pixel offsets of a certain type of element

163