Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
109
rated 0 times [  114] [ 5]  / answers: 1 / hits: 48341  / 12 Years ago, sat, june 23, 2012, 12:00:00

I'm doing some front end development on a hosted e-commerce platform. The system sends a text input for the customer to choose the quantity of items they want to add to their cart, but I would prefer a select. I don't have access to the markup the system spits out in this regard, so I'd like to use JavaScript to change the text input to a select element.



I used jQuery to remove the select and duplicate the input element with a select with the right choices, but when I try to add something to my cart, only one item is added, regardless of the choice in the select.



Here's the native markup:



<div id=buy-buttons>
<label>Quantity:</label>
<input name=txtQuantity type=text value=1 id=txtQuantity class=ProductDetailsQuantityTextBox>
<input type=submit name=btnAddToCart value=Add To Cart id=btnAddToCart class=ProductDetailsAddToCartButton ThemeButton AddToCartThemeButton ProductDetailsAddToCartThemeButton>
</div>


Here's the jQuery I'm running to update my markup.



 $(#txtQuantity).remove();
$(#buy-buttons).append('<select id=txtQuantity type=text value=1 class=ProductDetailsQuantityTextBox></select>');
$(#txtQuantity).append('<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>');


Does anyone know why this isn't working? Shouldn't the select be submitting the information to through the form in the same way as the text input?



Thanks!


More From » jquery

 Answers
44

You haven't set the field name. Add name='txtQuantity' to the select. Without a name attribute the value of the field will not get posted back to the server.



Also rather than setting value='1' on the select (which isn't doing anything), add selected='selected' to the first option. The result is the same as by default the first option is selected.



You are presumably ending up with the select after the submit button with the above code. However that has been covered in another answer.


[#84709] Thursday, June 21, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazminuniquer

Total Points: 63
Total Questions: 121
Total Answers: 96

Location: Cambodia
Member since Thu, May 21, 2020
4 Years ago
;