0
**I have many separated paragraphs in one HTML page all of them have the same id **id=text** when I use the onmousemove event only the first paragraph can increase and decrease the font size not all of them although they have the same id  .** 

This is the javascript code:

function incfont(){
    var t= document.getElementById('fontsize').value;
    var x= document.getElementById('text');
    x.style.fontSize = t+"px";

**Here are an example of paragraphs with the same id **

<p id="test">paragraph 1 </p>
<p id="test">paragraph 2 </p>

3 Answers3

0

As you have many paragraphs, you shouldn't be setting an id, but a class.

getElementById() only gets a single element.

Please refer to this question and the selected answer. GetElementByID - Multiple IDs

0

In Html

<p class="test">paragraph 1 </p>
<p class="test">paragraph 2 </p>

In Script

function incfont(){
    var t= document.getElementById('fontsize').value;
    var x= document.getElementsByClassName("test");
    for(var i=0;i<x.length;i++){
    x[i].style.fontSize = t+"px";
    }
}
srp
  • 619
  • 7
  • 18
-1

In Html

//Instead of Id use class in HTML
<p class="test">paragraph 1 </p>
<p class="test">paragraph 2 </p>

In Script

function incfont(){
    var t= document.getElementById('fontsize').value;
    //Here you need to change getElemetById to getElementsByClassName because getElementById() only gets a single element
    var x= document.getElementsByClassName("test");
    x.style.fontSize = t+"px";
    }
}
Dinesh kapri
  • 68
  • 1
  • 2
  • 9