473,395 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Function that accepts a style object.

This relates to the setAttribute problems in Internet Explorer.

I have a function that accepts as one of its arguments an object that
stores style information. The style object specifically adheres to CSS
styles (not the camelCase JavaScript CSS).

This works wonderfully in all browsers (except IE), because I can simply
concatenate the style declarations then set them in one go via:

new_elem.setAttribute('style', style_obj);

Now, I realize the optimal thing (in IE) to do would be to set the
styles directly (or even create another function that dynamically
creates CSS classes and assigns them). However, the only thing I can
think of to achieve that would be to create a lookup function, so that
font-size becomes fontSize, et cetera.

Before I do that however, am I missing something? Could it be done in
an easier fashion? Can I allow the user to pass a style object like: {
color: '#f00', 'font-size': '2em' } and somehow set the styles directly
in IE without a lookup function?

(Sorry if this is isn't that clear. Been up all night with sick kids
and this is the best I can muster.)

Basically, am I on the right track with a lookup function or am I
missing something obvious?

Thanks guys (and gals?).

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 2 '07 #1
10 1420
Hi Lost,

So you want to convert my-hyphenated-string to myHyphenatedString?

I would probably do something like this...

function camelizeStyle(input) {
if (input == 'class') {
return 'className';
}
var c, output='';
for (var i=0; i<input.length; i++) {
output += ((c=input.charAt(i)) == '-') ?
input.charAt(++i).toUpperCase() :
c;
}
return output;
}

var camel = camelizeStyle('my-hypenated-string');

Then I would test this against a version that uses split('-') and a
loop and join, for performance if I thought that mattered.

When I am writing JavaScript I always use camelCase for styles. I
never use hyphen-case. Then I don't have to think in two different
versions in JavaScript.

Peter

Jun 5 '07 #2
Peter Michaux wrote:
Hi Lost,
Hello, Mr. Michaux. :)
So you want to convert my-hyphenated-string to myHyphenatedString?
Um, yeah. I believe it's that simple.
I would probably do something like this...

function camelizeStyle(input) {
if (input == 'class') {
return 'className';
}
var c, output='';
for (var i=0; i<input.length; i++) {
output += ((c=input.charAt(i)) == '-') ?
input.charAt(++i).toUpperCase() :
c;
}
return output;
}

var camel = camelizeStyle('my-hypenated-string');
Beautiful. Definitely better than a lookup function.
Then I would test this against a version that uses split('-') and a
loop and join, for performance if I thought that mattered.
I wrote a very dirty version of just that, and then it dawned on me,
"Who cares?" It's not like I will be converting 10,000 style objects at
a time.

In a preliminary test though, split'ing, then using Array functions was
a bit faster.
When I am writing JavaScript I always use camelCase for styles. I
never use hyphen-case. Then I don't have to think in two different
versions in JavaScript.
You know, that was what I was going for at first. Then something else
dawned on me, "How can I expect CSS authors to be familiar with
JavaScript CSS conventions?"

Thanks!

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 6 '07 #3
You know, that was what I was going for at first. Then something else
dawned on me, "How can I expect CSS authors to be familiar with
JavaScript CSS conventions?"
How is it that the things a CSS designer type's ends up being
processed in your JavaScript? That seems like a red flag to me. Of
course there might be a good reason. I'm curious.

Peter

Jun 6 '07 #4
Peter Michaux wrote:
>You know, that was what I was going for at first. Then something else
dawned on me, "How can I expect CSS authors to be familiar with
JavaScript CSS conventions?"

How is it that the things a CSS designer type's ends up being
processed in your JavaScript? That seems like a red flag to me. Of
course there might be a good reason. I'm curious.
Well, I figured if any CSS designer was attempting to muddle through
*my* code, then I could at least make it as convenient as possible.

Thereby not forcing them to type something they are not used to.

text-decoration: line-through as opposed to textDecorationLineThrough
for example. In those few rare cases I would rather the CSS author rely
on his or her CSS knowledge to pass style information to Objects that
read and display from a template.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 6 '07 #5
On Jun 5, 9:56 pm, -Lost <maventheextrawo...@techie.comwrote:
Peter Michaux wrote:
You know, that was what I was going for at first. Then something else
dawned on me, "How can I expect CSS authors to be familiar with
JavaScript CSS conventions?"
How is it that the things a CSS designer type's ends up being
processed in your JavaScript? That seems like a red flag to me. Of
course there might be a good reason. I'm curious.

Well, I figured if any CSS designer was attempting to muddle through
*my* code, then I could at least make it as convenient as possible.

Thereby not forcing them to type something they are not used to.

