Sadia Mehjabin
Sadia Mehjabin

Reputation: 129

Adding css by jquery

I know how to add css by jquery .But I am thinking to do it other way.My approch

var alrt = 'width:200px;';

jQuery("#login").css(' + alrt + ');

Can I do this way?I have no output so far

Upvotes: 0

Views: 95

Answers (5)

user7856186
user7856186

Reputation:

you can add css in jquery in the following ways.

if you need to apply only one style in the css, you can add like this.

$("#login").css("width", "200px");

if you have to add multiple styles in the css, you can add like this.

$("#login")).css({"width-color": "200px", "font-size": "25px"});

Upvotes: 0

Andreas Josas
Andreas Josas

Reputation: 242

You can do the following:

var alrt = 'width:200px;';
jQuery("#login").attr("style", alrt);

If you want to copy across existing style:

var alrt = jQuery("#login").attr("style") + 'width:200px;';
jQuery("#login").attr("style", alrt);

Just be careful with that, using .css() is better since if the style is already set on an element then it will overwrite it whereas the above essentially concats strings together.

Upvotes: 0

Palash
Palash

Reputation: 598

You can have something like this is css jQuery("#login").css({'width':'200px'}); You also try the attr

Upvotes: 0

Sudharsan S
Sudharsan S

Reputation: 15403

Use this addClass in jquery. please don't add css in inline style.

<style>
    addingWidth {
        width:200px;
    }
</style>
jQuery("#login").addClass("addingWidth");

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388436

You need to pass the css property and value as separate arguments to .css()

jQuery("#login").css('width', '200px');

Upvotes: 1

Related Questions