473,387 Members | 3,033 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,387 software developers and data experts.

Dynamic css Property

Jezternz
145 100+
Okay basicly I wanna do the following:

Expand|Select|Wrap|Line Numbers
  1. var changetype = "backgroundColor";
  2. changevalue = "red";
  3. element.style.changetype = changevalue;
Where changetype an changevalue could be anything. Is this possible? possibly using the eval function? the changevalue works fine, just getting it to register the dynamic property is a problem.
My last resort will be a switch/case statement, but I would much rather a sigle/few lines of code.

Thanks, Josh
May 24 '08 #1
17 1789
hsriat
1,654 Expert 1GB
Try this...
Expand|Select|Wrap|Line Numbers
  1. var changetype = "backgroundColor";
  2. changevalue = "red";
  3. element.setAttribute('style', changetype+':'+changeval+';');
If it removes the other properties in style, then first use getAttribute and then replace the value.
May 24 '08 #2
Jezternz
145 100+
thanks! Got me thinking, and got this working:
Expand|Select|Wrap|Line Numbers
  1. if(window.attachEvent)
  2. elementdir.style.setAttribute('cssText',changetype+':'+changevalue+';'); 
  3. else elementdir.setAttribute('style',changetype+':'+changevalue+';');
Only thing is it means I am limited to the css property's not the javascript properties, what I mean is I cant change things like innerHTML.

But thats a good start, any other ideas?
May 24 '08 #3
hsriat
1,654 Expert 1GB
I think everything could be done with setAttribute, except innerHTML.
Everything means... all style properties and all the attributes which come within the tag. But innerHTML is something which lies out of the tag. So may be you could just keep that aside with a simple if statement.

Is there anything else I'm missing?
May 24 '08 #4
Jezternz
145 100+
thats a valid point. I might just go with that.
Thanks heaps, Josh

Edit: This will work but only when I only want to change a single property. as soon as I add a second property it deletes the previous one. So I made it so it just added ontop of the previous lot of cssText, only problem with that is u can end up with multiple definitions of the same property like "background:black;background:blue;background:green ;" and this wont do. so back to square one :(
May 25 '08 #5
hsriat
1,654 Expert 1GB
This will work but only when I only want to change a single property. as soon as I add a second property it deletes the previous one. So I made it so it just added ontop of the previous lot of cssText, only problem with that is u can end up with multiple definitions of the same property like "background:black;background:blue;background:green ;" and this wont do. so back to square one :(
Did you try getAttribute as I said in post #2?
May 25 '08 #6
gits
5,390 Expert Mod 4TB
Okay basicly I wanna do the following:

Expand|Select|Wrap|Line Numbers
  1. var changetype = "backgroundColor";
  2. changevalue = "red";
  3. element.style.changetype = changevalue;
Where changetype an changevalue could be anything. Is this possible? possibly using the eval function? the changevalue works fine, just getting it to register the dynamic property is a problem.
My last resort will be a switch/case statement, but I would much rather a sigle/few lines of code.

Thanks, Josh
doesn't the following simple adaption of your original code work?

Expand|Select|Wrap|Line Numbers
  1. var changetype = "backgroundColor";
  2. changevalue = "red";
  3. element.style[changetype] = changevalue;
  4.  
kind regards
May 25 '08 #7
acoder
16,027 Expert Mod 8TB
That's absolutely correct. You can avoid eval in most cases in this manner. For example, if you have variables test1, test2, etc. and you need to access them using a loop, something like the following should allow you to do so:
Expand|Select|Wrap|Line Numbers
  1. for (i = 0; i < len; i++) {
  2.     window["test"+i]... // equivalent to test1, test2, etc.
  3. }
May 25 '08 #8
Jezternz
145 100+
yeh hsriat, the problem i was having went with setAttribute. Thanks though, and thanks for the final answer, I never would have guessed you could do that.
May 26 '08 #9
hsriat
1,654 Expert 1GB
yeh hsriat, the problem i was having went with setAttribute.
setAttribute or getAttribute?
Thanks though, and thanks for the final answer,...
You are welcome :)
I never would have guessed you could do that.
Why? :(
May 27 '08 #10
acoder
16,027 Expert Mod 8TB
Why? :(
I think you've misunderstood. Sometimes you can use "you" to mean "one" or "anyone" (link).

I assume you're not joking and playing with words. If you were, sorry for the English lesson :)
May 27 '08 #11
hsriat
1,654 Expert 1GB
I think you've misunderstood. Sometimes you can use "you" to mean "one" or "anyone" (link).

I assume you're not joking and playing with words. If you were, sorry for the English lesson :)
Ok, I really misunderstood that.
English isn't my first language. And I was never good in English at school. My English is just Hollywood movies' contribution.

Thanks for that lesson ;)
May 27 '08 #12
acoder
16,027 Expert Mod 8TB
No problem. I did gather as much, though your English is pretty good for a second (or third?) language.
May 27 '08 #13
hsriat
1,654 Expert 1GB
No problem. I did gather as much, though your English is pretty good for a second (or third?) language.
Thanks, its third ;)
May 27 '08 #14
acoder
16,027 Expert Mod 8TB
I'll rephrase that then: your English is very good considering that it's your third language ;)
May 27 '08 #15
hsriat
1,654 Expert 1GB
I'll rephrase that then: your English is very good considering that it's your third language ;)
lol... thanks again



