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. 

spanTag.getAttribute("style")

See inside getAttribute() how we find attribute.

Now let's see our for loop

   let spanTag = document.getElementsByTagName('span');
    if(spanTag.length){
        for(var i=0; i<spanTag.length; i++){
        var tag = spanTag[i];
        if(tag.getAttribute("style")){
            if(tag.style.textDecoration=="underline"){
                    tag.style.fontSize="26px";
                    tag.style.color="#0b99d0";

            }
           
        }
     
        }

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

Comment

Add Reviews