How to give all p elements a class name?


John Carlson

I've tried to give all my p elements a class name and can't seem to figure out what the problem is.

var para = document.getElementsByTag("p");
para.className += "myClass";
tyme joint venture

You have to iterate over the collection and assign (also document.getElementsByTagName("p");)

for (var i = 0; i < para.length; i++) {
    para[i].className += " myClass";
    //                   ^^^
    //As @nnnnnn pointed out - a space beforehand will ensure the class is added to existing classes - and not added as a single word.
}

Related


How can I get all elements by class name of the same class?

username I am getting all elements by class name but I am not able to get This command works when I try to get an element document.getElementsByClassName('div1')[5].value But this command doesn't work var i=0; for ( i < 6; i++) { x = document.getElementsB

How to get all elements by class name in Iframe PEGA

Sal I am trying to get element by className in iframe. When I use IE5, "getElementsByClassName" doesn't work. To get any element in the document, I use pega with the following code: pega.util.Dom.getElementsByClassName("BlaBlaName");However, this doesn't work

How to access all dom elements by class name in React?

Mizlur I want to access all dom elements with a specific class name , but it doesn't seem to work on react like in pure javascript. Can't seem to find the element. import React from 'react'; import {Route, Link} from 'react-router-dom'; import {Bootstrap, Grid

How to get all elements by class name in Iframe PEGA

Sal I am trying to get element by className in iframe. When I use IE5, "getElementsByClassName" doesn't work. To get any element in the document, I use pega with the following code: pega.util.Dom.getElementsByClassName("BlaBlaName");However, this doesn't work

How to get all elements by class name in Iframe PEGA

Sal I am trying to get element by className in iframe. When I use IE5, "getElementsByClassName" doesn't work. To get any element in the document, I use pega with the following code: pega.util.Dom.getElementsByClassName("BlaBlaName");However, this doesn't work

How to access all dom elements by class name in React?

Mizlur I want to access all dom elements with a specific class name , but it doesn't seem to work on react like in pure javascript. Can't seem to find the element. import React from 'react'; import {Route, Link} from 'react-router-dom'; import {Bootstrap, Grid

How to get all elements by class name in Iframe PEGA

Sal I am trying to get element by className in iframe. When I use IE5, "getElementsByClassName" doesn't work. To get any element in the document, I use pega with the following code: pega.util.Dom.getElementsByClassName("BlaBlaName");However, this doesn't work

How to access all dom elements by class name in React?

Mizlur I want to access all dom elements with a specific class name , but it doesn't seem to work on react like in pure javascript. Can't seem to find the element. import React from 'react'; import {Route, Link} from 'react-router-dom'; import {Bootstrap, Grid

How to access all dom elements by class name in React?

Mizlur I want to access all dom elements with a specific class name , but it doesn't seem to work on react like in pure javascript. Can't seem to find the element. import React from 'react'; import {Route, Link} from 'react-router-dom'; import {Bootstrap, Grid

How to give a class to nested arrey elements?

Z 宗 · ((Egzon Berisha) I have a JSON that I store in a variable called photographersInfo and I get the data from there, but what I want to do is: I want to display the labels corresponding to each photographer and give each photographer a border ! Like this ph

How to give a class to nested arrey elements?

Z 宗 · ((Egzon Berisha) I have a JSON that I store in a variable called photographersInfo and I get the data from there, but what I want to do is: I want to display the labels corresponding to each photographer and give each photographer a border ! Like this ph

How to give a class to nested arrey elements?

Z 宗 · ((Egzon Berisha) I have a JSON that I store in a variable called photographersInfo and I get the data from there, but what I want to do is: I want to display the labels corresponding to each photographer and give each photographer a border ! Like this ph

How to give a class to nested arrey elements?

Z 宗 · ((Egzon Berisha) I have a JSON that I store in a variable called photographersInfo and I get the data from there, but what I want to do is: I want to display the labels corresponding to each photographer and give each photographer a border ! Like this ph

How to give a class to nested arrey elements?

Z 宗 · ((Egzon Berisha) I have a JSON that I store in a variable called photographersInfo and I get the data from there, but what I want to do is: I want to display the labels corresponding to each photographer and give each photographer a border ! Like this ph

How to use javascript to give a class name to a td?

Jack Mason In php's foreach loop, a list of lines with it is tdcreated . In 1 of these lines, the folder is named tmp. Is it possible to provide tda class hiddenso that the folders are not visible? This is how foreach generates my html: <tr> <td> <a hre

How to use javascript to give a class name to a td?

Jack Mason In php's foreach loop, a list of lines with it is tdcreated . In 1 of these lines, the folder is named tmp. Is it possible to provide tda class hiddenso that the folders are not visible? This is how foreach generates my html: <tr> <td> <a hre

jQuery get all elements by class name

Jack In the process of learning javscript and jquery, went through google's page but it doesn't seem to work. Basically, I'm trying to collect the innerhtml of the class and it seems that jquery is recommended over pure javascript into document.write. Here is

Get all elements by class name of textbox in javascript

Cousins: I have several textboxes with class textID on my JavaScript webpage. so if i do function UpdateTotals() { getText = document.getElementsByClassName('textID').value; } All the values are numbers, so how do I add all the values in this class toget

jQuery get all elements by class name

Jack In the process of learning javscript and jquery, went through google's page but it doesn't seem to work. Basically, I'm trying to collect the innerhtml of the class and it seems that jquery is recommended over pure javascript into document.write. Here is

Get all elements by class name in polymer

Sudipta Roy Can someone tell me how to get all elements by class name in polymer? Of course we can this.$$(selector), but it 's explicitly mentioned in the documentation .$$ returns the first node in the local DOM that matches selector ` Niklas Should this.$$(

Get all elements by class name of textbox in javascript

Cousins: I have several textboxes with class textID on my JavaScript webpage. so if i do function UpdateTotals() { getText = document.getElementsByClassName('textID').value; } All the values are numbers, so how do I add all the values in this class toget

Returns all elements matching the class name

Okra document.getElementsByClassName('big-button')[0,1,2].style.display = "inline-flex"; I'm trying to apply that style to all/any elements matching a class name. I tried that [0,1,2]because I only have 3 instances, but it only targets the third instance. This