Click to See Complete Forum and Search --> : help me, javascript with specific div id


neizo
06-09-2009, 10:29 PM
Hi, I have this javascript function
function increaseFontSize() {
var p = document.getElementsByTagName('div');
for(i=0;i<p.length;i++) {
if(p[i].style.fontSize) {
var s = parseInt(p[i].style.fontSize.replace("px",""));
} else {
var s = 12;
}
if(s!=max) {
s += 1;
}
p[i].style.fontSize = s+"px"
}
}
and I need to call a div with specific id,
like
var p = document.getElementsByTagName('div id=someid');
how can I do it?

thank you.

bradgrafelman
06-09-2009, 11:12 PM
You'd have to use the appropriate function; try getElementById() instead.

neizo
06-10-2009, 09:44 AM
I did it by id but is not working..
function increaseFontSize() {
var p = document.getElementById('thetext');
for(i=0;i<p.length;i++) {
if(p[i].style.fontSize) {
var s = parseInt(p[i].style.fontSize.replace("px",""));
} else {
var s = 12;
}
if(s!=max) {
s += 1;
}
p[i].style.fontSize = s+"px"
}
}

and my div is
<div id="thetext" style="color: #3C3C3C; font-size: 10px; font-family: Verdana;">
<br /><br />
<? echo $article[11]; ?>
</div>

JPnyc
06-10-2009, 02:27 PM
There is no need for a loop. The getElementsByTagName function returns an object, but get elements by ID returns a single element because IDs must be unique.

function increaseFontSize() {
var p = document.getElementById('thetext');

if(p.style.fontSize) {
var s = parseInt(p.style.fontSize.replace("px",""));
}

else {
var s = 12;
}
if(s!=max) {
s += 1;
}
p.style.fontSize = s+"px"
}

neizo
06-10-2009, 05:03 PM
thank you :)