Javascript Find Tag and Style Elements and Attributes

Created At: 2023-02-10 02:42:39 Updated At: 2023-02-10 02:53:23

Here, we will use pure Javascript to find a tag and style the tag attributes. We will find span tag using Javascript.

Let's see how to find it. We may use the document function getElementsByTagName(), this will take any tag and find it for you if it exist.

 let spanTag = document.getElementsByTagName('span');

You may find any tags like this. Here I created a variable name spanTag and saved all the tags name span. After that we will use for loop to loop through and find certain attriabutes.

After that we will look for style attribute, to find any attribute you may just use getAttribute() function. 


See inside getAttribute() how we find attribute.

Now let's see our for loop

   let spanTag = document.getElementsByTagName('span');
        for(var i=0; i<spanTag.length; i++){
        var tag = spanTag[i];


In the loop, we style the attribute's properties. Using the above tips, you may find any tags and style the attributes properties.


Add Reviews