I would like to style a <p>
dependant on the amount of text it contains ;
using plain - vanila javaScript {no external library}
+ of course css & html 4.1 for use in pre 29 FireFox.
Using 1 : Change an element's class with JavaScript - for the way to reference the html element getElementById("MyElement")
...
and adapting : How to detect when multiple lines are occurring in a div? ele.innerHTML.length >= 70
to my specific needs.
I cannot get http://jsfiddle.net/positive_x/HeCj7/ to work.
In that jsfiddle , I am trying to use the first way to prove - out & learn the algorithm ;
html + css & javaScript in the jsfiddle . net / positive_x / HeCj7 /
<aside>
<p>This is a plain - old paragraph.</p>
<p id='p_id1'>This is the p_id1 (blue) p</p>
<p id='p_id2'>This is the p_id2 (really l o n g ___ R E D ) paragraph . </p>
<p class='styletest'>This is a short class= p</p>
<p class='styletest'>This is other class= (really long _ R E D) paragraph .</p>
</aside>
<script>window.onload = style_byID_Blue_short_p;</script>
/* css in jsfiddle */
.blue {color: #117;}
.green {color: #161;}
.red {color: #511;}
/* javascript in jsfiddle TESTing 1 getElementById('idname') way */
function style_byID_Blue_short_p () {
var textIn = document.getElementById('p_id1');
var txtLen = textIn.innerHTML.length;
if( txtLen<30 ){
textIn.className += ' blue'; //turn it blue
alert('here is style_byID_Blue_short_p that IDS blue!'); //for testing
} else {
textIn.style.color = #511; //turn it red
alert('style_byID_Blue_short_p that is red.'); //for testing
}
}
Additionally, I really need to apply the styling to a number of <p>
via the getElementsByClass("myclassname")
that is in the second part of the jsfiddle shown in the jsfiddle itself.
The required array nomenclature has me intimidated ; (
; so I attempted the first _ simpler portion first.
As a copy-&-paste
coder , I could use your help.
Best regards.
` referenced by `getElementsByClassName('styletest')` & get-rid-of the test alert -- In the second "desired" portion of the jsfiddle.
– positive_x Jul 22 '14 at 03:42s based on the length of text contaiined in them.
– positive_x Jul 22 '14 at 04:00