jQuery - need to update select list?

Hi,
How can I modify my jquery setNewPrice function to also rebuild my QTY select list to correspond with the items in stock?
If I add another field called data-stock="10", can this be used to rebuild the my QTY drop down list?

Thanks in advance.

function setNewPrice(cnt){
		  var price = $("#spec1_grid"+cnt).find(":selected").data('price');
		  $("#price_grid"+cnt).html("Price $ "+price);
};


<div class="curvedbox styled-select">QTY&nbsp;&nbsp;
         	<select class="shop_qty" name="qty_grid2" id="qty_grid2" style.zindex="-6">
			<option value="1">1</option>
			<option value="2">2</option>
			<option value="3">3</option>
			<option value="4">4</option>
			<option value="5">5</option>
			<option value="6">6</option>
			<option value="7">7</option>
			<option value="8">8</option>
			<option value="9">9</option>
			<option value="10">10</option>
		</select>
</div>



<div class="curvedbox styled-select">Sizes</br>
          <select class="shop_product" name="spec1_grid2" id="spec1_grid2"  onChange="setNewPrice('2')" style.zindex="-6">
			<option value="10" data-price="45.00" data-stock="10">10 - stock (10)</option>
			<option value="12" data-price="45.00" data-stock="30">12 - stock (30)</option>
			<option value="14" data-price="45.00" data-stock="30">14 - stock (30)</option>
			<option value="16" data-price="45.00" data-stock="30">16 - stock (30)</option>
			<option value="S" data-price="45.00" data-stock="30">S - stock (30)</option>
			<option value="M" data-price="45.00" data-stock="30">M - stock (30)</option>
			<option value="L" data-price="45.00" data-stock="0">L - stock (0)</option>
			<option value="XL" data-price="45.00" data-stock="20">XL - stock (20)</option>
			<option value="2XL" data-price="45.00" data-stock="10">2XL - stock (10)</option>          
	  </select>
</div>

Open in new window

sabecsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Slick812Commented:
greetings sabecs, , I think I can see what you mean by  "used to rebuild the my QTY drop down list" .

But not sure about what you may want in the options.
So here is my version of your function from above, and it works for me in testing in laptop firefox -

<script>/* <![CDATA[ */

function setNewPrice(cnt){
var price = parseInt($("#spec1_grid"+cnt).find(":selected").data('stock'));
if (!price) {alert("OUT OF STOCK, no order"); return;}

++price;
opts = "";
for (var i=1; i < price; ++i){
    opts += '<option>'+i+'</option>';
}

$("#qty_grid"+cnt).html(opts);
price = $("#spec1_grid"+cnt).find(":selected").data('price');
$("#price_grid"+cnt).html("Price $ "+price);

}
/* ]]> */</script>

Open in new window


And you do not need a value in an option if it is identical to the display, so this -
    <option>1</option>
is the same as -
     <option value="1">1</option>

try this code, let me know if you have problems.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sabecsAuthor Commented:
Thanks Slick812, that is perfect.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.