1

How to write javascript to style an element in single line? For example,

document.getElementById('add_new').style.top=-15;
document.getElementById('add_new').style.position='absolute';

It is possible to make it array or single line to style the element using javascript itself?

How to provide a shorthand for writing recurring accesses to objects?

Justin John
  • 9,223
  • 14
  • 70
  • 129
  • I don't understand what you are saying. Are you asking about chaining the style commands? – Ariel Sep 13 '12 at 04:46
  • You can write in Jquery as `$('.add_new').css({left: 475, top: 215});`. But for javascript, how to do so? – Justin John Sep 13 '12 at 04:51
  • OP, I think you mean single expression. You can have as many expressions as you want on a single line. – Matthew Blancarte Sep 13 '12 at 04:53
  • 1
    jQuery _is_ JavaScript Justin. Its `.css()` method is simply some JavaScript code that takes that input and applies it appropriately, so you can write your own similar method from scratch - you can even look at the jQuery source to see how it works... – nnnnnn Sep 13 '12 at 05:04
  • @nnnnnn: Thank you for your information. I want to know whether this is supported with built-in javscript without adding additional function for that. – Justin John Sep 13 '12 at 05:40

5 Answers5

4

I don't recommend it, but you could do something like so to get it in "1" line:

with( document.getElementById('add_new').style ) { top=-15; position='absolute'; }

or another way:

element.style.cssText="background: black ; color: blue ; border: 1px solid green" 

thanks to Are there legitimate uses for JavaScript's "with" statement?

Community
  • 1
  • 1
Matthew Lock
  • 13,144
  • 12
  • 92
  • 130
3

You can't do this with vanilla JavaScript.

If you were using jQuery, you could use an object:

$('#add_new').css({top: '-15px', position: 'absolute'});
Blender
  • 289,723
  • 53
  • 439
  • 496
1

Native js don't have something like this but if you using jQuery you can use following code

$('add_new').css({backgroundColor: '#ffe', borderLeft: '5px solid #ccc'})
MichaelT
  • 7,574
  • 8
  • 34
  • 47
0

This is the somplest way:

Javascript

document.getElementById("add_new").classList.add('addstyle');

CSS

<style>
.addstyle
{
    top: -15;
    position: absolute;
}
</style>

OR with the JQuery as:

$('.add_new').css({top: '-15px', position: 'absolute'});
Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
  • 1
    Note that `.classList` [doesn't seem to be supported](http://caniuse.com/#search=classlist) in IE 9 and older... – nnnnnn Sep 13 '12 at 05:09
0

If you are willing to use jquery you can do it, but that's not completely within scope of the question. You will also have to notate it in the "jQuery" way. Here's a link for reference.

http://api.jquery.com/css/

It might look like:

$("#add_new").css({
    "top": "-15px",
    "position": "absolute"
});

Again, this is not in pure JavaScript as your question indicates. You will need an additional library to make it work.

Joe Mills
  • 1,619
  • 11
  • 12