dcsimg
www.webdeveloper.com
Results 1 to 6 of 6

Thread: best way to set elements properties

  1. #1
    Join Date
    Aug 2010
    Location
    San Diego
    Posts
    244

    best way to set elements properties

    So if I have an element:
    Code:
    element = $("#someElement");
    There are 2 ways to set it's width
    Code:
    element.width = 200;
    or:
    Code:
    element.style.width = "200px";
    Assuming I want to use pixel units, is there advantages to one way over the other?

  2. #2
    Join Date
    Mar 2011
    Posts
    1,252
    It's element.style.width.
    Rick Trethewey
    Rainbo Design

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    uhh, both the ways you show are wrong because $ doesn't return elements...
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

  4. #4
    Join Date
    Aug 2010
    Location
    San Diego
    Posts
    244
    Quote Originally Posted by rnd me View Post
    uhh, both the ways you show are wrong because $ doesn't return elements...
    Very good point. So use
    Code:
    element.width ( 200 )
    , right?

  5. #5
    Join Date
    Jul 2009
    Posts
    45
    It would be better to set properties like that using css:

    Code:
    $('#someElement').css({'width': '200px'});
    For non css attributes (such as id, name) use:

    Code:
    $('#someElement').attr('id', 'someId');
    If I recall correctly, I have had problems setting width with css, and had to have it inline in the tag, once in a while, but I can't remember any examples (if this is even a real memory.)

  6. #6
    Join Date
    Aug 2010
    Location
    San Diego
    Posts
    244
    Thanks. Seems to work fine.

    Quote Originally Posted by ntk418 View Post
    It would be better to set properties like that using css:

    Code:
    $('#someElement').css({'width': '200px'});
    For non css attributes (such as id, name) use:

    Code:
    $('#someElement').attr('id', 'someId');
    If I recall correctly, I have had problems setting width with css, and had to have it inline in the tag, once in a while, but I can't remember any examples (if this is even a real memory.)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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