473,396 Members | 1,834 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,396 software developers and data experts.

css class values not DOM object

AdrianH
1,251 Expert 1GB
Hi, I'm not exactly new to javascript, I just haven't used it in a few years. (BTW, I'm Forum Leader for C++/C. Don't want to scare off newbies here making them think that we at TSDN doen't know what we are talking about. :D)

I understand the DOM to the point that I thought I did, but I've come across a slight problem. A DOM object that is used to reference a class doesn't have its members populated when the class style has set them. When setting the class style directly on the html tag, is does work. I.e. style="width:40" will populate the DOM object's width member to 40.

Anyone know why or how to get around this? I need to know the location and dimentions of a DOM object and it would be a pain (as well as poor style) to put all of the locations into the tag's style element on an individual basis.

Here is the code that I am working with that shows this:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  <head>
  3.   <style>
  4.  
Expand|Select|Wrap|Line Numbers
  1.    .move {
  2.     position:relative;
  3.     top:0px;
  4.     left:0px;
  5.     height:100px;
  6.     width:100px;
  7.     background-color:#777777;
  8.     color:white;
  9.    }
  10.    .move2 {
  11.     top:50;
  12.    }
  13.  
Expand|Select|Wrap|Line Numbers
  1.   </style>
  2.   <script language="JavaScript" type="text/javascript">
  3.  
Expand|Select|Wrap|Line Numbers
  1.    var aDOM = 0;
  2.    var nsDOM = 0
  3.    var stdDOM = document.getElementById;
  4.  
  5.    var style = 1;
  6.    var value = 0;
  7.  
  8.    if (stdDOM) {
  9.     aDOM = 1;
  10.    }
  11.    else {
  12.     var nsDOM = ( (navigator.appName.indexOf('Netscape') != -1) && (parseInt(navigator.appVersion) == 4));
  13.     if (nsDOM) {
  14.      aDOM = 1;
  15.     }
  16.    }
  17.  
  18.    function xDOM(objectId, wS) {
  19.     if (stdDOM) {
  20.      if (wS) {
  21.       return document.getElementById(objectId).style;
  22.      }
  23.      else {
  24.       return document.getElementById(objectId);
  25.      }
  26.     }
  27.     else if (nsDOM) {
  28.       return document.layers[objectId];
  29.     }
  30.     alert("Unrecognised DOM");
  31.    }
  32.  
  33.    function myParseInt(value)
  34.    {
  35.      return !value?0:parseInt(value);
  36.    }
  37.  
  38.    /////////////////////////////////////////////////////
  39.    function obj(id)
  40.    {
  41.      this.value=xDOM(id, value);
  42.      this.style=xDOM(id, style);
  43.  
  44.      if (!this.value || !this.style) {
  45.       alert("value = " + this.value + " and style = " + this.style);
  46.      }
  47.  
  48.      this.moveRel = function(x, y)
  49.      {
  50.       this.style.left=this.getLeft()+x;
  51.       this.style.top=this.getTop()+y;
  52.      }
  53.  
  54.      this.moveAbs = function(x, y)
  55.      {
  56.       this.style.left=x;
  57.       this.style.top=y;
  58.      }
  59.  
  60.      this.resizeRel = function(x, y)
  61.      {
  62.       this.style.width=this.getWidth()+x;
  63.       this.style.height=this.getHeight()+y;
  64.      }
  65.  
  66.      this.resizeAbs = function(x, y)
  67.      {
  68.       this.style.width=x;
  69.       this.style.height=y;
  70.      }
  71.  
  72.      this.getTop = function()
  73.      {
  74.       return myParseInt(this.style.top);
  75.      }
  76.  
  77.      this.getLeft = function()
  78.      {
  79.       return myParseInt(this.style.left);
  80.      }
  81.  
  82.      this.getWidth = function()
  83.      {
  84.       return myParseInt(this.style.width);
  85.      }
  86.  
  87.      this.getHeight = function()
  88.      {
  89.       return myParseInt(this.style.height);
  90.      }
  91.  
  92.      this.setOnClick = function(fun)
  93.      {
  94.       this.value.onclick = fun;
  95.      }
  96.    }
  97.  
  98.  
  99.    function initPage()
  100.    {
  101.     var a = new obj('testObj2');
  102.     alert("(" + a.getLeft() + ", " + a.getTop() + ")-(" + a.getWidth() + ", " + a.getHeight() + ")");
  103.     a.moveRel(10,10);
  104.     alert("(" + a.getLeft() + ", " + a.getTop() + ")-(" + a.getWidth() + ", " + a.getHeight() + ")");
  105.     a.moveRel(10,10);
  106.     alert("(" + a.getLeft() + ", " + a.getTop() + ")-(" + a.getWidth() + ", " + a.getHeight() + ")");
  107.     alert("Page loaded");
  108.    }
Expand|Select|Wrap|Line Numbers
  1.   </script>
  2.  </head>
  3.  <body onload="initPage()">
  4.   <div class="move" id="testObj">hello</div>
  5.   <div class="move move2" id="testObj2" >bye</div>
  6.  </body>
  7. </html>
Thanks for your help.


Adrian
Jun 10 '07 #1
2 1506
drhowarddrfine
7,435 Expert 4TB
OT- in CSS you must set units for everything but zero. So 50 must be 50px or whatever.
Jun 10 '07 #2
mrhoo
428 256MB
You can get the cascaded style properties of any element in IE and the firefox/gecko browsers, each with their own interface, but Opera and some others do not make it so easy.

If you call deepCss(element,property) the cascaded element.style.property will be returned, at least in IE and firefox.

Note that it uses a string method called 'dasher', defined below.

Expand|Select|Wrap|Line Numbers
  1. deepCss=function(who, cssprop){
  2.     var  val= '', str= '';
  3.     str= cssprop.dasher();
  4.     val= who.style[str];
  5.     if(!val){
  6.         if(who.currentStyle) val= who.currentStyle[str];
  7.         else{
  8.             var dv= document.defaultView;
  9.             if(dv && dv.getComputedStyle){
  10.                 str= str.dasher(true);
  11.                 val= dv.getComputedStyle(who,'').getPropertyValue(str);
  12.             }
  13.         }
  14.     }
  15.     return val;
  16. }
/*
dasher takes a css property, like 'font-size' or 'border-top-color',
and javascripts it to 'fontSize' or 'borderTopColor',
or, if true is passed as its argument, the reverse.
*/
Expand|Select|Wrap|Line Numbers
  1. String.prototype.dasher= function (boo){
  2.     var x = this;
  3.     if (/^[A-Z]+$/.test(x) || /\-/.test(x)) x = x.toLowerCase();
  4.     if (boo=== true ){
  5.         if(/[a-z][A-Z]/.test(x)){
  6.             x= x.replace(/[A-Z]/g, function (w){
  7.                 return "-" + w.toLowerCase();
  8.             });
  9.         }
  10.     }
  11.     else if(/\-/.test(x)){
  12.         x= x.replace(/\-[a-z]/g, function (w){
  13.             return w.charAt(1).toUpperCase() + w.substring(2);
  14.         });
  15.     }
  16.     return x;
  17. }
  18.  
Jun 11 '07 #3

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

Similar topics

3
by: gry | last post by:
I often find myself wanting an instance attribute that can take on only a few fixed symbolic values. (This is less functionality than an enum, since there are no *numbers* associated with the...
10
by: Not Available | last post by:
On the host server: namespace JCart.Common public class JCartConfiguration : IConfigurationSectionHandler private static String dbConnectionString; public static String ConnectionString { get...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
2
by: Rick Palmer | last post by:
The code below is a class I am creating in order to help facilitate a generic "data layer" assembly. Anyway, as you see the RecordCriteria class contains three hashtables, and a structure called...
10
by: Joel | last post by:
Is it true that if we don't specify a default constructor for our class, then the C# compiler provides us with its own that zeroes (or assigns default values) to the data members? I wrote a...
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?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.