Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2004
    Location
    Hershey, PA
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript for HTML attribute

    Is it possible to use a javascript variable within an html tag to set an attribute? Basically, I want to declare a constant that I can insert in multiple tags.

    for example, instead of...
    <iframe name='if1' frameborder='5' ...></iframe>
    <iframe name='if2' frameborder='5' ...></iframe>

    can I do somthing like this...
    <script>var bdr='5';</script>
    <iframe name='if1' frameborder=<script>document.write(bdr)</script> ...></iframe>
    <iframe name='if2' frameborder=<script>document.write(bdr)</script> ...></iframe>

    or, do I need to set the attribute separately outside the html tag (i.e., setAttribute)?

    Thanks,
    pt

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow try this on for size

    Code:
    window.onload
    {
    	document.getElementById(sElementId).setAttribute(sAttrName, vAttrValue, iOptionalFlag) ;
    }
    >> where sElementId is the name of the object you wanna change;
    >> where sAttrName is the name of the attribute to be created or changed;
    >> where vAttrValue is the new value of the attribute;
    >> where iOptionalFlag is an optional integer (either 0, 1 or 2) which flags as how the method works

  • #3
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My test indicates that you can not change the frameborder once parsed.
    May be another type type of thing?

    Code:
    <script type="text/javascript">
     <!--//
      function setBorder(){
       var border = 0;
       var iframe = document.getElementsByTagName('iframe');
        for(var i=0; i<iframe.length; i++){
            iframe[i].setAttribute('frameborder',border);
        }   document.getElementById('if1').setAttribute('frameborder',border);
      }
     //-->
    </script>
    </head>
    
    <body onload="setBorder()">
    <iframe name="if1" frameborder="1" id="if1"></iframe>
    <iframe name="if2" frameborder="1" onmouseover="alert(this.frameborder)"></iframe>

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmmm ... according to my O'Reilly QuickRef, iframes don't use the Id attribute. in which case, document.getElementById won't work.

    you'll therefore need to use the document.getElementsByName method.

    try this:

    Code:
    document.getElementsByName(sElementId)[0].setAttribute(sAttrName, vAttrValue, iOptionalFlag) ;
    where the text coloured red is the item in the names array
    Last edited by jbot; 06-04-2004 at 04:36 PM.

  • #5
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    According to the W3 specs, the iframe element may use, as may all other elements, the id, class, style, and title attributes - from HTML 4 through XHTML 1 Transitional.


  •  

    Posting Permissions

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