CSS Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignCSS Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 12th, 2003, 04:37 PM
khwang's Avatar
khwang khwang is offline
Über nübe
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Babylon 4
Posts: 240 khwang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 27 sec
Reputation Power: 6
Get CSS rule by ID?

Here is one way to get a CSS rule:

Code:
document.styleSheets[0].rules.item(0).style.getPropertyValue('width');


In the above example, I have to know exactly which element of the array contains the class.

Is there an alternate way to get a CSS rule, just by ID? For example, if I have a DIV ID='bacon', then can I get the CSS rule for that DIV?

Reply With Quote
  #2  
Old November 13th, 2003, 10:17 AM
melsana's Avatar
melsana melsana is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: MD
Posts: 373 melsana User rank is Corporal (100 - 500 Reputation Level)melsana User rank is Corporal (100 - 500 Reputation Level)melsana User rank is Corporal (100 - 500 Reputation Level)melsana User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via AIM to melsana
It is actually simpilar than you might think...

Code:
alert(document.getElementById('bacon').style.width)


That will make an alert box pop up with the style width of the element with the ID bacon (provided that style has been set).

The width must be set for that ID it can't just be set for a class which that ID is a part of.

in otherwords

---inside style sheet---
.myclass { width: 500px; }

--inside html---
<DIV id="bacon" class="myclass">


If this is how you set the width of the DIV the javascript getElementById('bacon').style.width will not return a width.

You must have either

--- in javascript somewhere before your request ---
getElementById('bacon').style.width = "400px"

or
--- in style sheet --
#bacon { width: 400px; }

Then the code will return the correct width.

Reply With Quote
  #3  
Old November 13th, 2003, 02:36 PM
khwang's Avatar
khwang khwang is offline
Über nübe
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Babylon 4
Posts: 240 khwang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 27 sec
Reputation Power: 6
OK, I tried your suggestion:

Code:
<html>
<head>
	<title>test</title>
	<style type="text/css">
	#bacon { width:400px; border:1px solid black; }
	</style>
</head>

<body>
<div id="bacon">content here</div>

<script>
alert(document.getElementById('bacon').style.width);
</script>

</body>
</html>



But I get an empty alert message?
__________________
Hello, old friend...

Last edited by khwang : November 13th, 2003 at 02:43 PM.

Reply With Quote
  #4  
Old November 13th, 2003, 02:57 PM
melsana's Avatar
melsana melsana is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: MD
Posts: 373 melsana User rank is Corporal (100 - 500 Reputation Level)melsana User rank is Corporal (100 - 500 Reputation Level)melsana User rank is Corporal (100 - 500 Reputation Level)melsana User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via AIM to melsana
Okay... so I wasn't quite accurate in my description...

I didn't actully test when you set the style in a style sheet...

But if you do this it will work...

Code:
<html>
<head>
	<title>test</title>
	<SCRIPT language=javascript>
function setStyle() {
	document.getElementById('bacon').style.width ="400px"
        document.getElementById('bacon').style.border ="1px solid black"
}
	</SCRIPT>
</head>

<body onLoad=setStyle()>
<div id="bacon">content here</div>

<script>
function showAlert(){
  alert(document.getElementById('bacon').style.width);
}
</script>

<FORM>
<INPUT type=button onClick=showAlert() value="Show Width">
</FORM>
</body>
</html>


It just may not be what you want since I'm sure you would prefere to use a css style sheet rather than use javascript to define the styles.

Because the style is not set until after the page loads you have to request the alert box after the page loads... hense the form button.

Reply With Quote
  #5  
Old November 13th, 2003, 03:11 PM
khwang's Avatar
khwang khwang is offline
Über nübe
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Babylon 4
Posts: 240 khwang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 27 sec
Reputation Power: 6
Quote:
Originally posted by melsana
...
It just may not be what you want since I'm sure you would prefer to use a css style sheet rather than use javascript to define the styles.

Because the style is not set until after the page loads you have to request the alert box after the page loads... hence the form button.


Yes, I would prefer to use the CSS to set all styles. Then make some calculations (of height) based on the styles, then set height for another style.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignCSS Help > Get CSS rule by ID?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT