0

We have some code that populates a numerical value if greater than 1, and if not, it prints "In-Stock".

4
  • Add the relevant code pleaaase.. Commented Oct 14, 2016 at 11:53
  • Provide the whole code please. what's the value in span ? Commented Oct 14, 2016 at 11:53
  • maybe I'm tired but I can't understand your question. our_inventory is an id, not a value. Commented Oct 14, 2016 at 11:53
  • don't edit you question so that it's totally blank. It's fine how it is Commented Oct 14, 2016 at 14:21

4 Answers 4

2

You mean

var $inv = $('#our_inventory'), val = parseInt($inv.text(),10);
$inv.text(val>0?"Inventory:"+val:"In-Stock");
Sign up to request clarification or add additional context in comments.

2 Comments

use prepend() method
That may have side effects depending on the css
0

Add the following lines in the appropiate place of your jQuery script. You just need to use the .before method from jQuery to place some text before the chosen element.

<script>
if(parseInt($('#our_inventory').text(),10) > 0)
    $('our_inventory').before("Inventory: ");
</script>

Comments

0

$(()=>{
  
  var val = -5;
  trigger();
  $('#up').click(()=>{
     val++;
     $('#hnd').text(val);
   trigger();
  });

 $('#down').click(()=>{
       val--;
     $('#hnd').text(val);    
     trigger();
  });
  

function trigger(){
      var our_inventory_value = Number($('#hnd').text());
      $('#show').text(our_inventory_value);
    
     if(our_inventory_value<0){
      $('#our_inventory').text('in inventory');
     }else{
      $('#our_inventory').text(our_inventory_value);
     }

}

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <h3>Demo </h3>
  <button id='up'> up </button>
  <button id='down' >down </button>
  <br/>
  <span id="our_inventory" class="value"></span> <br/><br/>
   <span id="show" class="value"></span>
  <input type='hidden' id='hnd' value='-5'/>
  </form>

Comments

-1
var inventory = $('#our_inventory').getAttribute(class);

if (inventory < 0) {
    $('#our_inventory').html('In-Stock');
    } else {
    $('#our_inventory').html('Inventory:' + inventory);
    }

2 Comments

what is inventory?
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.