4

Add a style attribute to existing style properties

I have a div element:

<div id="permissions" style="clear: both; float: left;">

I want to set border on onsubmit event:

<div id="permissions" style="clear: both; float: left; border: 1px #F00 solid; ">

Is there a way to do this: Append new style attribute (border) to existing styles (with out reading existing styles)?

Musa
  • 96,336
  • 17
  • 118
  • 137
Sharath
  • 320
  • 1
  • 2
  • 11
  • 1
    Tried anything yet? Seems very basic. http://api.jquery.com/css/ – j08691 Apr 28 '14 at 02:36
  • I am a jquery newbie. I have tried $('#persmissions').attr('style', '...') but dint work. I thought $('#permissions').css() will do the same. I Google'd but dint find examples for my case and not here either so I've asked this question. – Sharath Apr 28 '14 at 04:18

5 Answers5

7

As an alternative to manipulating the style directly you could have a css class:

.submitted
{
    border: 1px #F00 solid;
}

Then using jQuery:

$('#permissions').addClass('submitted');

Then to remove:

$('#permissions').removeClass('submitted');

Then if you want to reuse the same approach elsewhere, you can just use the submitted class. Also if you want to extend the class (e.g. change the color) you can just edit the submitted class.

acarlon
  • 16,764
  • 7
  • 75
  • 94
  • This is not what the OP asked for and should not be the accepted solution, see the answer by "lolcaks" below. – DanielT Jan 25 '23 at 14:23
4

You can use the css() method of jquery:

$('#permissions').css('border','1px #000 dashed');
lolcaks
  • 126
  • 3
  • It would be great if you could show us how to remove the added style, like acarlon did in its solution! – aldemarcalazans May 29 '17 at 19:11
  • This should be the accepted answer to "jquery: add a style attribute to existing style" because that is exactly what this is doing, not adding classes. I needed the exact style added to my style attribute, but using $('#permissions').attr('style',"border: 1px #000 dashed"); replaced my existing styles, where $('#').css(' – DanielT Jan 25 '23 at 14:16
  • @aldemarcalazans, the OP did not ask for that, but you can get that answer here: https://stackoverflow.com/questions/4036857/how-can-i-remove-a-style-added-with-css-function – DanielT Jan 25 '23 at 14:21
2

You can add style to existing style or override existing style like this.

var style =$('#permissions').attr('style'); //it will return string
//on submit update style as
style += ';border: 1px #F00 solid;';
$('#permissions').attr('style',style);

It will not affect your existing style and will add your new style to existing one even if you'll have to override any other property as well.

Rajender Verma
  • 389
  • 3
  • 9
1

Sure:

$('#permissions').css({
  borderWidth: 1,
  borderColor: '#fff',
  borderStyle: 'solid'
});

JS Fiddle.

James Hafner
  • 478
  • 2
  • 6
0

You can do this:

$('form').submit(function(){
  $('#permissions').css("border","1px #F00 solid");
});
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
  • 1
    It would be great if you could show us how to remove the added style, like acarlon did in its solution! – aldemarcalazans May 29 '17 at 19:12
  • @aldemarcalazans if you want to remove a style, then you can reset it using `.css()` again, with a new appropriate value. `unset`, `none`, `auto` or 0 – Pars Sep 24 '21 at 20:50