VGTech is a blog where the developers and devops of Norways most visited website share code and tricks of the trade… Read more



the

JS

category

Hover state on touch devices

Frontend

With an increasing number of users on tablets and smartphones, the need to take touch friendliness into consideration is greater than ever. Even though both the browsers and hardware are approaching desktop speed, there are still some issues with implementing web sites and web apps for these devices. (more…)


Strip HTML from a string in Javascript

JS

Quick tip; never use innerHTML (or jQuery’s html()) unless you really want to insert HTML.
Quite often, what you actually want is to insert some text. If this is the case, use innerText (or jQuery’s text()).

Should you need to strip HTML from a string (say you are building a chunk of HTML and need to insert the content of an input-field into it), this is a simple way of doing it:

Show code
function stripHtml(str) {
    var temp = document.createElement('DIV');
    temp.innerHTML = str;
    return temp.textContent || temp.innerText;
}

Or, if you’re using jQuery:

Show code
function stripHtml(str) {
    return jQuery('<div />', { html: str }).text();
}