Created
January 27, 2014 09:56
-
-
Save apphp/8645895 to your computer and use it in GitHub Desktop.
Converting a text input to a select with JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sometimes you need the page dimanically changes a texbox with a dropdown box. | |
// On the example below we show you how to perform this work. | |
// source: http://www.apphp.com/index.php?snippet=javascript-converting-text-input-to-select | |
<div id="buy-buttons"> | |
<label>Quantity:</label> | |
<input name="txtQuantity" id="txtQuantity" type="text" value="1" /> | |
<input type="submit" name="btnAddToCart" value="Add To Cart" id="btnAddToCart" /> | |
</div> | |
<script> | |
// First way: | |
$("#txtQuantity").remove(); | |
$("#buy-buttons").append('<select name="txtQuantity" id="txtQuantity"></select>'); | |
$("#txtQuantity").append('<option value="1" selected="selected">1</option>' + | |
'<option value="2">2</option>' + | |
'<option value="3">3</option>' + | |
'<option value="4">4</option>' + | |
'<option value="5">5</option>'); | |
// Second way: | |
$("#txtQuantity") | |
.replaceWith('<select name="txtQuantity" id="txtQuantity">' + | |
'<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>' + | |
'</select>'); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment