samedi 14 septembre 2019

JAVASCRIPT HELP!! Get a specific text value of an element when there is multiple elements with the same class names and attribute names

<div class="my-attributes">
                <div class="row">
                    <div class="SameClass"><strong>Condition</strong></div>
                    <div class="SameClass-value" itemprop="condition">New</div>
                </div>
                <div class="row">
                    <div class="SameClass"><strong>Color</strong></div>
                    <div class="SameClass-value" itemprop="color">Black</div>
                </div>

            <div class="row">
                <div class="SameClass"><strong>Availability</strong></div>
                <div class="SameClass-value" itemprop="avail">In Stock</div>
            </div>

                <div class="row">
                    <div class="SameClass"><strong>Quantity</strong></div>
                    **<div class="SameClass-value" itemprop="qty"> 5 </div>**
                </div>

                <div class="row">
                    <div class="SameClass"><strong>Price</strong></div>
                    <div class="SameClass-value" itemprop="price">250</div>
            </div>

I have multiple divs that has the same class and 'itemprop' attribute and I need to be able to create a function that will return the Quantity (which in the example above is '5')

the catch here is that the structure you see above changes in other pages. In the example above, the Condition and the Color shows but on other pages those do not appear which means the structure changes except the class name and the attribute value of the 'itemprop'. See below:

<div class="my-attributes">
            <div class="row">
                <div class="SameClass"><strong>Availability</strong></div>
                <div class="SameClass-value" itemprop="avail">In Stock</div>
            </div>

                <div class="row">
                    <div class="SameClass"><strong>Quantity</strong></div>
                    **<div class="SameClass-value" itemprop="qty"> 5 </div>**
                </div>

                <div class="row">
                    <div class="SameClass"><strong>Price</strong></div>
                    <div class="SameClass-value" itemprop="price">250</div>
            </div>

How do I get the Quantity into a JavaScript function and return the value of 5? Without having to edit the code of the site itself?

I have been playing with this but I am sure this is completely and utterly incorrect.

function () {
  var x = document.getElementsByClassName("SameClass").getAttribute("itemprop"); 
  if (x = 'price')
  {
  var a = document.getElementsByClassName("SameClass").getAttribute("itemprop").innerText;
  return a;
  }
}

Aucun commentaire:

Enregistrer un commentaire