Its just, I've never heard someone praising my English. Though many do give contradicting comments. So I'm glad to hear that, especially from a British guy.

May 27 '08 #16
acoder
16,027 Expert Mod 8TB
Its just, I've never heard someone praising my English. Though many do give contradicting comments. So I'm glad to hear that, especially from a British guy.

Hey, I wasn't praising it per se, and you shouldn't be too chuffed - I'm no expert on the language...

PS. I like how you're linking to smilie images to get around the fact that they're disabled in this forum

May 27 '08 #17
hsriat
1,654 Expert 1GB
Ok, I'm back on the ground.


Actually I wasn't chuffed. Just due to the communication gap, it sounded like. I was just having fun.



... and again, you and of course Google have taught me a new word. Thanks for that.

... and even I like the smilies, I don't know why are they disabled. In cafe/lounge, one can't even use this method, because even image is disabled.

... and in the end, we should end the thread now, rather then hijacking it.

Regards,
Harpreet

May 27 '08 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: SlipperyCat | last post by:
I want to use the property grid to display instances of dynamic types. A problem I'm having is that the property grid is not respecting attributes that are set with SetCustomAttribute. I've taken...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
3
by: CAD Fiend | last post by:
Hello, Well, after an initial review of my database by my client, they have completely changed their minds about how they want their form. As a result, I'm having to re-think the whole process....
3
by: MikeY | last post by:
Hi Everyone, I am working in C#, windows forms.My question is this. All my button dynamic controls properties are present and accounted for except for the"FlatStyle" properties. I can't seem to...
3
by: Guy Harwood | last post by:
Hi, I have designed a textbox that inherits from the System.Windows.Forms.Textbox control. when the control is readonly the back color changes to a light blue to indicate that it is frozen. ...
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
5
by: cwertman | last post by:
I have a question regarding dynamic properties. I have an Object say Account --Id --Prefix --Fname --Lname --Suffix
3
by: creative1 | last post by:
Here is how you create a complex data report that involves parent and child commands and you can update information at runtime. Its pretty straight forward to work with simple queries; however,...
15
by: EDBrian | last post by:
My problem is this. Our clients create different fields they want to collect and we allow them build dynamic filters, reports etc... We run some TSQL to actually create the column and all works...
5
by: Hans Kesting | last post by:
Hi, Is there good information about the asp.net page lifecycle in combination with dynamically loaded controls? Or on "how to build dynamic controls"? I keep hitting problems where values are...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.