Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I select multiple tags using getElementsByTagName?

I'm using a javascript snippet in order for visitors to my site to increase the font size on all paragraphs using the following javascript:

function increaseFontSize() {        var paragraphs = document.getElementsByTagName('p');       for(i=0;i<paragraphs.length;i++) {             if(paragraphs[i].style.fontSize) {              var s = parseInt(paragraphs[i].style.fontSize.replace("px",""));         } else {                var s = 14;         }          if(s != max) {               s += 1;          }          paragraphs[i].style.fontSize = s+"px"     }  }  

How can I also include "li" to this code so that "p" and "li" are the selected elements that are affected?

I would also like to avoid adding a class or an id to my "li" or "ul". Is there a way to select two tags at once?

like image 618
biddybump Avatar asked Jan 17 '14 18:01

biddybump


People also ask

How do you select multiple tags?

For windows: Hold down the control (ctrl) button to select multiple options. For Mac: Hold down the command button to select multiple options.

How do I select all tags?

The * selector selects all elements. The * selector can also select all elements inside another element (See "More Examples").

What does document getElementsByTagName do?

getElementsByTagName() The getElementsByTagName method of Document interface returns an HTMLCollection of elements with the given tag name. The complete document is searched, including the root node.

Does getElementsByTagName return an array?

getElementsByTagName - the method name itself implies that it will return multiple elements - i.e. an array. The method always returns an array, with the length equal to the number of matching elements. As such you must always access the elements by the index of the element in the array.


1 Answers

No, you can't select multiple tags with a single call to getElementsByTagName. You can either do two queries using getElementsByTagName or use querySelectorAll.

JSFiddle

var elems = document.querySelectorAll('p,li') 
like image 159
Daniel Imms Avatar answered Oct 13 '22 07:10

Daniel Imms