I want the user to input a piece of text in an alert box.
HTML:
<form>
<select class="favoritefood">
<optgroup label="Dairy products">
<option>Cheese</option>
<option>Egg</option>
<option id="newDairy">NEW</option>
</optgroup>
<optgroup label="Vegetables">
<option>Cabbage</option>
<option>Lettuce</option>
<option>Beans</option>
<option>Onions</option>
<option>Courgettes</option>
<option id="newVeg">NEW</option>
</optgroup>
</select>
</form>
JS:
$('.favoritefood').change(function () {
var id = $(this).find('option:selected').attr('id');
if (id == 'newDairy') {
alert("input");
};
});
I know it is possible to have an input inside the alert box, but how?
That would be the prompt box
var result = prompt('type something');
FIDDLE
You can also specify a default value for the prompt using
var value = prompt('Enter Value', 'Default Value');
Also note: "Cancel" from the prompt will return null.
Try a demo here: http://www.w3schools.com/js/tryit.asp?filename=tryjs_prompt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With