Reputation: 21701
Here is my code (jquery)
var my_css_class = "{ position: absolute; top: 1400px;left: 40%;background-color: Menu;border: #f9f9f9;height: 200px;width: 300px; }";
this.popOut = function(msgtxt) {
//filter:alpha(opacity=25);-moz-opacity:.25;opacity:.25;
this.overdiv = document.createElement("div");
this.overdiv.className = "overdiv";
/* I want to add style into class square?*/
this.square = document.createElement("div");
this.square.className = "square";
$('.square').css(my_css_class);/*<----*/
this.square.Code = this;
var msg = document.createElement("div");
msg.className = "msg";
msg.innerHTML = msgtxt;
this.square.appendChild(msg);
var closebtn = document.createElement("button");
closebtn.onclick = function() {
this.parentNode.Code.popIn();
}
closebtn.innerHTML = "Close";
this.square.appendChild(closebtn);
document.body.appendChild(this.overdiv);
document.body.appendChild(this.square);
}
I want to add style into class square?
div.square {
position: absolute;
top: 1400px;
left: 40%;
background-color: Menu;
border: #f9f9f9;
height: 200px;
width: 300px;
}
How can I do this?
Thanks
Upvotes: 1
Views: 103
Reputation:
var my_css_class = { "position": "absolute",
"top": "1400px",
"left": "40%",
};
$('.square').css(my_css_class);
Upvotes: 1
Reputation: 1207
var my_css_class = { "position": "absolute",
"top": "1400px",
"left": "40%",
};
$("#myelement").css(my_css_class);
$('.square').css(my_css_class);
Upvotes: 2
Reputation:
you can use this format for single css properties...
$('div.square').css('propertie','value'); // this is for only CSS propertie..
if you want to add multiple CSS properties at once. you have to pass an object as a parameter in css({}) like this
$('div.square').css({
position: absolute;
top: 1400px;
left: 40%;
background-color: Menu;
border: #f9f9f9;
height: 200px;
width: 300px;
});
Upvotes: 1
Reputation: 11750
$('.square').css({'width':'300px','background-color': Menu, ... });
Upvotes: 0
Reputation: 1456
You can use the following format to add styles to class using jQuery:
$('selector').css(propName, propValue)
Example:
$('div.square').css('position','absolute');
Repeat this for all the properties. It is also possible to add multiple props at once. Refer the following document for more.
http://api.jquery.com/css/#css-propertyName-function
Upvotes: 0