Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change css of element using jquery

Tags:

html

jquery

css

I have defined a CSS property as

#myEltId span{
  border:1px solid black;
}

On clicking a button, I want to remove its border.

$('#button1').click(function() {
  // How to fetch all those spans and remove their border
});
like image 240
Sachin Avatar asked Apr 26 '12 13:04

Sachin


People also ask

How do you change the style of an element using jQuery?

The css() method in JQuery is used to change the style property of the selected element. The css() in JQuery can be used in different ways. Return value: It will return the value of the property for the selected element.

Can we change CSS using jQuery?

You can change CSS using the jQuery css() method which is used for the purpose of getting or setting style properties of an element. Using this method you can apply multiple styles to an HTML all at once by manipulating CSS style properties.

How can give multiple CSS properties in jQuery?

Apply multiple CSS properties using a single JQuery method CSS( {key1:val1, key2:val2....). You can apply as many properties as you like in a single call. Here you can pass key as property and val as its value as described above.

How can check CSS property value in jQuery?

Get a CSS Property Value You can get the computed value of an element's CSS property by simply passing the property name as a parameter to the css() method. Here's the basic syntax: $(selector). css("propertyName");


2 Answers

Just use:

$('#button1').click(
    function(){
        $('#myEltId span').css('border','0 none transparent');
    });

Or, if you prefer the long-form:

$('#button1').click(
    function(){
        $('#myEltId span').css({
            'border-width' : '0',
            'border-style' : 'none',
            'border-color' : 'transparent'
        });
    });

And, I'd strongly suggest reading the API for css() (see the references, below).

References:

  • css().
like image 68
David Thomas Avatar answered Oct 23 '22 12:10

David Thomas


If you will use this several times, you can also define css class without border:

.no-border {border:none !important;}

and then apply it using jQuery;

$('#button1').click(function(){
        $('#myEltId span').addClass('no-border');
});
like image 20
felixyadomi Avatar answered Oct 23 '22 13:10

felixyadomi