Using JavaScript to Target an Element Using Class

first create your variable, in this istance we called our variable “LocateByClass”. Then use the function to search the webpage by class name you do this by document.getElements by class name function.

Finally, most importantly you can have many classes on a page and so you need to target the class by adding the number parameter. this will appear in brackets at the end of the string. for example: [0] will indicate the first time that class is present in the document and target that. [1] will target the second and so fourth

var LocateByClass = document.getElementsByClassName('woocommerce-product-details__short-description')[0];

Leave a Reply