Lee-yoshi Posted January 26, 2012 Share Posted January 26, 2012 Hey guys! Another stupid problem from yours truely :-( I've created a StyleSheet with a few custom paragraph styles (Such as below): p.n_textheader {font-size: 15em; font-weight: bold; margin: 0px;} The StyleSheet is linked to the .php file all fine, but how can I change the font-size element using JavaScript in my .php file? I've tried things like document.getElementById( "n_textheader" ).style.fontSize;document.getElementById( "p.n_textheader" ).style.fontSize; document.getElementById( "p" )( "n_textheader" ).style.fontSize;document.getElementByTagName( "n_textheader" ).style.fontSize; document.getElementById( "n_textheader" ).style.font-size; ...but alas, no joy :-( Any help, greatly appreciated as always :-) Thanks! Link to comment Share on other sites More sharing options...
justsomeguy Posted January 26, 2012 Share Posted January 26, 2012 CSS rules are not elements, elements are HTML elements. There's some information about using Javascript to work with CSS here: http://dev.opera.com/articles/view/dynamic-style-css-javascript/ Link to comment Share on other sites More sharing options...
dsonesuk Posted January 26, 2012 Share Posted January 26, 2012 you are trying to target a paragraph element with classname with getElementById that targets a id element ref and getElementsByTagName that only targets the tag name of "p" or "div", and you not changing the css, you applying a inline style to the element which will take priority over css styling in the head of the document, or external css stylesheet UNLESS they are using !important, you need a script that combines both parent_elem = document.body; will target all p elements with class name "n_textheader" parent_elem = document.getElementById("content2"); will only target p elements with class name "n_textheader" within a parent element with id ref content2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title><script type="text/javascript">/*<![CDATA[*//*---->*//*--*//*]]>*/</script><style type="text/css">body{background-color:#FFF;}p{margin: 0.5em 0; font-size: 20px;}.em_icons { position:relative; top:4px; height:1em;}</style><script type="text/javascript">/*<![CDATA[*//*---->*/window.onload=function(){//parent_elem = document.body;parent_elem = document.getElementById("content2");child_elements=parent_elem.getElementsByTagName("p");for(i=0;i<child_elements.length;i++){if(child_elements[i].className=="n_textheader"){child_elements[i].style.fontSize="50px";}}}/*--*//*]]>*/</script></head><body><div id="content"><p class="n_textheader">I'm flying to Ghana on 27th October and will be there for a month :-D - anyone else going around that time? If so I'd love to hear from you :-) </p><p>I'm flying to Ghana on 27th October and will be there for a month :-D - anyone else going around that time? If so I'd love to hear from you :-) </p><p class="n_textheader">I'm flying to Ghana on 27th October and will be there for a month :-D - anyone else going around that time? If so I'd love to hear from you :-) </p><p>I'm flying to Ghana on 27th October and will be there for a month :-D - anyone else going around that time? If so I'd love to hear from you :-) </p></div><div id="content2"><p>I'm flying to Ghana on 27th October and will be there for a month :-D - anyone else going around that time? If so I'd love to hear from you :-) </p><p class="n_textheader">I'm flying to Ghana on 27th October and will be there for a month :-D - anyone else going around that time? If so I'd love to hear from you :-) </p><p>I'm flying to Ghana on 27th October and will be there for a month :-D - anyone else going around that time? If so I'd love to hear from you :-) </p><p class="n_textheader">I'm flying to Ghana on 27th October and will be there for a month :-D - anyone else going around that time? If so I'd love to hear from you :-) </p></div></body></html> Link to comment Share on other sites More sharing options...
Lee-yoshi Posted February 1, 2012 Author Share Posted February 1, 2012 Thanks guys, it's all been sussed now using similar to your example dsonesuk. Think I'm starting to get the hang of this JavaScript marlarky afterall :-) Thanks very much again!! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.