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 10 of 10
  1. #1
    New Coder
    Join Date
    Jan 2008
    Posts
    93
    Thanks
    30
    Thanked 0 Times in 0 Posts

    Changing font style to match parent font style

    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

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    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}

  • #3
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    i believe you're looking for something like this

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

    or in better context

    Code:
     
    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);
    }
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • #4
    New Coder
    Join Date
    Jan 2008
    Posts
    93
    Thanks
    30
    Thanked 0 Times in 0 Posts
    Something like this:

    Code:
    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};
    	}
    }
    ?

  • #5
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    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";
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • #6
    New Coder
    Join Date
    Jan 2008
    Posts
    93
    Thanks
    30
    Thanked 0 Times in 0 Posts
    It still comes back as italics regardless of the font-style of the parent. Thanks for the suggestion.

  • #7
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    EDIT: my mistake, i thought you said bolded for some reason
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • #8
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    Code:
     
    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");
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • #9
    New Coder
    Join Date
    Jan 2008
    Posts
    93
    Thanks
    30
    Thanked 0 Times in 0 Posts
    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.

  • #10
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    the code you posted before never actually references the parent element, just use the last post and pass in "var"


    setVarStyle("var");
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • Users who have thanked mjlorbet for this post:

    dragon6158 (05-05-2008)


  •  

    Posting Permissions

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