We have some code that populates a numerical value if greater than 1, and if not, it prints "In-Stock".
4 Answers
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
$(()=>{
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
var inventory = $('#our_inventory').getAttribute(class);
if (inventory < 0) {
$('#our_inventory').html('In-Stock');
} else {
$('#our_inventory').html('Inventory:' + inventory);
}
2 Comments
beerwin
what is
inventory?andreas
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.
our_inventoryis an id, not a value.