0

I need to create an if statement in Js fetching when there's only 1 left (product) in stock. How would I go about this?

HTML on productpage:

<div id="product-spec">
 <div class="row">
  <ul class="col-xs-12">
   <li class="h4">
    <span class="availabilty"
     <span class="small">1 left</span>
    </span>
   </li>
  </ul>
 </div>
</div>

My Javascript so far:

if (.small == 1 left) { 
// execute this code 
}

W3 Schools - If Statements doesn't make me more sane.

Hbaecklund
  • 261
  • 1
  • 4
  • 16
  • Don's use W3Schools for reference, ever. Check [this question](http://stackoverflow.com/questions/3808808/how-to-get-element-by-class-in-javascript) to figure how to select an element by class in javascript. Look [here](https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML) to learn about `innerHTML` in javascript and [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators) to get your head around value comparison in javascript. Good luck! – Luuuud Jan 11 '16 at 10:06

2 Answers2

1

try

if(document.querySelectorAll(".small")[0].innerHTML==="1 left"){
// get funky
}

[EDIT]

if($(".small").eq(0).text() == "1 left"){ 
$(".small").eq(0).prepend( "<div style="lots of styling"></div>" ); 
 }
lucas
  • 1,485
  • 13
  • 22
0

You write like this

var product = document.getElementsByClassName('modal_link')[0];
// if inner text exist
if(product.innerText) {
    // getting count products from string
    var countProduct = parseInt(product.innerText, 10);
    if(countProduct === 1) {
        console.log('Left');
    }
}
Artem Chernov
  • 856
  • 2
  • 8
  • 26