Add inline style using Javascript

JavascriptCssInnerhtml

Javascript Problem Overview


I'm attempting to add this code to a dynamically created div element

style = "width:330px;float:left;" 

The code in which creates the dynamic div is

var nFilter = document.createElement('div');
nFilter.className = 'well';
nFilter.innerHTML = '<label>' + sSearchStr + '</label>';

My idea is to add the style after < div class="well" but i don't know how i'd do it.

Javascript Solutions


Solution 1 - Javascript

nFilter.style.width = '330px';
nFilter.style.float = 'left';

This should add an inline style to the element.

Solution 2 - Javascript

You can do it directly on the style:

var nFilter = document.createElement('div');
nFilter.className = 'well';
nFilter.innerHTML = '<label>'+sSearchStr+'</label>';

// Css styling
nFilter.style.width = "330px";
nFilter.style.float = "left";

// or
nFilter.setAttribute("style", "width:330px;float:left;");

Solution 3 - Javascript

Using jQuery :

$(nFilter).attr("style","whatever");

Otherwise :

nFilter.setAttribute("style", "whatever");

should work

Solution 4 - Javascript

You can try with this

nFilter.style.cssText = 'width:330px;float:left;';

That should do it for you.

Solution 5 - Javascript

var div = document.createElement('div');
div.setAttribute('style', 'width:330px; float:left');
div.setAttribute('class', 'well');
var label = document.createElement('label');
label.innerHTML = 'YOUR TEXT HERE';
div.appendChild(label);

Solution 6 - Javascript

A few people have an example using setAttribute which I like. However it assumes you don't have any styles currently set. I would maybe do something like:

nFilter.setAttribute('style', nFilter.getAttribute('style') + ';width:330px;float:left;');

Or make it into a helper function like this:

function setStyle(el, css){
  el.setAttribute('style', el.getAttribute('style') + ';' + css);
}

setStyle(nFilter, 'width:330px;float:left;');

This makes sure that you can add styles to it continuously and it won't remove any style currently set by always appending to the current styles. It also adds an extra semi colon so that if there is a style ever missing one it will append another to make sure it is fully delimited.

Solution 7 - Javascript

you should make a css class .my_style then use .addClass('.mystyle')

Solution 8 - Javascript

If you don't want to add each css property line by line, you can do something like this:

document.body.insertAdjacentHTML('afterbegin','<div id="div"></div>');

/**
 * Add styles to DOM element
 * @element DOM element
 * @styles object with css styles
 */
function addStyles(element,styles){
  for(id in styles){
    element.style[id] = styles[id];
  }
}

// usage
var nFilter = document.getElementById('div');
var styles = {
  color: "red"
  ,width: "100px"
  ,height: "100px"
  ,display: "block"
  ,border: "1px solid blue"
}
addStyles(nFilter,styles);

Solution 9 - Javascript

Use cssText

nFilter.style.cssText +=';width:330px;float:left;';

Solution 10 - Javascript

Try something like this

document.getElementById("vid-holder").style.width=300 + "px";

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCurtis CreweView Question on Stackoverflow
Solution 1 - JavascriptDharmanView Answer on Stackoverflow
Solution 2 - JavascriptMario SView Answer on Stackoverflow
Solution 3 - JavascriptJean-GeorgesView Answer on Stackoverflow
Solution 4 - JavascriptHector RomeroView Answer on Stackoverflow
Solution 5 - JavascriptJames KyburzView Answer on Stackoverflow
Solution 6 - JavascriptstuyamView Answer on Stackoverflow
Solution 7 - Javascriptbennett_anView Answer on Stackoverflow
Solution 8 - JavascriptVincurekfView Answer on Stackoverflow
Solution 9 - Javascriptmarbor3View Answer on Stackoverflow
Solution 10 - JavascriptZubair MushtaqView Answer on Stackoverflow