Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: passing style properties to function, howto?

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default passing style properties to function, howto?

    Hello everyone,
    I was making some scripts when I came across a problem, can I pass style properties as an arguement? Something like this:

    Code:
    function stylize(prop, propval, obj){
    document.getElementById("obj").style.prop = propval;
    }
    stylize(border, "1px solid red", "somediv");
    It doesn't seem to work and I am tired of trying this. Does anyone know how to make this work? If so I would be greatly appreciated if you could help me with this.

  2. #2
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default

    Hi there shachi,

    The problem is that...
    Code:
    
    document.getElementById("obj").style.prop = propval;
    
    ...will not work.
    I would suggest that, instead, you set the style changes as a class in your style sheet and then callthat...
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <style type="text/css">
    <!--
    #somediv {
        width:100px;
        height:100px;
    }
    .stuff {
        border:1px solid red;
    }
    -->
    </style>
    
    <script type="text/javascript">
    <!--
    function stylize(obj,propval){
       document.getElementById(obj).className=propval;
     }
    window.onload=function(){
       stylize('somediv','stuff');
     }
    //-->
    </script>
    
    </head>
    <body>
    
    <div id="somediv"></div>
    
    </body>
    </html>
    coothead

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The same types of problems apply to this code that apply to the code coothead posted in this thread. However, the basic idea is sound: it's much better to use a CSS class and the className property when dealing with this sort of thing.

    However, I sense that the code you posted was just a demonstration, and perhaps your actual code does require you to modify a property directly. Every object in Javascript is also an associative array: that is to say,
    Code:
    style.prop
    is exactly equivalent to
    Code:
    style['prop']
    As such, you can do:
    Code:
    function stylize(obj, prop, val) {
      obj.style[prop] = val;
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    That works great, Thanks a lot Twey. You completely got what I meant. Thanks a lot again.

    Would this be a cross-browser code?

    Code:
    function stylize(obj, prop, val) { 
    document.getElementById(obj).style[prop] = val; 
    }
    Brilliant!!!

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yes, it should work with any current DOM-supporting browser.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Oh, thanks again. By the way sorry to bother you but can I ask another question?

    Can you tell me why this doesn't work?

    Code:
    var elem = document.getElementById("someelement");
    while(elem.offsetLeft != 200){
    elem.style.left += 2 + "px";
    }
    The browser stops the script everytime I try to run it.

    Thanks a lot again.

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Because elem.style.left is a string. Say, for example, that its initial value was 5px. After the first iteration of your loop, it would be 5px2px. After another, it would be 5px2px2px.
    You could use parseInt():
    Code:
    var elem = document.getElementById("someelement");
    while(elem.offsetLeft !== 200)
      elem.style.left = (parseInt(elem.style.left) + 2) + "px";
    However, it seems to me to be much simpler to set:
    Code:
    elem.style.left = "200px";
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Actually, I wanted to animate the element(without using setTimeout). Any ideas how I can do it? Thanks again.

  9. #9
    Join Date
    Aug 2006
    Posts
    239
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    you are on right track, however this is going to slow down the browser considerably on weaker hardware- you ask to iterate thru loop at every available speed, I'd suggest setting up an interval instead of while loop.

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yes, setTimeout() or setInterval() is really the only way to do it.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •