View Full Version : javascript for HTML attribute

06-04-2004, 02:41 PM
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)?


06-04-2004, 03:01 PM
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

Willy Duitt
06-04-2004, 05:21 PM
My test indicates that you can not change the frameborder once parsed.
May be another type type of thing? :confused:

<script type="text/javascript">
function setBorder(){
var border = 0;
var iframe = document.getElementsByTagName('iframe');
for(var i=0; i<iframe.length; i++){
} document.getElementById('if1').setAttribute('frameborder',border);

<body onload="setBorder()">
<iframe name="if1" frameborder="1" id="if1"></iframe>
<iframe name="if2" frameborder="1" onmouseover="alert(this.frameborder)"></iframe>

06-04-2004, 05:32 PM
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:

document.getElementsByName(sElementId)[0].setAttribute(sAttrName, vAttrValue, iOptionalFlag) ;

where the text coloured red is the item in the names array

06-04-2004, 05:58 PM
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.