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

Thread: Runtime create attribute

  1. #1
    Join Date
    Jan 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Runtime create attribute

    how to create Runtime html tag attribute using javascript?

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Which attribute you want to create on which tag (element)?

  3. #3
    Join Date
    Jan 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    i have create select attribute ( multiple ) at runtime ! it possible ?

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    First of all, runtime is an imprecise term as regards both javascript and HTML. Do you mean onload, or as the page is being parsed?

    If the latter, the easiest way is of course to hard code the attribute to the select tag.

    There are at least several other options, here are two -

    As the page is parsed:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    </head>
    <body>
    <select id="myselect">
    <option>Something</option>
    <option>Something</option>
    <option>Something</option>
    </select>
    
    <script type="text/javascript">
    document.getElementById('myselect').multiple = true;
    </script>
    </body>
    </html>
    Onload:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    onload = function(){
     document.getElementById('myselect').multiple = true;
    }
    </script>
    
    </head>
    <body>
    <select id="myselect">
    <option>Something</option>
    <option>Something</option>
    <option>Something</option>
    </select>
    
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Just a quick follow up question John, is the value of true for multiple attribute valid when doing like that (el.attribute=value)?

    I thought it was only multiple that's valid.
    http://www.w3.org/TR/html401/interact/forms.html#h-17.6

    Hope to hear some of your thoughts.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    From the link you posted (emphasis added):

    multiple [CI]
    If set, this boolean attribute allows multiple selections. If not set, the SELECT element only permits single selections.
    In case you didn't know, boolean means true or false. However, I believe you are correct as far as valid HTML/XHTML goes. But since this is javascript, things are different. The bottom line is that the browser will set the valid HTML for the DTD of the page visa vis its own requirements for it once javascript sets the boolean value.

    To think of it another way, what I did wasn't exactly setting the attribute value of the tag. I set the boolean value of the property of the object. This has the same ultimate result in any compliant browser. If I had quoted 'true', it would have been invalid unless error corrected.
    Last edited by jscheuer1; 01-09-2009 at 12:43 PM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. The Following User Says Thank You to jscheuer1 For This Useful Post:

    rangana (01-10-2009)

  8. #7
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Okay, that makes sense. Thanks for clearing it up. Will take this from you:
    Quote Originally Posted by jscheuer1
    I set the boolean value of the property of the object. This has the same ultimate result in any compliant browser.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

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

    Default

    If I had quoted 'true', it would have been invalid unless error corrected.
    No, it would have the same effect — it would be type-coerced. It's only 'false' you've got to look out for, since it's truthy.

    Not to repeat what John said, but maybe to shed light on it from a different angle: the attribute on the element is boolean (true or false) but HTML doesn't have boolean datatypes, so instead of writing multiple=true and multiple=false in HTML, we write multiple="multiple" or multiple="". In fact, that is mostly for XHTML compatibility: in HTML we would normally use shorthand, just writing multiple or else leaving the attribute out entirely.
    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!

  10. The Following User Says Thank You to Twey For This Useful Post:

    rangana (01-10-2009)

  11. #9
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Thank you Twey. That makes a lot more sense.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  12. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    No, it would have the same effect — it would be type-coerced. It's only 'false' you've got to look out for, since it's truthy.
    In my view, type coercion is a form of error correction. And just to be clear, though I'm pretty sure everyone immediately involved already gets this, false (unquoted) is fine. It's just that once it it is quoted, it becomes a string. Any non-empty string will in this situation be type coerced into a value of true.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •