...

View Full Version : Changing font style to match parent font style



dragon6158
04-28-2008, 07:42 PM
Hello,
I am trying to change the font style of var element to match the font style of whatever i am inserting the var element into. This element could be a header, list item, paragraph or eny other kind of element. Any help in doing this would be appreciated because all I get when I try to access the parent element is undefined or null.

Thanks,
Don

mrhoo
04-28-2008, 07:49 PM
var element to match the font style

do you mean a <var> element, and do you mean font-style?

if so, use css: var{font-style:inherit}

mjlorbet
04-28-2008, 07:54 PM
i believe you're looking for something like this

myElement.style.fontStyle = oldElement.style.fontStyle;

or in better context



function doAddElem(prnt, objType, contents){
var fntStyl = prnt.style.fontStyle;
var obj = document.createElement(objType);
obj.innerHTML = contents;
obj.style.fontStyle = fntStyl;
prnt.appendChild(obj);
}

dragon6158
04-28-2008, 07:54 PM
Something like this:



function setVarStyle(){
var varcopy = document.getElementsByTagName("var");
var varparent;
if (varcopy.length < 1) return false;
for (var i = 0; i < varcopy.length; i++){
varcopy[i].css{font-style:inherit};
}
}
?

mjlorbet
04-28-2008, 07:59 PM
function setVarStyle(){
var varcopy = document.getElementsByTagName("var");
var varparent;
if (varcopy.length < 1) return false;
for (var i = 0; i < varcopy.length; i++){
varcopy[i].css{font-style:inherit};
}
}


bolded line should read

varcopy[i].style.fontStyle = "inherit";

dragon6158
04-28-2008, 08:09 PM
It still comes back as italics regardless of the font-style of the parent. Thanks for the suggestion.

mjlorbet
04-28-2008, 08:11 PM
EDIT: my mistake, i thought you said bolded for some reason

mjlorbet
04-28-2008, 08:15 PM
function setVarStyle(tgnm){
var varcopy = document.getElementsByTagName(tgnm);
if (varcopy.length < 1) return false;
for (var i = 0; i < varcopy.length; i++){
varcopy[i].style.fontStyle = varcopy[i].parentElement?varcopy[i].parentElement.style.fontStyle:"normal";
}
}


pass in the name of the tag to the function, ie

setVarStyle("div");

dragon6158
04-28-2008, 08:16 PM
Apparently the var element is italicized by default and what my supervisor wants me to do is set the font attributes based on what the parent has. With doing nothing, the var element inherits everything from the parent but the font-style. I can set the font-style by hard coding it but I am having problems accessing the font-style property of the parent.

mjlorbet
04-28-2008, 08:19 PM
the code you posted before never actually references the parent element, just use the last post and pass in "var"


setVarStyle("var");



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum