Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add property to existing class

Tags:

jquery

css

I have got a CSS class like so:

.simpleClass {
    width: 25px;
}

And I have a matching element:

<div class="simpleClass"></div>

Can I add the property display: none; to the CSS class .simpleClass dynamically through jQuery? Thanks.

like image 846
Alex Avatar asked Jul 16 '11 15:07

Alex


People also ask

How to add extra property in object in c#?

We will add the Language property. // Add properties dynamically to expando AddProperty(expando, "Language", "English"); The AddProperty method takes advantage of the support that ExpandoObject has for IDictionary<string, object> and allows us to add properties using values we determine at runtime.

How do you extend a class in C#?

You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself.

What are properties in class give example?

Properties do not name the storage locations. Instead, they have accessors that read, write, or compute their values. For example, let us have a class named Student, with private fields for age, name, and code.

How can I extend a class without changing its properties?

The alternative is using standard inheritance or composition to extend the class, which you already mentioned. Use a separate hash table to add properties to objects you don't control, and use your own access functions to wrap whatever behavior you want to appear to be uniform with respect to your added properties.

How to add properties to a class in Java?

We can add properties to our class using the following syntax: In our example above, we've defined two propertyproperties of type string , one named FirstName , the other named LastName .

How to add properties to an object without its control?

Show activity on this post. Use a separate hash table to add properties to objects you don't control, and use your own access functions to wrap whatever behavior you want to appear to be uniform with respect to your added properties.

How do I add state to a class in Java?

Properties allow us to add state to our class. State can be anything which needs to be stored by an instance of your class. We can add properties to our class using the following syntax: In our example above, we've defined two propertyproperties of type string , one named FirstName , the other named LastName .


2 Answers

you can specify the style of the element by using .css like

$("div.simpleClass").css("width","25px");

have a look at jQuery.css()

like image 50
Rafay Avatar answered Sep 20 '22 15:09

Rafay


If I get it right, You don't want to give property to the instance element of simpleClass, but would like to give an extra property to the whole class ( covering all instances ) using JS.

In this case, You can use jQuery to append a style element to the head, giving You the opportunity to add additional CSS declarations.

However, I would not recommend this, because it causes Your code, and structure to get messy.

$("head").append("<style> .simpleClass{ display:none; } </style>");

This snippet will give extra display:none property to all .simpleClass elements existing in the time of doing this, and will also affect every later-inserted .simpleClass elements.

like image 27
István Pálinkás Avatar answered Sep 17 '22 15:09

István Pálinkás