text-decoration: line-through as opposed to textDecorationLineThrough
for example. In those few rare cases I would rather the CSS author rely
on his or her CSS knowledge to pass style information to Objects that
read and display from a template.
This sounds like a bad situation if CSS designers are looking at
JavaScript code.

The JavaScript should be adding, removing or changing CSS class names
in the class attribute of an element. Then the CSS designer can just
stay with the stylesheets and the rules associated with the classes.
The only exception to this rule is when updating styles repeatedly in
an animations (which includes drag and drop.) In this case you would
need hundreds of CSS classes which is just not a good idea.

Peter

Jun 6 '07 #6
On Jun 5, 4:09 pm, Peter Michaux <petermich...@gmail.comwrote:
So you want to convert my-hyphenated-string to myHyphenatedString?

I would probably do something like this...

function camelizeStyle(input) {
...
}
or:

function camelizeStyle(input){
return input.replace(/-(.)/g,function(x,y){return y.toUpperCase()})
}

Jun 6 '07 #7
On Jun 6, 1:40 pm, "scripts.contact" <scripts.cont...@gmail.com>
wrote:
On Jun 5, 4:09 pm, Peter Michaux <petermich...@gmail.comwrote:
So you want to convert my-hyphenated-string to myHyphenatedString?
I would probably do something like this...
function camelizeStyle(input) {
...
}

or:

function camelizeStyle(input){
return input.replace(/-(.)/g,function(x,y){return y.toUpperCase()})

}
Respect!

Peter

Jun 6 '07 #8
Peter Michaux wrote:
On Jun 6, 1:40 pm, "scripts.contact" <scripts.cont...@gmail.com>
wrote:
>On Jun 5, 4:09 pm, Peter Michaux <petermich...@gmail.comwrote:
>>So you want to convert my-hyphenated-string to myHyphenatedString?
I would probably do something like this...
function camelizeStyle(input) {
...
}
or:

function camelizeStyle(input){
return input.replace(/-(.)/g,function(x,y){return y.toUpperCase()})

}

Respect!
Ditto.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 7 '07 #9
Peter Michaux wrote:
So you want to convert my-hyphenated-string to myHyphenatedString?

I would probably do something like this...

function camelizeStyle(input) {
if (input == 'class') {
return 'className';
}
var c, output='';
for (var i=0; i<input.length; i++) {
output += ((c=input.charAt(i)) == '-') ?
input.charAt(++i).toUpperCase() :
c;
}
return output;
}
Or:

function camelizeStyle(input){
return input.replace(/-(.)/g,function(x,y){return y.toUpperCase()})
}

Jun 8 '07 #10
Peter Michaux wrote:
So you want to convert my-hyphenated-string to myHyphenatedString?

I would probably do something like this...

function camelizeStyle(input) {
if (input == 'class') {
return 'className';
}
var c, output='';
for (var i=0; i<input.length; i++) {
output += ((c=input.charAt(i)) == '-') ?
input.charAt(++i).toUpperCase() :
c;
}
return output;
}
Or:

function camelizeStyle(input){
return input.replace(/-(.)/g,function(x,y){return y.toUpperCase()})
}

Jun 8 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Fresh Air Rider | last post by:
Hello Could anyone please explain how I can pass more than one arguement/parameter value to a function using <asp:linkbutton> or is this a major shortfall of the language ? Consider the...
1
by: zx.zhangxiong | last post by:
Dear all, I'm puzzled about the usage of class member function. Any help would be appreciated. class Account { ...
7
by: Alfonso Morra | last post by:
How can this work ? I have the following declarations in a header: Header ========= typedef void (*VFPTR)(void) ; void FOO_Callback(void*, char*, char*, int, unsigned long, void*,void*);...
10
by: Robert Skidmore | last post by:
Take a look at this new JS function I made. It is really simple but very powerful. You can animate any stylesheet numeric value (top left width height have been tested), and works for both % and px...
17
by: Jef Driesen | last post by:
Suppose I have a datastructure (actually it's a graph) with one template parameter (the property P for each edge and vertex): struct graph<P>; struct vertex<P>; struct edge<P>; I also have...
10
by: Janus | last post by:
Hi, Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this: var...
6
by: mdh | last post by:
I guess pointers just take time!!! This is exercise 5-13, peripherally. The function accepts a command line argument of -n. So, given int main ( int argc, char *argv)
2
by: esource | last post by:
Hi all, I'm trying to use threading model in my web service but addressof does not work with functions I'm using VS 2005 my code: Public Function Start() as Boolean Dim MyThread As New...
16
by: Peng Yu | last post by:
Hi, I'm wondering if there is a min function (in boost, maybe?) that accepts any number of arguments? std::min only accepts two arguments. If I want to get the minimum number out of many, I have...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.