...

View Full Version : javascript for HTML attribute



pt83
06-04-2004, 01: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)?

Thanks,
pt

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

Willy Duitt
06-04-2004, 04: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++){
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>

jbot
06-04-2004, 04: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

Choopernickel
06-04-2004, 04: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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum